PSImmich.psm1
#Region './prefix.ps1' -1 # The content of this file will be prepended to the top of the psm1 module file. This is useful for custom module setup is needed on import. #EndRegion './prefix.ps1' 3 #Region './Classes/ImmichSession.class.ps1' -1 [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'JWT token retreived in plain text')] class ImmichSession { [string]$BaseUri [string]$AuthMethod [securestring]$AccessToken [pscredential]$Credential [securestring]$JWT [string]$APIUri [string]$ImmichVersion [string]$SessionID ImmichSession ([string]$BaseUri, [securestring]$AccessToken) { Write-Debug -Message 'ImmichSession.Class; Running constructor accesstoken' $this.SessionID = (New-Guid).Guid $this.BaseUri = $BaseUri $this.APIUri = "$BaseUri/api" $this.AuthMethod = 'AccessToken' $this.AccessToken = $AccessToken $this.GetStatus() Write-Verbose -Message "Connected to Immich instance at $($this.BaseUri) with AccessToken" } ImmichSession ([string]$BaseUri, [pscredential]$Credential) { Write-Debug -Message 'ImmichSession.Class; Running constructor credential' $this.SessionID = (New-Guid).Guid $this.BaseUri = $BaseUri $this.APIUri = "$BaseUri/api" $this.AuthMethod = 'Credential' $this.Credential = $Credential $this.AuthenticateCredential() $this.GetStatus() Write-Verbose -Message "Connected to Immich instance at $($this.BaseUri) with Credentials" } hidden AuthenticateCredential() { $BodyObject = @{ password = $this.Credential.GetNetworkCredential().Password email = $this.Credential.Username } $JWTResponse = InvokeImmichRestMethod -NoAuth -Method:'Post' -ImmichSession $this -RelativePath '/auth/login' -Body $BodyObject $this.JWT = ConvertTo-SecureString -String $JWTResponse.accessToken -AsPlainText -Force Remove-Variable -Name JWTResponse } hidden GetStatus() { $Status = InvokeImmichRestMethod -Method:'Get' -ImmichSession $this -RelativePath '/server-info/version' $this.ImmichVersion = "$($Status.Major).$($Status.Minor).$($Status.Patch)" Remove-Variable -Name Status } } #EndRegion './Classes/ImmichSession.class.ps1' 56 #Region './Private/InvokeImmichRestMethod.ps1' -1 <#PSScriptInfo { "VERSION": "1.0.0", "GUID": "fd399995-5714-4944-b370-23b2b4dcd299", "FILENAME": "InvokeImmichRestMethod.ps1", "AUTHOR": "Hannes Palmquist", "CREATEDDATE": "2024-03-07", "COMPANYNAME": [], "COPYRIGHT": "(c) 2024, Hannes Palmquist, All Rights Reserved" } PSScriptInfo#> function InvokeImmichRestMethod { <# .DESCRIPTION Function that is responsible for making the rest api web call .PARAMETER NoAuth Specifies that the REST API call do not need authentication .PARAMETER Method Defines the method to use when calling the REST API, valid values GET,POST etc. .PARAMETER ImmichSession A ImmichSession object to use for the call. .PARAMETER RelativePath The REST API path relative to the base URL .PARAMETER Body Defines body attributes for the REST API call .PARAMETER Headers Defines header attributes for the REST API call .EXAMPLE InvokeImmichRestMethod #> [CmdletBinding()] # Enabled advanced function support param( [switch]$NoAuth, [string]$Method, [immichsession]$ImmichSession, [string]$RelativePath, [hashtable]$Body = @{}, [hashtable]$Headers = @{} ) if (-not $ImmichSession) { Write-Debug -Message 'InvokeImmichRestMethod; No ImmichSession passed as parameter' if ($script:ImmichSession) { Write-Debug -Message 'InvokeImmichRestMethod; ImmichSession found in script scope' $ImmichSession = $script:ImmichSession } else { Write-Error -Message 'No Immich Session established, please call Connect-Immich' } } $InvokeRestMethodSplat = @{ Method = $Method Uri = "$($ImmichSession.ApiUri)$($RelativePath)" } if (-not $NoAuth) { switch ($ImmichSession.AuthMethod) { 'Credential' { $InvokeRestMethodSplat.Authentication = 'Bearer' $InvokeRestMethodSplat.Token = $ImmichSession.JWT } 'AccessToken' { $Headers.'X-API-Key' = (ConvertFrom-SecureString -SecureString $ImmichSession.AccessToken -AsPlainText) } } } if ($Headers.Keys.Count -gt 0) { $InvokeRestMethodSplat.Headers = $Headers } if ($InvokeRestMethodSplat.Method -eq 'Get') { if ($Body.Keys.Count -gt 0 ) { $InvokeRestMethodSplat.Body = $Body } } elseif ($InvokeRestMethodSplat.Method -eq 'Post') { # Might need to be changed, some post requests require formdata $InvokeRestMethodSplat.Body = $Body | ConvertTo-Json -Compress $InvokeRestMethodSplat.ContentType = 'application/json' } Write-Debug -Message "InvokeImmichRestMethod; Calling Invoke-RestMethod with settings`r`n$($InvokeRestMethodSplat | ConvertTo-Json)" Invoke-RestMethod @InvokeRestMethodSplat -Verbose:$false | ForEach-Object { $_ } } #endregion #EndRegion './Private/InvokeImmichRestMethod.ps1' 102 #Region './Private/Write-PSProgress.ps1' -1 function Write-PSProgress { <# .SYNOPSIS Wrapper for PSProgress .DESCRIPTION This function will automatically calculate items/sec, eta, time remaining as well as set the update frequency in case the there are a lot of items processing fast. .PARAMETER Activity Defines the activity name for the progressbar .PARAMETER Id Defines a unique ID for this progressbar, this is used when nesting progressbars .PARAMETER Target Defines a arbitrary text for the currently processed item .PARAMETER ParentId Defines the ID of a parent progress bar .PARAMETER Completed Explicitly tells powershell to set the progress bar as completed removing it from view. In some cases the progress bar will linger if this is not done. .PARAMETER Counter The currently processed items counter .PARAMETER Total The total number of items to process .PARAMETER StartTime Sets the start datetime for the progressbar, this is required to calculate items/sec, eta and time remaining .PARAMETER DisableDynamicUpdateFrquency Disables the dynamic update frequency function and every item will update the status of the progressbar .PARAMETER NoTimeStats Disables calculation of items/sec, eta and time remaining .EXAMPLE 1..10000 | foreach-object -begin {$StartTime = Get-Date} -process { Write-PSProgress -Activity 'Looping' -Target $PSItem -Counter $PSItem -Total 10000 -StartTime $StartTime } Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines #> [CmdletBinding()] param( [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'Standard')] [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'Completed')] [string] $Activity, [Parameter(Position = 1, ParameterSetName = 'Standard')] [Parameter(Position = 1, ParameterSetName = 'Completed')] [ValidateRange(0, 2147483647)] [int] $Id, [Parameter(Position = 2, ParameterSetName = 'Standard')] [string] $Target, [Parameter(Position = 3, ParameterSetName = 'Standard')] [Parameter(Position = 3, ParameterSetName = 'Completed')] [ValidateRange(-1, 2147483647)] [int] $ParentId, [Parameter(Position = 4, ParameterSetname = 'Completed')] [switch] $Completed, [Parameter(Mandatory = $true, Position = 5, ParameterSetName = 'Standard')] [long] $Counter, [Parameter(Mandatory = $true, Position = 6, ParameterSetName = 'Standard')] [long] $Total, [Parameter(Position = 7, ParameterSetName = 'Standard')] [datetime] $StartTime, [Parameter(Position = 8, ParameterSetName = 'Standard')] [switch] $DisableDynamicUpdateFrquency, [Parameter(Position = 9, ParameterSetName = 'Standard')] [switch] $NoTimeStats ) # Define current timestamp $TimeStamp = (Get-Date) # Define a dynamic variable name for the global starttime variable $StartTimeVariableName = ('ProgressStartTime_{0}' -f $Activity.Replace(' ', '')) # Manage global start time variable if ($PSBoundParameters.ContainsKey('Completed') -and (Get-Variable -Name $StartTimeVariableName -Scope Global -ErrorAction SilentlyContinue)) { # Remove the global starttime variable if the Completed switch parameter is users try { Remove-Variable -Name $StartTimeVariableName -ErrorAction Stop -Scope Global } catch { throw $_ } } elseif (-not (Get-Variable -Name $StartTimeVariableName -Scope Global -ErrorAction SilentlyContinue)) { # Global variable do not exist, create global variable if ($null -eq $StartTime) { # No start time defined with parameter, use current timestamp as starttime Set-Variable -Name $StartTimeVariableName -Value $TimeStamp -Scope Global $StartTime = $TimeStamp } else { # Start time defined with parameter, use that value as starttime Set-Variable -Name $StartTimeVariableName -Value $StartTime -Scope Global } } else { # Global start time variable is defined, collect and use it $StartTime = Get-Variable -Name $StartTimeVariableName -Scope Global -ErrorAction Stop -ValueOnly } # Define frequency threshold $Frequency = [Math]::Ceiling($Total / 100) switch ($PSCmdlet.ParameterSetName) { 'Standard' { # Only update progress is any of the following is true # - DynamicUpdateFrequency is disabled # - Counter matches a mod of defined frequecy # - Counter is 0 # - Counter is equal to Total (completed) if (($DisableDynamicUpdateFrquency) -or ($Counter % $Frequency -eq 0) -or ($Counter -eq 1) -or ($Counter -eq $Total)) { # Calculations for both timestats and without $Percent = [Math]::Round(($Counter / $Total * 100), 0) # Define count progress string status $CountProgress = ('{0}/{1}' -f $Counter, $Total) # If percent would turn out to be more than 100 due to incorrect total assignment revert back to 100% to avoid that write-progress throws if ($Percent -gt 100) { $Percent = 100 } # Define write-progress splat hash $WriteProgressSplat = @{ Activity = $Activity PercentComplete = $Percent CurrentOperation = $Target } # Add ID if specified if ($Id) { $WriteProgressSplat.Id = $Id } # Add ParentID if specified if ($ParentId) { $WriteProgressSplat.ParentId = $ParentId } # Calculations for either timestats and without if ($NoTimeStats) { $WriteProgressSplat.Status = ('{0} - {1}%' -f $CountProgress, $Percent) } else { # Total seconds elapsed since start $TotalSeconds = ($TimeStamp - $StartTime).TotalSeconds # Calculate items per sec processed (IpS) $ItemsPerSecond = ([Math]::Round(($Counter / $TotalSeconds), 2)) # Calculate seconds spent per processed item (for ETA) $SecondsPerItem = if ($Counter -eq 0) { 0 } else { ($TotalSeconds / $Counter) } # Calculate seconds remainging $SecondsRemaing = ($Total - $Counter) * $SecondsPerItem $WriteProgressSplat.SecondsRemaining = $SecondsRemaing # Calculate ETA $ETA = $(($Timestamp).AddSeconds($SecondsRemaing).ToShortTimeString()) # Add findings to write-progress splat hash $WriteProgressSplat.Status = ('{0} - {1}% - ETA: {2} - IpS {3}' -f $CountProgress, $Percent, $ETA, $ItemsPerSecond) } # Call writeprogress Write-Progress @WriteProgressSplat } } 'Completed' { Write-Progress -Activity $Activity -Id $Id -Completed } } } #EndRegion './Private/Write-PSProgress.ps1' 214 #Region './Public/ServerInfo/Get-IMServer.ps1' -1 function Get-IMServer { <# .DESCRIPTION Retreives all Immich server info properties .PARAMETER Session Optionally define a immich session object to use. This is useful when you are connected to more than one immich instance. -Session $Session .EXAMPLE Get-IMServer Retreives all Immich server info properties #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) $ResultObject = [ordered]@{} $Results = [array]@() $Results += Get-IMServerConfig -Session:$Session | Add-Member -MemberType NoteProperty -Name 'ObjectType' -Value 'Config' -PassThru $Results += Get-IMServerFeature -Session:$Session | Add-Member -MemberType NoteProperty -Name 'ObjectType' -Value 'Feature' -PassThru $Results += Get-IMServerInfo -Session:$Session | Add-Member -MemberType NoteProperty -Name 'ObjectType' -Value 'Info' -PassThru $Results += Get-IMServerStatistic -Session:$Session | Add-Member -MemberType NoteProperty -Name 'ObjectType' -Value 'Stats' -PassThru $Results += Get-IMServerVersion -Session:$Session | Add-Member -MemberType NoteProperty -Name 'ObjectType' -Value 'Version' -PassThru $Results += Get-IMSupportedMediaType -Session:$Session | Add-Member -MemberType NoteProperty -Name 'ObjectType' -Value 'Media' -PassThru $Results += Get-IMTheme -Session:$Session | Add-Member -MemberType NoteProperty -Name 'ObjectType' -Value 'Theme' -PassThru $Results += Test-IMPing -Session:$Session | Add-Member -MemberType NoteProperty -Name 'ObjectType' -Value 'Ping' -PassThru foreach ($Result in $Results) { foreach ($property in ($Result.PSObject.Properties.Name | Sort-Object)) { if ($property -eq 'ObjectType') { continue } $ResultObject.Add("$($Result.ObjectType)_$Property", $Result.$Property) } } return [pscustomobject]$ResultObject } #endregion #EndRegion './Public/ServerInfo/Get-IMServer.ps1' 47 #Region './Public/ServerInfo/Get-IMServerConfig.ps1' -1 function Get-IMServerConfig { <# .DESCRIPTION Retreives Immich server config .PARAMETER Session Optionally define a immich session object to use. This is useful when you are connected to more than one immich instance. -Session $Session .EXAMPLE Get-IMServerConfig Retreives Immich server config #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) InvokeImmichRestMethod -noauth -Method Get -RelativePath '/server-info/config' -ImmichSession:$Session } #endregion #EndRegion './Public/ServerInfo/Get-IMServerConfig.ps1' 25 #Region './Public/ServerInfo/Get-IMServerFeature.ps1' -1 function Get-IMServerFeature { <# .DESCRIPTION Retreives Immich server feature .PARAMETER Session Optionally define a immich session object to use. This is useful when you are connected to more than one immich instance. -Session $Session .EXAMPLE Get-IMServerFeature Retreives Immich server feature #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) InvokeImmichRestMethod -noauth -Method Get -RelativePath '/server-info/features' -ImmichSession:$Session } #endregion #EndRegion './Public/ServerInfo/Get-IMServerFeature.ps1' 25 #Region './Public/ServerInfo/Get-IMServerInfo.ps1' -1 function Get-IMServerInfo { <# .DESCRIPTION Retreives Immich server info .PARAMETER Session Optionally define a immich session object to use. This is useful when you are connected to more than one immich instance. -Session $Session .EXAMPLE Get-IMServerInfo Retreives Immich server info #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) InvokeImmichRestMethod -Method Get -RelativePath '/server-info' -ImmichSession:$Session } #endregion #EndRegion './Public/ServerInfo/Get-IMServerInfo.ps1' 25 #Region './Public/ServerInfo/Get-IMServerStatistic.ps1' -1 function Get-IMServerStatistic { <# .DESCRIPTION Retreives Immich server statistic .PARAMETER Session Optionally define a immich session object to use. This is useful when you are connected to more than one immich instance. -Session $Session .EXAMPLE Get-IMServerStatistic Retreives Immich server statistic #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) InvokeImmichRestMethod -Method Get -RelativePath '/server-info/statistics' -ImmichSession:$Session } #endregion #EndRegion './Public/ServerInfo/Get-IMServerStatistic.ps1' 25 #Region './Public/ServerInfo/Get-IMServerVersion.ps1' -1 function Get-IMServerVersion { <# .DESCRIPTION Retreives Immich server version .PARAMETER Session Optionally define a immich session object to use. This is useful when you are connected to more than one immich instance. -Session $Session .EXAMPLE Get-IMServerVersion Retreives Immich server version #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) $Result = InvokeImmichRestMethod -noauth -Method Get -RelativePath '/server-info/version' -ImmichSession:$Session return [pscustomobject]@{ version = "$($Result.Major).$($Result.Minor).$($Result.Patch)" } } #endregion #EndRegion './Public/ServerInfo/Get-IMServerVersion.ps1' 28 #Region './Public/ServerInfo/Get-IMSupportedMediaType.ps1' -1 function Get-IMSupportedMediaType { <# .DESCRIPTION Retreives Immich supported media type .PARAMETER Session Optionally define a immich session object to use. This is useful when you are connected to more than one immich instance. -Session $Session .EXAMPLE Get-IMSupportedMediaType Retreives Immich supported media type #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) InvokeImmichRestMethod -noauth -Method Get -RelativePath '/server-info/media-types' -ImmichSession:$Session } #endregion #EndRegion './Public/ServerInfo/Get-IMSupportedMediaType.ps1' 25 #Region './Public/ServerInfo/Get-IMTheme.ps1' -1 function Get-IMTheme { <# .DESCRIPTION Retreives Immich theme CSS .PARAMETER Session Optionally define a immich session object to use. This is useful when you are connected to more than one immich instance. -Session $Session .EXAMPLE Get-IMTheme Retreives Immich theme CSS #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) InvokeImmichRestMethod -Method Get -RelativePath '/server-info/theme' -ImmichSession:$Session } #endregion #EndRegion './Public/ServerInfo/Get-IMTheme.ps1' 25 #Region './Public/ServerInfo/Test-IMPing.ps1' -1 function Test-IMPing { <# .DESCRIPTION Ping Immich instance .PARAMETER Session Optionally define a immich session object to use. This is useful when you are connected to more than one immich instance. -Session $Session .EXAMPLE Test-IMPing Ping Immich instance #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) $Result = InvokeImmichRestMethod -noauth -Method Get -RelativePath '/server-info/ping' -ImmichSession:$Session if ($Result.res -eq 'pong') { return [pscustomobject]@{responds = $true } } else { return [pscustomobject]@{responds = $false } } } #endregion #EndRegion './Public/ServerInfo/Test-IMPing.ps1' 33 #Region './Public/Session/Connect-Immich.ps1' -1 function Connect-Immich { <# .DESCRIPTION Connect to a Immich instance .PARAMETER BaseURL Defines the base URL to the portainer instance -BaseURL 'https://portainer.contoso.com' .PARAMETER AccessToken Connects to portainer using a access token. This AccessToken can be generated from the Immich Web GUI. -AccessToken 'ptr_ABoR54bB1NUc4aNY0F2PhppP1tVDu2Husr3vEbPUsw5' .PARAMETER Credential Connect to portainer using username and password. Parameter accepts a PSCredentials object -Credential (Get-Credential) .PARAMETER PassThru This parameter will cause the function to return a ImmichSession object that can be stored in a variable and referensed with the -Session parameter on most cmdlets. -PassThru .EXAMPLE Connect-Immich -BaseURL 'https://portainer.contoso.com' -AccessToken 'ptr_ABoR54bB1NUc4aNY0F2PhppP1tVDu2Husr3vEbPUsw5=' Connect using access token .EXAMPLE Connect-Immich -BaseURL 'https://portainer.contoso.com' -Credentials (Get-Credential) Connect using username and password #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'AccessToken')] [CmdletBinding()] param( [Parameter(Mandatory)][string]$BaseURL, [Parameter(ParameterSetName = 'AccessToken')][string]$AccessToken, [Parameter(ParameterSetName = 'Credentials')][pscredential]$Credential, [switch]$PassThru ) switch ($PSCmdlet.ParameterSetName) { 'AccessToken' { $AccessTokenSS = ConvertTo-SecureString -String $AccessToken -AsPlainText -Force Remove-Variable -Name AccessToken $script:ImmichSession = [ImmichSession]::New($BaseURL, $AccessTokenSS) } 'Credentials' { $script:ImmichSession = [ImmichSession]::New($BaseURL, $Credential) } } if ($Passthru) { return $script:ImmichSession } } #EndRegion './Public/Session/Connect-Immich.ps1' 60 #Region './Public/Session/Disconnect-Immich.ps1' -1 function Disconnect-Immich { <# .DESCRIPTION Disconnect and cleanup session configuration .PARAMETER Session Defines a ImmichSession object that will be disconnected and cleaned up. .EXAMPLE Disconnect-Immich Disconnect from the default portainer session .EXAMPLE Disconnect-Immich -Session $Session Disconnect the specified session #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) InvokeImmichRestMethod -Method Post -RelativePath '/auth/logout' -ImmichSession:$Session # Remove ImmichSession variable if ($Session) { if ($script:ImmichSession.SessionID -eq $Session.SessionID) { Remove-Variable ImmichSession -Scope Script } } else { Remove-Variable ImmichSession -Scope Script } } #EndRegion './Public/Session/Disconnect-Immich.ps1' 39 #Region './Public/Session/Get-IMSession.ps1' -1 function Get-IMSession { <# .DESCRIPTION Displays the Immich Session object. .PARAMETER Session Optionally define a portainer session object to use. This is useful when you are connected to more than one portainer instance. -Session $Session .EXAMPLE Get-PSession Returns the ImmichSession, if none is specified, it tries to retreive the default #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) if ($Session) { Write-Debug -Message 'Get-PSession; ImmichSession was passed as parameter' return $Session } elseif ($script:ImmichSession) { Write-Debug -Message 'Get-PSession; ImmichSession found in script scope' return $script:ImmichSession } else { Write-Error -Message 'No Immich Session established, please call Connect-Immich' } } #endregion #EndRegion './Public/Session/Get-IMSession.ps1' 37 #Region './Public/Session/Get-IMStatus.ps1' -1 function Get-IMStatus { <# .DESCRIPTION Get public status for portainer instance .PARAMETER Session Optionally define a portainer session object to use. This is useful when you are connected to more than one portainer instance. -Session $Session .EXAMPLE Get-PStatus Get public status for portainer instance #> [CmdletBinding()] param( [Parameter()][ImmichSession]$Session = $null ) InvokeImmichRestMethod -NoAuth -Method Get -RelativePath '/status' -ImmichSession:$Session } #EndRegion './Public/Session/Get-IMStatus.ps1' 23 #Region './suffix.ps1' -1 # The content of this file will be appended to the top of the psm1 module file. This is useful for custom procesedures after all module functions are loaded. #EndRegion './suffix.ps1' 2 |