diff --git a/Module/Plex-PS-Module.psd1 b/Module/Plex-PS-Module.psd1 new file mode 100644 index 0000000..501c6f4 --- /dev/null +++ b/Module/Plex-PS-Module.psd1 @@ -0,0 +1,148 @@ +<# + =========================================================================== + Created with: SAPIEN Technologies, Inc., PowerShell Studio 2019 v5.6.163 + Created on: 2019-05-23 9:44 AM + Created by: Administrator + Organization: + Filename: Plex-PS-Module.psd1 + ------------------------------------------------------------------------- + Module Manifest + ------------------------------------------------------------------------- + Module Name: Plex-PS-Module + =========================================================================== +#> + + +@{ + + # Script module or binary module file associated with this manifest + RootModule = 'Plex-PS-Module.psm1' + + # Version number of this module. + ModuleVersion = '1.2.1.0' + + # ID used to uniquely identify this module + GUID = '4a2c4842-30fb-48bb-beef-b440615fae59' + + # Author of this module + Author = 'Maxime B' + + # Company or vendor of this module + CompanyName = 'BVAULT' + + # Copyright statement for this module + Copyright = '(c) 2019. All rights reserved.' + + # Description of the functionality provided by this module + Description = 'Module description' + + # Minimum version of the Windows PowerShell engine required by this module + PowerShellVersion = '5.0' + + # Name of the Windows PowerShell host required by this module + PowerShellHostName = '' + + # Minimum version of the Windows PowerShell host required by this module + PowerShellHostVersion = '' + + # Minimum version of the .NET Framework required by this module + DotNetFrameworkVersion = '2.0' + + # Minimum version of the common language runtime (CLR) required by this module + CLRVersion = '2.0.50727' + + # Processor architecture (None, X86, Amd64, IA64) required by this module + ProcessorArchitecture = 'None' + + # Modules that must be imported into the global environment prior to importing + # this module + RequiredModules = @() + + # Assemblies that must be loaded prior to importing this module + RequiredAssemblies = @() + + # Script files (.ps1) that are run in the caller's environment prior to + # importing this module + ScriptsToProcess = @() + + # Type files (.ps1xml) to be loaded when importing this module + TypesToProcess = @() + + # Format files (.ps1xml) to be loaded when importing this module + FormatsToProcess = @() + + # Modules to import as nested modules of the module specified in + # ModuleToProcess + NestedModules = @() + + # Functions to export from this module + FunctionsToExport = @( + 'Get-PlexTVToken', + 'Get-PlexTVServerList', + 'Test-PlexConnection', + 'Get-PlexSectionKey', + 'Get-PlexLibrarieSections', + 'Get-PlexOnDeck', + 'Get-PlexListLibraryContent', + 'Get-PlexVideoMetadata', + 'Set-PlexVideoWatchedStatus', + 'Get-PlexSearchMedia', + 'Set-PlexCollectionMovie', + 'Get-PlexServerPreferences', + 'Get-PlexLocalServers', + 'Get-PlexSystemInformation', + 'Get-PlexAvailableAgents', + 'Get-PlexSessionsStatus', + 'Get-PlexSessionsHistory' + ) #For performance, list functions explicitly + + # Cmdlets to export from this module + CmdletsToExport = '*' + + # Variables to export from this module + VariablesToExport = '*' + + # Aliases to export from this module + AliasesToExport = '*' #For performance, list alias explicitly + + # DSC class resources to export from this module. + #DSCResourcesToExport = '' + + # List of all modules packaged with this module + ModuleList = @() + + # List of all files packaged with this module + FileList = @() + + # Private data to pass to the module specified in ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. + PrivateData = @{ + + #Support for PowerShellGet galleries. + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + # Tags = @() + + # A URL to the license for this module. + # LicenseUri = '' + + # A URL to the main website for this project. + # ProjectUri = '' + + # A URL to an icon representing this module. + # IconUri = '' + + # ReleaseNotes of this module + # ReleaseNotes = '' + + } # End of PSData hashtable + + } # End of PrivateData hashtable +} + + + + + + + diff --git a/Module/Plex-PS-Module.psm1 b/Module/Plex-PS-Module.psm1 new file mode 100644 index 0000000..4d93a7b --- /dev/null +++ b/Module/Plex-PS-Module.psm1 @@ -0,0 +1,333 @@ +<# + =========================================================================== + Created with: SAPIEN Technologies, Inc., PowerShell Studio 2019 v5.6.163 + Created on: 2019-05-23 9:44 AM + Created by: Administrator + Organization: + Filename: Plex-PS-Module.psm1 + ------------------------------------------------------------------------- + Module Name: Plex-PS-Module + =========================================================================== +#> + +<# + .SYNOPSIS + Get plex user toker from plex.tv + + .DESCRIPTION + Can be use for the Token parameter in othe functions + if you dont know how to get it yourself + + .PARAMETER Credentials + A description of the Credentials parameter. + + .EXAMPLE + PS C:\> Get-PlexTVToken + + .NOTES + Additional information about the function. +#> +function Get-PlexTVToken +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $false)] + [pscredential]$Credentials + ) + + $url = "https://plex.tv/users/sign_in.xml" + if ([string]::IsNullOrEmpty($Credentials) -ne $true) + { + $cred = Get-Credential -Credential $Credentials + } + else + { + $cred = Get-Credential + } + $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $cred.GetNetworkCredential().UserName, $cred.GetNetworkCredential().Password))) + $headers = @{ } + $headers.Add("Authorization", "Basic $($base64AuthInfo)") | out-null + $headers.Add("X-Plex-Client-Identifier", "TESTSCRIPTV1") | Out-Null + $headers.Add("X-Plex-Product", "Test script") | Out-Null + $headers.Add("X-Plex-Version", "V1") | Out-Null + [xml]$res = Invoke-RestMethod -Headers $headers -Method Post -Uri:$url + $token = $res.user.authenticationtoken + return $token +} + +<# + .SYNOPSIS + Use to Get list of Servers and the IP and Ports + + .DESCRIPTION + Use this to fill the $hostname Variable for all other founctions if you dont know it by heart + + .PARAMETER Credentials + A description of the Credentials parameter. + + .EXAMPLE 1 + PS C:\> $UserName = PLEX_USERNAME + PS C:\> $PlexPassword = PLEX_PASSWORD + PS C:\> $password = ConvertTo-SecureString $PlexPassword -AsPlainText -Force + PS C:\> $Cred = New-Object System.Management.Automation.PSCredential ($UserName, $password) + PS C:\> $PlexServerList = Get-PlexServerList -Credentials $Cred | where-object {$_.Name -eq "PLEX_SERVER_NAME"} + PS C:\> $PlexServerList.HostAdress + + .EXAMPLE 2 + PS C:\> $UserName = PLEX_USERNAME + PS C:\> $PlexPassword = PLEX_PASSWORD + PS C:\> $password = ConvertTo-SecureString $PlexPassword -AsPlainText -Force + PS C:\> $Cred = New-Object System.Management.Automation.PSCredential ($UserName, $password) + PS C:\> Get-PlexServerList -Credentials $Cred + + .EXAMPLE 3 + PS C:\> Get-PlexServerList + + .EXAMPLE 4 + PS C:\> $PlexServerList = Get-PlexServerList | where-object {$_.Name -eq "PLEX_SERVER_NAME"} + PS C:\> $PlexServerList.HostAdress + + .NOTES + The Credential Parameter is not Mandatory. + Also it is best to use exemple 2,3 first and then use 1,4 to put in variable this way you will see the list first. +#> +function Get-PlexTVServerList +{ + [CmdletBinding()] + [OutputType([array])] + param + ( + [Parameter(Mandatory = $false)] + [pscredential]$Credentials + ) + + $url = "https://plex.tv/pms/servers.xml" + if ([string]::IsNullOrEmpty($Credentials) -ne $true) + { + $cred = Get-Credential -Credential $Credentials + } + else + { + $cred = Get-Credential + } + $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $cred.GetNetworkCredential().UserName, $cred.GetNetworkCredential().Password))) + $headers = @{ } + $headers.Add("Authorization", "Basic $($base64AuthInfo)") | out-null + $headers.Add("X-Plex-Client-Identifier", "TESTSCRIPTV1") | Out-Null + $headers.Add("X-Plex-Product", "Test script") | Out-Null + $headers.Add("X-Plex-Version", "V1") | Out-Null + [xml]$res = Invoke-RestMethod -Headers $headers -Method GET -Uri $url + $Servers = $res.MediaContainer.Server | select name, localAddresses, port + $array = @() + foreach ($Server in $Servers) + { + $Object = New-Object PSObject + Add-Member -InputObject $Object -TypeName Noteproperty -NotePropertyName Name -NotePropertyValue $Server.name + $IpAdress = $Server.localAddresses + if ($IpAdress -like '*,*') + { + $IP = $IpAdress.split(',') + $HostAddress = $IP[0] + ":" + $Server.port + } + else + { + $HostAddress = $IpAdress + ":" + $Server.port + } + Add-Member -InputObject $Object -TypeName Noteproperty -NotePropertyName HostAdress -NotePropertyValue $HostAddress + $array += $object + } + return $array +} + +function Test-PlexConnection ($hostname, $Token) +{ + $URI = "http://$hostname/?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer +} + +<# + .SYNOPSIS + Get the Section Key From Library Sections + + .DESCRIPTION + A detailed description of the Get-PlexSectionKey function. + + .PARAMETER hostname + A description of the hostname parameter. + + .PARAMETER Token + A description of the Token parameter. + + .PARAMETER Libraryname + A description of the Library parameter. + + .EXAMPLE + PS C:\> Get-PlexSectionKey -Library $value1 + + .NOTES + Additional information about the function. +#> +function Get-PlexSectionKey +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [string]$hostname, + [Parameter(Mandatory = $true)] + [string]$Token, + [Parameter(Mandatory = $true)] + [string]$LibraryName + ) + + $Libraries = Get-PlexLibrarieSections -hostname $hostname -Token $Token + $SelectedSection = $Libraries | Where-Object { $_.title -eq $LibraryName } + $SelectedSection.key +} + +function Get-PlexLibrarieSections ($hostname, $Token) +{ + $URI = "http://$hostname/library/sections?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.Directory | Select-Object title, type, key +} + +function Get-PlexOnDeck ($hostname, $Token) +{ + $URI = "http://$hostname/library/onDeck?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.Video +} + + +<#function Get-PlexUpdateLibrary ($hostname, $Token, $Section) +{ + $URI = "http://$hostname/library/sections/$Section/refresh?X-Plex-Token=$Token" + Invoke-WebRequest -Uri $URI +} + +function Get-PlexRefreshEntireLibrary ($hostname, $Token, $Section) +{ + $URI = "http://$hostname/library/sections/$Section/refresh?force=1&X-Plex-Token=$Token" + Invoke-WebRequest -Uri $URI +} +#> +function Get-PlexListLibraryContent ($hostname, $Token, $LibraryName) +{ + $Section = Get-PlexSectionKey -hostname $hostname -Token $Token -LibraryName $LibraryName + $URI = "http://$hostname/library/sections/$Section/all?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.Video +} + +function Get-PlexVideoMetadata ($hostname, $Token, $RatingKey) +{ + $URI = "http://$hostname/library/metadata/$RatingKey" + "?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.Video + +} + +function Set-PlexVideoWatchedStatus +{ + Param ( + [Parameter(Mandatory = $True, Position = 1)] + [string]$hostname, + [Parameter(Mandatory = $True, Position = 1)] + [string]$token, + [Parameter(Mandatory = $True, ValueFromPipeline = $true, Position = 0)] + [PSobject[]]$RatingKey, + [Parameter(Mandatory = $True)] + [ValidateSet('Watched', 'Unwatched')] + [string]$ViewedStatus + ) + + If ($ViewedStatus.ToLower() -eq "watched") + { + $ScrobbleAction = "scrobble" + } + Else + { + $ScrobbleAction = "unscrobble" + } + $URI = "http://$hostname/:/$ScrobbleAction" + "?key=" + $RatingKey + "&identifier=com.plexapp.plugins.library&" + "X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Invoke + +} + +function Get-PlexSearchMedia ($hostname, $Token, $Search) +{ + $URI = "http://$hostname/search/?query=$Search&" + "X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.Video +} + +function Set-PlexCollectionMovie ($hostname, $Token, $CollectionName, $Section, $RatingKey) +{ + $URL = @" +type=1&id=$RatingKey&collection[0].tag.tag=$CollectionName +"@ + $Encode = [uri]::EscapeUriString($URL) + $Encode = $Encode.Replace('%20', '+') + $URI = "http://$hostname/library/sections/$Section/all?" + $Encode + "&X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI -Method PUT + $Invoke.StatusDescription +} + + +function Get-PlexServerPreferences ($hostname, $Token) +{ + $URI = "http://$hostname/:/prefs/?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.setting +} + +function Get-PlexLocalServers ($hostname, $Token) +{ + $URI = "http://$hostname/servers/?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.server +} + +function Get-PlexSystemInformation ($hostname, $Token) +{ + $URI = "http://$hostname/system/?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.Directory +} + +function Get-PlexAvailableAgents ($hostname, $Token) +{ + $URI = "http://$hostname/system/agents/?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.agent +} + +function Get-PlexSessionsStatus ($hostname, $Token) +{ + $URI = "http://$hostname/status/sessions/?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.video +} + +function Get-PlexSessionsHistory ($hostname, $Token) +{ + $URI = "http://$hostname/status/sessions/history/all/?X-Plex-Token=$Token" + $Invoke = Invoke-WebRequest -Uri $URI + $Xmlfile = [XML]$Invoke.content + $Xmlfile.MediaContainer.video +} diff --git a/Plex-PS-Module.psd1 b/Plex-PS-Module.psd1 index 84240b6..501c6f4 100644 --- a/Plex-PS-Module.psd1 +++ b/Plex-PS-Module.psd1 @@ -19,7 +19,7 @@ RootModule = 'Plex-PS-Module.psm1' # Version number of this module. - ModuleVersion = '1.2.0.0' + ModuleVersion = '1.2.1.0' # ID used to uniquely identify this module GUID = '4a2c4842-30fb-48bb-beef-b440615fae59'