[英]How to pass a parameter with parentheses to Powershell from command line?
我有一個名為MSSQLStartStopServices.ps1
的 Powershell 腳本,用於停止一些 SQL 服務器服務。 正在使用第三方供應商軟件調用此腳本。 有問題的參數是Fully qualified instance name
和Start|Stop
例如
MSSQLStartStopServices.ps1 HOSTNAME\INSTANCENAME Start
對於默認實例,軟件將(本地)作為 INSTANCENAME 傳遞。
MSSQLStartStopServices.ps1 HOSTNAME\(local) Start
但是當我運行它時,我從 Powershell 收到一個錯誤:
術語“本地”不能識別為 cmdlet、function、腳本文件或可運行程序的名稱。 檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確並重試。
當我嘗試將實例名稱放在引號中時,它在 Powershell window 中工作正常:
MSSQLStartStopServices.ps1 "HOSTNAME\(local)" Start
然而,第三方軟件首先從命令行調用 Powershell,如下所示:
powershell .\MSSQLStartStopServices.ps1 "HOSTNAME\(local)" Start
這會導致與引號不存在相同的錯誤。
我該如何解決這個問題?
嘗試將腳本調用括在雙引號中,然后將每個參數括在單引號中:
".\MSSQLStartStopServices.ps1" 'HOSTNAME\(local)' 'Start'
為任何未來有類似問題的人添加此信息:
如果您想使用包含“(”或“|”等字符的變量作為參數值,則需要這樣做: -parameter_name ("'" + $variable + "'")
這對我有用,因為 powershell 在調用非 powershell 方法時會刪除添加到字符串中的任何雙引號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.