powershell.exe -ex bypass -command

问题描述 投票:1回答:1

我有一个工作的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)
powershell powershell-v2.0 powershell-v3.0
1个回答
2
投票

如果您不使用文件,则不需要-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

如果您可以解释更多关于不想使用文件的原因,那么获得更适合您情况的答案可能会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.