【问题标题】:powershell.exe -ex bypass -commandpowershell.exe -ex 绕过 -command
【发布时间】:2019-01-14 18:23:12
【问题描述】:

我有一个可用的 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-2.0 powershell-3.0


    【解决方案1】:

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

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

    【讨论】:

    • 我实际上检查了帮助,但在这种情况下不清楚如何制作如此大的一行一对一的脚本..
    • @Huntsman 如果您使用-EncodedCommand,则不再需要将原始脚本放在一行中。您不需要转义任何字符。您只需对整个文件进行 Base64 编码,然后编码结果将是一大行。
    猜你喜欢
    • 2022-11-09
    • 2021-12-22
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2023-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多