使用多行命令参数批量启动 PowerShell

发布于 2025-01-07 05:13:19 字数 323 浏览 1 评论 0 原文

我想将批处理文件中的几行代码作为 -command 参数传递给 powershell.exe。

例如,这样的代码:

SET LONG_COMMAND=
if ($true)
{
Write-Host "Result is True"
}
else
{
Write-Host "Result is False"
}

START Powershell -noexit -command "%LONG_COMMAND%"

我想在不创建 PowerShell 脚本文件的情况下执行此操作,只创建一个批处理文件。
是否可以?

谢谢。

I want to pass several lines of code from a batch file to powershell.exe as -command parameter.

For example, code like this:

SET LONG_COMMAND=
if ($true)
{
Write-Host "Result is True"
}
else
{
Write-Host "Result is False"
}

START Powershell -noexit -command "%LONG_COMMAND%"

I would like to do it without creating a PowerShell script file, only a batch file.
Is it possible?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东北女汉子 2025-01-14 05:13:19

您可以添加“^”来继续分配给变量的字符串。这会将命令创建为单行,因此您需要使用“;”语句之间:

@ECHO off
SET LONG_COMMAND= ^
if ($true) ^
{ ^
Write-Host "Result is True"; ^
Write-Host "Multiple statements must be separated by a semicolon." ^
} ^
else ^
{ ^
Write-Host "Result is False" ^
}

START Powershell -noexit -command %LONG_COMMAND%

如果您需要执行的唯一代码是 PowerShell,则可以使用类似以下内容的内容:

;@Findstr -bv ;@F "%~f0" | powershell -command - & goto:eof

if ($true){
    Write-Host "Result is True" -fore green
}
else{
    Write-Host "Result is False" -fore red
}

Start-Sleep 5

将所有不以“;@F”开头的行传输到 PowerShell。

编辑:我能够在单独的窗口中启动 PowerShell 并允许 cmd 退出:

@@ECHO off
@@setlocal EnableDelayedExpansion
@@set LF=^


@@SET command=#
@@FOR /F "tokens=*" %%i in ('Findstr -bv @@ "%~f0"') DO SET command=!command!!LF!%%i
@@START powershell -noexit -command !command! & goto:eof

if ($true){
    Write-Host "Result is True" -fore green
}
else{
    Write-Host "Result is False" -fore red
}

请注意,设置“LF”变量后必须有 2 个空格,因为我们正在为多变的。

You can add ' ^' to continue the string that you are assigning to a variable. This creates the command as a single line, so you need to use ';' between statements:

@ECHO off
SET LONG_COMMAND= ^
if ($true) ^
{ ^
Write-Host "Result is True"; ^
Write-Host "Multiple statements must be separated by a semicolon." ^
} ^
else ^
{ ^
Write-Host "Result is False" ^
}

START Powershell -noexit -command %LONG_COMMAND%

If the only code you need to execute is PowerShell, you can use something like:

;@Findstr -bv ;@F "%~f0" | powershell -command - & goto:eof

if ($true){
    Write-Host "Result is True" -fore green
}
else{
    Write-Host "Result is False" -fore red
}

Start-Sleep 5

which pipes all lines not starting with ";@F" to PowerShell.

Edit: I was able to start PowerShell in a separate window and allow cmd to exit with this:

@@ECHO off
@@setlocal EnableDelayedExpansion
@@set LF=^


@@SET command=#
@@FOR /F "tokens=*" %%i in ('Findstr -bv @@ "%~f0"') DO SET command=!command!!LF!%%i
@@START powershell -noexit -command !command! & goto:eof

if ($true){
    Write-Host "Result is True" -fore green
}
else{
    Write-Host "Result is False" -fore red
}

Note that there must be 2 spaces after setting the 'LF' variable since we are assigning a line feed to the variable.

将军与妓 2025-01-14 05:13:19

使用

start powershell -NoExit -EncodedCommand "aQBmACAAKAAkAHQAcgB1AGUAKQAKAHsACgBXAHIAaQB0AGUALQBIAG8AcwB0ACAAIgBSAGUAcwB1AGwAdAAgAGkAcwAgAFQAcgB1AGUAIgAKAH0ACgBlAGwAcwBlAAoAewAKAFcAcgBpAHQAZQAtAEgAbwBzAHQAIAAiAFIAZQBzAHUAbAB0ACAAaQBzACAARgBhAGwAcwBlACIACgB9AAoA"

来自 powershell /? 的引用

# 使用 -EncodedCommand 参数:
$command = 'dir "c:\程序文件" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [转换]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand

您可以通过简单地提供 Base64 编码的字符串来使用原本需要通过 -EncodedCommand 进行尴尬转义的命令。

Use

start powershell -NoExit -EncodedCommand "aQBmACAAKAAkAHQAcgB1AGUAKQAKAHsACgBXAHIAaQB0AGUALQBIAG8AcwB0ACAAIgBSAGUAcwB1AGwAdAAgAGkAcwAgAFQAcgB1AGUAIgAKAH0ACgBlAGwAcwBlAAoAewAKAFcAcgBpAHQAZQAtAEgAbwBzAHQAIAAiAFIAZQBzAHUAbAB0ACAAaQBzACAARgBhAGwAcwBlACIACgB9AAoA"

Quoting from powershell /?

# 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

You can use a command that would otherwise require awkward escaping via -EncodedCommand by simply supplying a Base64-encoded string.

很酷不放纵 2025-01-14 05:13:19

Joey 的方式是万无一失的,但是您也可以在您的情况下使用简单的多行命令

这里需要空行(就像 Rynant 的 LF 示例中一样)

powershell -command if ($true)^

{^

Write-Host "Result is True"^

}^

else^

{^

Write-Host "Result is False"^

}

这里是 长命令分为多行

The way of Joey is foolproof, but you could also use a simple multiline command in your case

The empty lines are necessary here (like in the LF sample of Rynant)

powershell -command if ($true)^

{^

Write-Host "Result is True"^

}^

else^

{^

Write-Host "Result is False"^

}

Here is an explanation of Long commands split over multiple lines

失去的东西太少 2025-01-14 05:13:19

您可以使用 -Command - 使 ps 从标准输入读取命令。将命令放入文件中,然后调用

powershell -Command - <myCommandFile

You could use -Command - which causes ps to read it's commands from stdin. Put your commands in a file, and invoke

powershell -Command - <myCommandFile
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文