根据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脚本压缩到一行中?
https://stackoverflow.com/questions/54066930
复制相似问题