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
}
}