Назад | Перейти на главную страницу

Параметр функции PowerShell становится нулевым внутри блока try

Я собираю сценарий PowerShell zip в пути, который будет вызываться через функцию, но он продолжает говорить, что путь равен нулю после создания zip-файла. После запуска сценария в ISE я запускаю из cli:

Move-Stuff c:\logs HASTNAME

Он заархивирует файлы в первый предоставленный мной параметр, но затем выдает ошибку: Любые идеи? Из того, что я могу сказать, похоже, что до $ beforeHash, но потом не получается

Невозможно привязать аргумент к параметру «Путь», поскольку он имеет значение NULL.

Вот полный сценарий:

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