powershell.exe -ex绕过-command

powershell.exe -ex bypass -command

我有一个有效的 powershell 脚本,我需要使用 powershell.exe -ex bypass -command 将整个脚本转换为单行代码中的 运行 ....

后台的原因是脚本不应该 运行 作为 .ps1 文件,我可以 运行 作为单个命令。我正在寻找这方面的帮助...我对 powershell 很陌生..但试图管理下面的脚本..我需要将它从命令转换为 运行 作为单行代码..

    # Config
$logFileName = "Application" # Add Name of the Logfile (System, Application, etc)
$path = "c:\Intel\" # Add Path, needs to end with a backsplash

# do not edit
$exportFileName = $logFileName + (get-date -f yyyyMMdd) + ".evt"
$logFile = Get-WmiObject Win32_NTEventlogFile | Where-Object {$_.logfilename -eq $logFileName}
$logFile.backupeventlog($path + $exportFileName)

如果您不使用文件,则不需要 -ex-ExecutionPolicy 的缩写),因为它仅适用于文件。

为了让它成为一行,你基本上用 ; 替换换行符。

但是 -Command 不是最好的主意。您将不得不小心地正确转义整个代码中的所有引号和管道。

您可以查看 -EncodedCommand,通过 Base64 对您的代码进行编码,并将其作为一个字符串传递。

如果您检查 powershell.exe /? 它在底部有一个示例:

# To use the -EncodedCommand parameter:
$command = 'dir "c:\program files" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand

如果您能详细解释不想使用文件的原因,可能有助于获得更适合您情况的答案。