0

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 {
                param(
                        [Parameter(Mandatory)]
                        #[string]$Path,
                        [string]$Path, 

                        [Parameter(Mandatory)]
                        [string]$DestinationPath
                     )
     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 {
             $PSCmdlet.ThrowTerminatingError($_)
             } 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
     }
 }
Brad
  • 250
  • 1
  • 11
  • AFAIK, [`Compress-Archive`](https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Archive/Compress-Archive?view=powershell-5.0#outputs) 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 08 '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 08 '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**](https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Archive/Compress-Archive?view=powershell-6#outputs). 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 08 '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 08 '19 at 21:24

0 Answers0