powershell.exe -ex旁路-命令

时间:2018-08-07 15:34:09

标签: powershell powershell-v2.0 powershell-v3.0

我有一个有效的powershell脚本,我需要使用 powershell.exe -ex旁路-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)

1 个答案:

答案 0 :(得分: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

如果您可以详细说明不想使用文件的原因,则可能有助于获取更适合您情况的答案。

相关问题