根据this answer,PowerShell语句可以用分号分隔。
以下是我正在使用的脚本片段(为简化起见,对其进行了一些编辑):
Add-Type -AssemblyName System.Web;
$server = "http://website.com/index.php";
foreach ($path in [System.IO.Directory]::EnumerateFiles("C:\path\to\dir","*.xml","AllDirectories")) {
try {
$oXml = New-Object System.XML.XMLDocument;
$oXml.Load($path);
<more commands here>;
} catch {}
}
以下是我的众多尝试中的一种:
powershell -ExecutionPolicy Bypass -Command "Add-Type -AssemblyName System.Web; $server = 'http://website.com/index.php'; foreach ($path in [System.IO.Directory]::EnumerateFiles("C:\path\to\dir","*.xml","AllDirectories")) { try { $oXml = New-Object System.XML.XMLDocument; $oXml.Load($path); <more commands here>;} catch {}}"
下面是cmd返回的错误:
At line:1 char:140
+ ... hp'; foreach ($path in [System.IO.Directory]::EnumerateFiles(C:\path ...
+ ~
Missing ')' in method call.
At line:1 char:140
+ ... ry]::EnumerateFiles(C:\path\to\dir,*.xml,A ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'C:\path\to\dir' in expression or statement.
At line:1 char:140
+ ... hp'; foreach ($path in [System.IO.Directory]::EnumerateFiles(C:\path ...
+ ~
Missing closing ')' after expression part of foreach loop.
At line:1 char:181
+ ... y]::EnumerateFiles(C:\path\to\dir,*.xml,Al ...
+ ~
Missing argument in parameter list.
At line:1 char:202
+ ... \path\to\dir,*.xml,AllDirectories)) { try ...
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:203
+ ... path\to\dir,*.xml,AllDirectories)) { try { ...
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
如何(正确地)将上面的PowerShell脚本压缩到一行中?
发布于 2019-01-07 08:03:26
妮可你有两个选择。
First类似于您现在使用的技术,但将脚本中的任何双引号替换为单引号。将整个脚本括在双引号中,并用&号和括号括起来。这会给你类似这样的东西。
powershell -ExecutionPolicy Bypass -Command "& {Add-Type -AssemblyName System.Web; $server = 'http://website.com/index.php'; foreach ($path in [System.IO.Directory]::EnumerateFiles('C:\path\to\dir','*.xml','AllDirectories')) { try { $oXml = New-Object System.XML.XMLDocument; $oXml.Load($path); <more commands here>;} catch {}}}"
第二种选择是对脚本进行base64编码,这样就不必担心脚本有多复杂了。然后使用-EncodedCommand选项启动powershell.exe。
为此,您需要首先准备base64编码的脚本,如下所示
$Script = @'
Add-Type -AssemblyName System.Web;
$server = "http://website.com/index.php";
foreach ($path in [System.IO.Directory]::EnumerateFiles("C:\path\to\dir","*.xml","AllDirectories")) {
try {
$oXml = New-Object System.XML.XMLDocument;
$oXml.Load($path);
<more commands here>;
} catch {}
}
'@
$ByteScript = [System.Text.Encoding]::Unicode.GetBytes($Script)
[System.Convert]::ToBase64String($ByteScript)
请注意,您的实际脚本位于多行字符串文字之间,其中@‘和'@用单独的行括起来。该命令的输出是脚本的base64编码字符串。现在,您只需在命令行中使用它
powershell.exe -EncodedCommand "QQBkAGQALQBUAHkAcABlACAALQBBAHMAcwBlAG0AYgBsAHkATgBhAG0AZQAgAFMAeQBzAHQAZQBtAC4AVwBlAGIAOwANAAoADQAKACQAcwBlAHIAdgBlA
HIAIAA9ACAAIgBoAHQAdABwADoALwAvAHcAZQBiAHMAaQB0AGUALgBjAG8AbQAvAGkAbgBkAGUAeAAuAHAAaABwACIAOwANAAoADQAKAGYAbwByAGUAYQ
BjAGgAIAAoACQAcABhAHQAaAAgAGkAbgAgAFsAUwB5AHMAdABlAG0ALgBJAE8ALgBEAGkAcgBlAGMAdABvAHIAeQBdADoAOgBFAG4AdQBtAGUAcgBhAHQ
AZQBGAGkAbABlAHMAKAAiAEMAOgBcAHAAYQB0AGgAXAB0AG8AXABkAGkAcgAiACwAIgAqAC4AeABtAGwAIgAsACIAQQBsAGwARABpAHIAZQBjAHQAbwBy
AGkAZQBzACIAKQApACAAewANAAoADQAKACAAIAB0AHIAeQAgAHsADQAKACAAIAAgACAAJABvAFgAbQBsACAAPQAgAE4AZQB3AC0ATwBiAGoAZQBjAHQAI
ABTAHkAcwB0AGUAbQAuAFgATQBMAC4AWABNAEwARABvAGMAdQBtAGUAbgB0ADsADQAKACAAIAAgACAAJABvAFgAbQBsAC4ATABvAGEAZAAoACQAcABhAH
QAaAApADsADQAKACAAIAAgACAAPABtAG8AcgBlACAAYwBvAG0AbQBhAG4AZABzACAAaABlAHIAZQA+ADsADQAKACAAIAB9ACAAYwBhAHQAYwBoACAAewB
9AA0ACgANAAoAfQA="
这样做的唯一主要缺点是Base64编码的字符串比原始字符串要大。
发布于 2019-01-07 08:27:21
关键是要确保在powershell命令中使用双引号。例如:
Powershell.exe -Command "foobar"
然后,命令中的所有内容都应该使用单引号。例如:
Powershell.exe -Command "Write-Host 'foobar'"
有时,您不得不在单引号中重用双引号,因此可以通过使用转义字符()来解决此问题。例如:
Powershell.exe -Command "Write-Host \"foobar\""
或者使用双引号("")进行转义。例如:
Powershell.exe -Command "Write-Host ""foobar"""
最后,当你的命令变得棘手时,你也可以使用严重的重音转义字符(`)。例如:
Powershell.exe -Command "Write-Host `'foobar`'"
我希望这能帮到你。
发布于 2019-01-07 07:59:08
使用'
而不是"
,或者您需要转义引号:
C:\>powershell -Command "echo "Hello, World!"; echo "Hello, World!""
Hello
World!
Hello
World!
Powershell将其视为echo Hello, World!
,它将每个单词打印在单独的一行上。现在我们转义引号:
C:\>powershell -Command "echo \"Hello, World!\"; echo \"Hello, World!\""
Hello, World!
Hello, World!
Powershell将其视为echo "Hello, World!"
,它可以满足您的需求。
因此,您的代码行变成(为了可读性而拆分):
powershell -ExecutionPolicy Bypass -Command "Add-Type -AssemblyName System.Web;
$server = \"http://website.com/index.php\";
foreach ($path in [System.IO.Directory]::EnumerateFiles(\"C:\path\to\dir\",\"*.xml\",
\"AllDirectories\"))
{ try { $oXml = New-Object System.XML.XMLDocument; $oXml.Load($path); <more commands here>;} catch {}}"
然而,在这种情况下,@Drew的评论可能是更好的方法……
https://stackoverflow.com/questions/54066930
复制相似问题