Public/Save-DattoBcdrAssetScreenshot.ps1
|
function Save-DattoBcdrAssetScreenshot { <# .SYNOPSIS Saves the latest BCDR asset screenshot verification image. .DESCRIPTION Implements GET /v1/bcdr/device/{serialNumber}/asset/{volumeName}/screenshot/latest and writes the returned PNG bytes to disk. Existing files are never overwritten unless -Force is specified. .PARAMETER SerialNumber The BCDR device serial number. .PARAMETER VolumeName The protected volume name returned by Get-DattoBcdrAsset. .PARAMETER Path A destination file or directory. A .png extension is appended when no extension is supplied. .PARAMETER CreateDirectory Creates a missing destination directory only after ShouldProcess approval. In -WhatIf mode, no directory is created. .PARAMETER Force Overwrites an existing file. .PARAMETER PassThru Returns the resulting FileInfo object. .PARAMETER Connection A connection object, name, or ID. .EXAMPLE Save-DattoBcdrAssetScreenshot -SerialNumber 'ABC123ABC1' -VolumeName 'VOLUME_UUID' -Path '.\screenshots\' -CreateDirectory -PassThru #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')] param( [Parameter(Mandatory, ValueFromPipelineByPropertyName)][ValidateNotNullOrEmpty()][string]$SerialNumber, [Parameter(Mandatory, ValueFromPipelineByPropertyName)][ValidateNotNullOrEmpty()][string]$VolumeName, [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$Path, [Parameter()][switch]$CreateDirectory, [Parameter()][switch]$Force, [Parameter()][switch]$PassThru, [Parameter()][AllowNull()][object]$Connection ) process { $safeSerial = ConvertTo-DattoApiSafeFileComponent -Value $SerialNumber $safeVolume = ConvertTo-DattoApiSafeFileComponent -Value $VolumeName $destination = Resolve-DattoApiOutputPath -Path $Path -DefaultFileName "Datto-BCDR-$safeSerial-$safeVolume-latest.png" -RequiredExtension '.png' -CreateDirectory:$CreateDirectory if ((Test-Path -LiteralPath $destination -PathType Leaf) -and -not $Force) { throw [System.IO.IOException]::new("The destination file already exists: $destination. Use -Force to overwrite it.") } if (-not $PSCmdlet.ShouldProcess($destination, 'Save latest Datto BCDR screenshot')) { return } $destinationDirectory = Split-Path -Path $destination -Parent if (-not (Test-Path -LiteralPath $destinationDirectory -PathType Container)) { if (-not $CreateDirectory) { throw [System.IO.DirectoryNotFoundException]::new("The destination directory does not exist: $destinationDirectory") } $null = New-Item -ItemType Directory -Path $destinationDirectory -Force } $serial = ConvertTo-DattoApiPathSegment -Value $SerialNumber $volume = ConvertTo-DattoApiPathSegment -Value $VolumeName $response = Invoke-DattoApiHttpRequest -Method GET -Path "/v1/bcdr/device/$serial/asset/$volume/screenshot/latest" -Connection $Connection -Accept 'image/png' -AsByteArray Assert-DattoApiImageResponse -Response $response -ImageType Png Write-DattoApiFileAtomically -Path $destination -Bytes $response.Bytes -Force:$Force if ($PassThru) { Get-Item -LiteralPath $destination } } } |