I'm putting together a powershell zip in transit script that would be called via a function, but it keeps saying the path is null after it creates the zip file. After running the script in ISE I then run from the cli:

Move-Stuff c:\logs HASTNAME

It zips the files into the first parameter I provide but then throws the error: Any ideas? From what I can tell it looks to get to $beforeHash but then fails

Cannot bind argument to parameter 'Path' because it is null.

Here's the full script:

function Move-Stuff {

     try {
             echo "The path is set to $Path"
             ## Zip up the folder
             echo "Zipping stuff"
             $zipFile = Compress-Archive -Path $Path -DestinationPath "$Path\($(New-Guid).Guid).zip" -CompressionLevel Optimal # -PassThru
             echo "The path is set to $Path"
             echo "$Path\($(New-Guid).Guid).zip"

             ## Create the before hash
             echo "Before Hash"
             echo "The path is set to $Path"
             echo "$zipFile"
             echo "$zipFile.FullName"
             Write-Host "zipfile:" $zipFile
             Write-Host "zipfile full name:" $zipFile.FullName
             echo "$Path\($(New-Guid).Guid).zip"
             $beforeHash = (Get-FileHash -Path $zipFile.FullName).Hash
             echo "$beforeHash"
             echo "After Hash"
             echo "The path is set to $Path"

             ## Transfer to a temp folder
             echo "Starting Transfer"
             $destComputer = $DestinationPath.Split('\')[2]
             $remoteZipFilePath = "\\$destComputer\c$\Temp"

             Start-BitsTransfer -Source $zipFile.FullName -Destination $remoteZipFilePath

             ## Create a PowerShell remoting session
             echo "Starting PSSession"
             $destComputer = $DestinationPath.Split('\')[2]
             $session = New-PSSession -ComputerName $destComputer
             echo "PSSession Created"

             ## Compare the before and after hashes
             ## Assuming we're using the c$ admin share
             $localFolderPath = $DestinationPath.Replace("\\$destComputer\c$\",'C:\')
             $localZipFilePath = $remoteZipFilePath.Replace("\\$destComputer\c$\",'C:\')
             $afterHash = Invoke-Command -Session $session -ScriptBlock { (Get-FileHash -Path "$using:localFolderPath\$localZipFilePath").Hash }
             if ($beforeHash -ne $afterHash) {
             throw 'File modified in transit!'

             ## Decompress the zip file
             Invoke-Command -Session $session -ScriptBlock { Expand-Archive -Path "$using:localFolderPath\$localZipFilePath" -DestinationPath $using:localFolderPath }
             } catch {
             } finally {
             ## Cleanup
             #Invoke-Command -Session $session -ScriptBlock { Remove-Item "$using:localFolderPath\$localZipFilePath" -ErrorAction Ignore }
             #Remove-Item -Path $zipFile.FullName -ErrorAction Ignore
             #Remove-PSSession -Session $session -ErrorAction Ignore
  • AFAIK, Compress-Archive produces no output. I'd use $zipFile = "$Path\($(New-Guid).Guid).zip"; Compress-Archive -Path $Path -DestinationPath $zipFile (and avoid further $(New-Guid).Guid which produces different value on every next call). – JosefZ Aug 8 '19 at 10:26
  • Compress-Archive DOES produce output for me it creates the guid based zip filename which is unique, but even if I replace with a static file name the path becomes null for $beforeHash = (Get-FileHash -Path $zipFile.FullName).Hash – Brad Aug 8 '19 at 14:20
  • Why did you tagged the question as powershell-v5.0 then? Please see my previous link to powershell-v5.0 docs. The Compress-Archive cmdlet only returns a FileInfo object when you use the PassThru parameter since version 6. Moreover, Compress-Archive -Path $Path -DestinationPath "$Path\($(New-Guid).Guid).zip" creates a .zip archive inside source folder (which could lead to some kind of faulty recursion). – JosefZ Aug 8 '19 at 17:10
  • Powershell Version 5.1.14393.3053 is what is on the server that it runs on which is Windows server 2016. – Brad Aug 8 '19 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.