你爲什麼以這種方式運行它?當您從內Powershell的powershell.exe
您強制的參數要經過舊的Windows風格CMD.EXE命令行處理。
相反,你只是應該直接調用腳本:
PS C:\scripts> .\PowershellTest.ps1 -message "Error: Test-Error"
Info: Test-Information
Error: Test-Error
這樣,所有的參數都將直接通過通過,而不被轉換爲字符串,其剝離雙引號,然後被重新解析。
如果你得到一個錯誤,因爲運行的腳本是不允許的,那麼答案是改變你的執行策略,不要啓動的powershell.exe
另一個副本:
PS C:\scripts> .\PowershellTest.ps1 -message "Error: Test-Error"
.\PowershellTest.ps1 : File C:\scripts\PowershellTest.ps1 cannot be loaded because running scripts is disabled on this
system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\PowershellTest.ps1 -message "Error: Test-Error"
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
如果您看到這個錯誤,然後啓動一個PowerShell窗口使用「以管理員身份運行」選項,並輸入命令:
Set-ExecutionPolicy RemoteSigned
關閉管理員PowerShell和重新啓動所有正在運行的其他PowerShell的窗口,現在你應該可以運行沒有任何本地創建的腳本 問題。
這就是它! Ty 但爲什麼有必要排除空白?它背後的邏輯是什麼? – 5im
我想像它是與你在PowerShell進程是和啓動另外一個使用這些參數。第一個shell需要轉義所有數據才能正確地將其發送到新的數據庫。正如vonPryz所建議的,如果從普通的cmd命令提示符運行,使用單引號就足夠了。 – arco444