你可以通過一個PowerShell命令powershell.exe這樣的:如何將命令傳遞給包含花括號的PowerShell?
PowerShell -Command {Get-EventLog -LogName security}
但是,如果命令包含{或}?如:
dir z:\test -fi "*.tmp" -r | ?{$_.creationtime -le (Get-Date).adddays(-30)} | del
謝謝。
你可以通過一個PowerShell命令powershell.exe這樣的:如何將命令傳遞給包含花括號的PowerShell?
PowerShell -Command {Get-EventLog -LogName security}
但是,如果命令包含{或}?如:
dir z:\test -fi "*.tmp" -r | ?{$_.creationtime -le (Get-Date).adddays(-30)} | del
謝謝。
這樣的:使用調用表達可以是一種選擇
PowerShell.exe -Command "dir z:\test -fi "*.tmp" -r | ?{$_.creationtime -le (Get-Date).adddays(-30)} | del"
PowerShell -Command {Invoke-Expression "dir z:\test -fi `"*.tmp`" -r | ?{`$_.creationtime -le (Get-Date).adddays(-30)} | del"}
另一種可能是編碼的命令:
$command = "dir z:\test -fi '.tmp' -r | ?{$_.creationtime -le (Get-Date).adddays(-30)} | del "
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand
命令參數可以接受腳本塊以及字符串,在您的頂級示例{}中表示腳本塊。所以只需將命令放在「」而不是{}中。
PowerShell.exe -Command "dir z:\test -fi "*.tmp" -r | ?{$_.creationtime -le (Get-Date).adddays(-30)} | del"
要記住的唯一的事情是,如果你將它指定爲類似上面的字符串命令必須是你因爲一切都指定它被解釋爲要運行該命令後,最後一個參數。