powershell EncodedCommand 只支持base64?貌似是的

介绍如何使用 powershell.exe 命令行接口。 显示命令行参数并描述语法。

长说明

PowerShell[.exe]
    [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo]
    [-NoExit]
    [-Sta]
    [-Mta]
    [-NoProfile]
    [-NonInteractive]
    [-InputFormat {Text | XML}]
    [-OutputFormat {Text | XML}]
    [-WindowStyle <style>]
    [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File - | <filePath> <args>]
    [-ExecutionPolicy <ExecutionPolicy>]
    [-Command - | { <script-block> [-args <arg-array>] }
                | { <string> [<CommandParameters>] } ]

PowerShell[.exe] -Help | -? | /?

参数

-PSConsoleFile <FilePath>

加载指定的 PowerShell 控制台文件。 输入控制台文件的路径和名称。 若要创建控制台文件,请使用 PowerShell 中的 Export-console cmdlet。

-Version <PowerShell Version>

启动 PowerShell 的指定版本。 有效值为 2.0 和 3.0。 必须在系统上安装指定的版本。 如果计算机上安装了 Windows PowerShell 3.0,则“3.0”是默认版本。 否则,“2.0”是默认版本。 有关详细信息,请参阅 安装 PowerShell

启动时隐藏版权横幅。

-NoExit

运行启动命令后不退出。

-Sta

使用单线程单元启动 PowerShell。 在 Windows PowerShell 2.0 中,多线程单元 (MTA) 是默认设置。 在 Windows PowerShell 3.0 中,单线程单元 (STA) 是默认设置。

-Mta

使用多线程单元启动 PowerShell。 此参数是在 PowerShell 3.0 中引入的。 在 PowerShell 2.0 中,多线程单元 (MTA) 是默认设置。 在 PowerShell 3.0 中,单线程单元 (STA) 是默认设置。

-NoProfile

不加载 PowerShell 配置文件。

-NonInteractive

不向用户显示交互式提示。

-InputFormat {文本 |XML}

描述发送到 PowerShell 的数据格式。 有效值为“Text”(文本字符串)或“XML”(序列化 CLIXML 格式)。

-OutputFormat {文本 | XML}

确定 PowerShell 输出内容的格式。 有效值为“Text”(文本字符串)或“XML”(序列化 CLIXML 格式)。

-WindowStyle <Window style>

为会话设置窗口样式。 有效值包括 Normal、Minimized、Maximized 和 Hidden。

-EncodedCommand <Base64EncodedCommand>

接受命令的 base-64 编码字符串版本。 使用此参数将命令提交到需要复杂的引号或大括号的 PowerShell。 必须使用 UTF-16LE 字符编码设置字符串的格式。

-ConfigurationName <string>

指定运行 PowerShell 的配置终结点。 这可以是在本地计算机上注册的任何终结点,包括默认 PowerShell 远程处理终结点或具有特定用户角色功能的自定义终结点。

-File - | <filePath> <args>

如果 File 的值为“-”,则从标准输入读取命令文本。 在没有重定向的标准输入的情况下运行 powershell -File - 会启动常规会话。 这与根本不指定 File 参数相同。

如果 文件 的值是文件路径,脚本将在本地范围 (“dot-sourced”) 中运行,以便脚本创建的函数和变量在当前会话中可用。 输入脚本文件路径和任何参数。 “文件”必须是命令中的最后一个参数。 在 File 参数之后键入的所有值都解释为脚本文件路径和传递给该脚本的参数。

传递给脚本的参数作为文字字符串(在当前 shell 的解释后)传递。 例如,如果处于 cmd.exe 并且想要传递环境变量值,则可以使用 cmd.exe 语法: powershell.exe -File .\test.ps1 -TestParam %windir%

相比之下,在 cmd.exe 中运行powershell.exe -File .\test.ps1 -TestParam $env:windir会导致脚本接收文本字符串$env:windir,因为它对当前 cmd.exe shell 没有特殊意义。 $env:windir环境变量引用的样式 可以在 Command 参数中使用,因为它将被解释为 PowerShell 代码。

同样,如果要从 Batch 脚本 执行相同的命令,则可以使用%~dp0.\``$PSScriptRoot表示当前执行目录: powershell.exe -File %~dp0test.ps1 -TestParam %windir% 如果改用 .\test.ps1了,PowerShell 会引发错误,因为它找不到文本路径 .\test.ps1

当 File 的值是文件路径时,文件必须是 命令中的最后一个参数,因为 文件参数名称 之后键入的任何字符都解释为脚本文件路径,后跟脚本参数。

可以在 File 参数的值中包含脚本参数和值。 例如: -File .\Get-Script.ps1 -Domain Central

