是否可以将PowerShell命令行拆分为多行?
在Visual Basic中,我可以在下一行中使用下划线(_)来继续命令。
是否可以将PowerShell命令行拆分为多行?
在Visual Basic中,我可以在下一行中使用下划线(_)来继续命令。
当前回答
扩展一下克里斯托巴利托的回答:
我假设您谈论的是命令行——如果是在脚本中,那么新行>充当命令分隔符。 在命令行中,使用分号';'
例如:
在命令行上签署PowerShell脚本。没有换行。
powershell -Command "&{$cert=Get-ChildItem –Path cert:\CurrentUser\my -codeSigningCert ; Set-AuthenticodeSignature -filepath Z:\test.ps1 -Cert $cert}
其他回答
$scriptBlock = [Scriptblock]::Create(@'
echo 'before'
ipconfig /all
echo 'after'
'@)
Invoke-Command -ComputerName AD01 -ScriptBlock $scriptBlock
源 不要使用反引号
如果你想把字符串分成多行,你可以使用“+”。例如:
$header = "Make," +
"ComputerName," +
"Model," +
"Windows Version"
看起来就像:
$header = "Make,ComputerName,Model,Windows Version"
扩展一下克里斯托巴利托的回答:
我假设您谈论的是命令行——如果是在脚本中,那么新行>充当命令分隔符。 在命令行中,使用分号';'
例如:
在命令行上签署PowerShell脚本。没有换行。
powershell -Command "&{$cert=Get-ChildItem –Path cert:\CurrentUser\my -codeSigningCert ; Set-AuthenticodeSignature -filepath Z:\test.ps1 -Cert $cert}
只要在这里加上一个极端情况。这可能会帮你节省5分钟。如果你使用一连串的动作,你需要在行末加上“。”,然后在“'”后面留下一个空格(反勾号)。这是我很不容易才发现的。
$yourString = "HELLO world! POWERSHELL!". `
Replace("HELLO", "Hello"). `
Replace("POWERSHELL", "Powershell")
我开始的时候
if ($true) {
"you can write multiple lines here, and the command doesn't run untill you close the bracket"
"like this"
}
最近我发现我可以
&{
get-date
"more stuff"
}