简体   繁体   English

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

[英]Batch launching PowerShell with a multiline command parameter

I want to pass several lines of code from a batch file to powershell.exe as -command parameter. 我想将几行代码从批处理文件传递给powershell.exe as -command参数。

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. 我想这样做而不创建PowerShell脚本文件,只创建一个批处理文件。
Is it possible? 可能吗?

Thanks. 谢谢。

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: 如果您需要执行的唯一代码是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

which pipes all lines not starting with ";@F" to PowerShell. 它将所有不以“; @F”开头的行传递给PowerShell。

Edit: I was able to start PowerShell in a separate window and allow cmd to exit with this: 编辑:我能够在单独的窗口中启动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
}

Note that there must be 2 spaces after setting the 'LF' variable since we are assigning a line feed to the variable. 请注意,设置'LF'变量后必须有2个空格,因为我们正在为变量分配换行符。

Use 使用

start powershell -NoExit -EncodedCommand "aQBmACAAKAAkAHQAcgB1AGUAKQAKAHsACgBXAHIAaQB0AGUALQBIAG8AcwB0ACAAIgBSAGUAcwB1AGwAdAAgAGkAcwAgAFQAcgB1AGUAIgAKAH0ACgBlAGwAcwBlAAoAewAKAFcAcgBpAHQAZQAtAEgAbwBzAHQAIAAiAFIAZQBzAHUAbAB0ACAAaQBzACAARgBhAGwAcwBlACIACgB9AAoA"

Quoting from powershell /? 引自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. 您可以使用一个命令,否则只需提供Base64编码的字符串,就可以通过-EncodedCommand进行笨拙的转义。

You could use -Command - which causes ps to read it's commands from stdin. 您可以使用-Command -这会导致ps从stdin读取它的命令。 Put your commands in a file, and invoke 将您的命令放在一个文件中,然后调用

powershell -Command - <myCommandFile

The way of Joey is foolproof, but you could also use a simple multiline command in your case Joey的方式是万无一失的,但你也可以在你的情况下使用简单的多行命令

The empty lines are necessary here (like in the LF sample of Rynant) 这里需要空行(就像在Rynant的LF样本中一样)

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 以下是分割多行Long命令的说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM