#Requires -Version 5.0 # The following variables need to be set in the RMM platform # [string]$PathToFile # [string]$DeleteAfterUpload # [string]$MessageSubject # [string]$MessageBody # This must run before downloading Tools.ps1, including on Server 2016. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $RmmToolsUrl = 'https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1' $RmmToolsClient = New-Object Net.WebClient Try { $RmmToolsText = $RmmToolsClient.DownloadString($RmmToolsUrl) $RmmToolsScript = [Scriptblock]::Create($RmmToolsText) } Catch { Throw "Unable to download or parse Tools.ps1 from ${RmmToolsUrl}: $($_.Exception.Message)" } Finally { $RmmToolsClient.Dispose() } # Dot-source in this scope so the RMM input variables remain accessible. . $RmmToolsScript If ( !(Test-Path -LiteralPath $PathToFile -PathType Leaf) ) { Write-Error "The specified path does not exist: ${PathToFile}" -ErrorAction Stop } $Delete = ConvertTo-RmmBoolean -Value $DeleteAfterUpload Write-Output "Uploading file: ${PathToFile}" $UploadReceipt = Upload-File -Path $PathToFile ` -Subject "${MessageSubject} [${Env:COMPUTERNAME}]" ` -Message "The file ""$(Split-Path -Path $PathToFile -Leaf)"" has been uploaded on $((Get-Date).ToString('yyyy-MM-dd HH:mm:ss zzz'))`n`n${MessageBody}" ` -ErrorAction Stop If ( [string]::IsNullOrWhiteSpace([string]$UploadReceipt.AttachmentId) -or [string]::IsNullOrWhiteSpace([string]$UploadReceipt.Status) ) { Throw 'The upload did not return a confirmation receipt. The local manifest has been retained.' } Write-Output "File uploaded: ${PathToFile}" If ( $Delete ) { Remove-Item -LiteralPath $PathToFile -Force -ErrorAction Stop Write-Output "Deleted uploaded file: ${PathToFile}" } $UploadReceipt