通常,将包括或忽略脚本的开关参数。 例如,以下命令使用脚本文件的 All 参数 Get-Script.ps1 : -File .\Get-Script.ps1 -All

在极少数情况下,可能需要为参数提供 布尔 值。 以这种方式运行脚本时,无法为 switch 参数传递显式布尔值。 PowerShell 6 () pwsh.exe 中删除了此限制。

 备注

File 参数不能使用需要参数值数组的参数来支持脚本。 遗憾的是,本机命令如何获取参数值是限制。 调用本机可执行文件 ((例如 powershell 或 pwsh) )时,它不知道对数组执行的操作,因此它作为字符串传递。

-ExecutionPolicy <ExecutionPolicy>

设置当前会话的默认执行策略,并将其保存在环境变量中 $env:PSExecutionPolicyPreference 。 此参数不会更改在注册表中设置的 PowerShell 执行策略。 若要了解 PowerShell 执行策略(包括有效值列表),请参阅 about_Execution_Policies

-Command

执行指定的命令 (和任何参数) ,就像在 PowerShell 命令提示符处键入这些命令一样,然后退出,除非 NoExit 指定了参数。

Command 的值可以是-脚本块或字符串。 如果 命令 的值为 -,则从标准输入读取命令文本。

Command 参数仅接受脚本块以供执行,当它可以将传递给 Command 的值识别为 ScriptBlock 类型时。  当从另一个 PowerShell 主机运行powershell.exe时,才能执行此操作。 ScriptBlock 类型可以包含在现有变量中,从表达式返回,或者由 PowerShell 主机分析为文本脚本块,该块包含在大括号 ({}) 中,然后传递给powershell.exe它。

PowerShell
powershell -Command {Get-WinEvent -LogName security}

在 cmd.exe中,没有脚本块 (或 ScriptBlock 类型) 之类的内容,因此传递给 Command 的值 始终 为字符串。 可以在字符串中编写一个脚本块,但它不会被执行,该脚本块的行为与你在典型 PowerShell 提示符中键入它的行为完全相同,即将脚本块内容输出出来返还给你。

传递给 Command 的字符串仍作为 PowerShell 代码执行,因此在从 cmd.exe中运行时,通常不需要脚本块大括号。 要执行在字符串中定义的内联脚本块,可以使用调用操作符 &

cmd
powershell.exe -Command "& {Get-WinEvent -LogName security}"

如果 Command 的值是字符串, 则 Command 必须是 pwsh 的最后一个参数,因为它后面的所有参数都解释为要执行的命令的一部分。

从现有 PowerShell 会话中调用时,结果将作为反序列化的 XML 对象而不是实时对象返回到父 shell。 对于其他 shell,结果将作为字符串返回。

如果 命令 的值为 -,则从标准输入读取命令文本。 在将 Command 参数与标准输入配合使用时,必须重定向标准输入。 例如:

PowerShell
@'
"in"

"hi" |
  % { "$_ there" }

"out"
'@ | powershell -NoProfile -Command -

该示例产生下面的输出:

Output
in
hi there
out

进程退出代码由脚本块中执行的最后一个 () 命令的状态确定。 退出代码是0何时$?``$true1何时$?``$false退出。 如果最后一个命令是外部程序或 PowerShell 脚本,该脚本显式设置退出代码以外的 0 退出代码, 1则退出代码将 1 转换为进程退出代码。 若要保留特定的退出代码,请添加到 exit $LASTEXITCODE 命令字符串或脚本块。

同样,当脚本终止 (runspace 终止) 错误(如 throw 或) -ErrorAction Stop发生或当 使用 Ctrl-C 中断执行时,将返回值 1。

-Help、-?、/?

显示 有关PowerShell.exe 的帮助。 如果要在 PowerShell 会话中键入 PowerShell.exe 命令,请使用连字符 (-) 前附加命令参数,而不是正斜杠 (/) 。 可以在 cmd.exe 中使用连字符或正斜杠。

REMARKS

故障排除说明:在 PowerShell 2.0 中,从 PowerShell 控制台启动某些程序会失败,并且 LastExitCode 为 0xc0000142。

EXAMPLES

PowerShell
# Create a new PowerShell session and load a saved console file
PowerShell -PSConsoleFile sqlsnapin.psc1

# Create a new PowerShell V2 session with text input, XML output, and no logo
PowerShell -Version 2.0 -NoLogo -InputFormat text -OutputFormat XML

# Execute a PowerShell Command in a session
PowerShell -Command "Get-EventLog -LogName security"

# Run a script block in a session
PowerShell -Command {Get-EventLog -LogName security}

# An alternate way to run a command in a new session
PowerShell -Command "& {Get-EventLog -LogName security}"

# 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
posted @ 2022-06-21 11:15  bonelee  阅读(418)  评论(0编辑  收藏  举报