【发布时间】:2021-12-05 15:53:55
【问题描述】:
所以我有这段代码可以在生成 xml 后将文件上传到目的地。检查服务器,我发现该文件的内容正确,但从我的一个 catch 块中,我得到“发生无法解决的错误”。我无法判断这是否是严重错误,因为我不知道错误是什么。该文件至少已上传,但我想找出这个错误,以便解决它。有没有办法知道这里抛出了什么错误?
try {
$wc = New-Object System.Net.WebClient
$rawResponse = $wc.UploadFile("someURIhere", "Post", $File)
$resp = System.Text.Encoding.ASCII.GetString($rawResponse)
Write-Host $resp
}
catch [System.Net.WebException] {
$Request = $_.Exception
Write-host "Exception caught: $Request"
$crapMessage = ($_.Exception.Message).ToString().Trim()
Write-Output $crapMessage
}
catch {
Write-Host "An error occurred that could not be resolved."
}
【问题讨论】:
-
您能否在收到错误后运行以下命令并使用输出编辑您的帖子,请
$Error[0].Exception.PSObject.Properties | select Name,Value | FL *。如果文件上传正常,我怀疑由于这一行而发生错误 -$resp = System.Text.Encoding.ASCII.GetString($rawResponse)。
标签: .net powershell error-handling