PowerShell 函数参数在 try 块内变为空

PowerShell 函数参数在 try 块内变为空

我正在整理一个 powershell zip 传输脚本,该脚本将通过函数调用,但它在创建 zip 文件后一直说路径为空。在 ISE 中运行脚本后,我从 cli 运行:

Move-Stuff c:\logs HASTNAME

它将文件压缩到我提供的第一个参数中,但随后抛出错误:有什么想法吗?据我所知,它似乎可以到达 $beforeHash,但随后失败了

无法将参数绑定到参数“Path”,因为它为空。

以下是完整脚本:

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

相关内容