我遇到了 Powershell 的 Invoke-Webrequest 功能問題。我知道如何使用它,但是某些檔案的檔案名中有一個括號。這是 .ps1 檔案的代碼:
Invoke-Webrequest http://FreeZipFiles.com/FreeZipFile.zip -Outfile C:\Users\MyUserName\Desktop\test.zip
請注意,FreeZipFiles.com 是虛構的。當我運行代碼時,我收到此錯誤:
FREE! : The term 'FREE!' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:76
... http://FreeZipFiles.com/FreeZipFile (FREE!). ...
~~~
CategoryInfo : ObjectNotFound: (FREE!:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
看完之后,我意識到括號表示“這是一個命令,運行它”,但事實并非如此!無論如何,提前致謝!
uj5u.com熱心網友回復:
您的示例代碼顯示了您的 URL,http://FreeZipFiles.com/FreeZipFile.zip但是根據您的示例錯誤訊息,我懷疑您遺漏了重要的部分,即 URL 中有一個空格,例如http://FreeZipFiles.com/FreeZipFile.zip (FREE!).
在這種情況下,您應該確保參考 URL:
Invoke-WebRequest 'http://FreeZipFiles.com/FreeZipFile.zip (FREE!)' -Outfile C:\Users\MyUserName\Desktop\test.zip
第一種方法失敗的原因是用作引數的未加引號的值被解釋為字串,但由于空格將引數和引數分開,您必須注意確保正確處理空格。通常這是通過參考完成的。
您也可以使用雙引號,但雙引號可以擴展某些特殊字符。例如,如果你這樣做:
Invoke-Webrequest "http://FreeZipFiles.com/$HOST/FreeZipFile.zip" -Outfile C:\Users\MyUserName\Desktop\test.zip
您可能會驚訝地發現,它$HOST最終會$Host在 PowerShell中獲取變數的值,而不是按字面意思獲取。單引號不會以這種方式解釋變數。
從技術上講,您可以使用反引號`來轉義空格,這是 PowerShell 中的轉義字符,但是您還必須自己轉義括號:
Invoke-Webrequest http://FreeZipFiles.com/FreeZipFile.zip` `(FREE!`) -Outfile C:\Users\MyUserName\Desktop\test.zip
很明顯,這并不理想,我更多地將其添加為好奇心。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/323346.html
下一篇:Powershell計算機名輸出
