如何在PowerShell中将多个命令分割为多个命令

asg*_*las 196 powershell

如何在PowerShell中执行如下命令并将其拆分为多行?

&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" -verb:sync -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" -dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"
Run Code Online (Sandbox Code Playgroud)

Col*_*ard 276

尾随反引号字符,即

&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
-verb:sync `
-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" `
-dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx"
Run Code Online (Sandbox Code Playgroud)

  • 倒退前面的空间需要#learn-the-hard-way (41认同)
  • @ josh-graham在后面的勾号之后不应该有任何空格(或内联评论).#了解到,在硬路 (27认同)
  • 这似乎破坏了命令历史记录(向上箭头)功能;因为每一行都显示为一个单独的命令。有没有解决的办法? (2认同)
  • 如果您运行的是powershell 3或更高版本,请参阅github.com/lzybkr/psreadline - 对多行语句修复历史记录遍历. (2认同)

小智 60

更清晰的参数传递的另一种方法是splatting.

将您的参数和值定义为哈希表,如下所示:

$params = @{ 'class' = 'Win32_BIOS';
             'computername'='SERVER-R2';
             'filter'='drivetype=3';
             'credential'='Administrator' }
Run Code Online (Sandbox Code Playgroud)

然后像这样调用你的命令行开关:

Get-WmiObject @params
Run Code Online (Sandbox Code Playgroud)

Windows PowerShell:Splatting.

看起来它适用于Powershell 2.0及更高版本.

  • 太棒了!您可以添加参数是这样的:`$ params.add("名","鲍勃·纽哈特")`https://ramblingcookiemonster.wordpress.com/2014/12/01/powershell-splatting-build-parameters-dynamically/ (5认同)
  • 分号可以,但是是多余的。仅当每行有多个值时才需要。 (2认同)
  • 这不适用于普通 shell 命令,主要适用于 PowerShell commandlet (2认同)

bgm*_*der 37

啊,如果你有一个非常长的字符串,你想要分解,比如说HTML,你可以通过@在外面的两边放一个"- 就像这样:

$mystring = @"
Bob
went
to town
to buy
a fat
pig.
"@
Run Code Online (Sandbox Code Playgroud)

你得到这个:

Bob
went
to town
to buy
a fat
pig.
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Notepad ++,它甚至可以正确地突出显示为字符串块.

现在,如果您希望该字符串也包含双引号,只需添加它们,如下所示:

$myvar = "Site"
$mystring = @"
<a href="http://somewhere.com/somelocation">
Bob's $myvar
</a>
"@
Run Code Online (Sandbox Code Playgroud)

你会得到这个:

<a href="http://somewhere.com/somelocation">
Bob's Site
</a>
Run Code Online (Sandbox Code Playgroud)

但是,如果你在@ -string中使用双引号,那么Notepad ++没有意识到这一点,并且会将语法着色切换为没有引用或引用,具体取决于具体情况.

更好的是:在你插入$变量的任何地方,它都会得到解释!(如果你需要文本中的美元符号,你可以用这样的刻度标记来逃避它:``$ not-a-variable`.)

注意!如果你没有把决赛放在线"@最开头,它就会失败.花了我一个小时的时间才发现我无法在代码中缩进!

以下是关于此主题的MSDN:使用Windows PowerShell"Here-Strings"


Aar*_*sen 18

您可以使用反引号运算符:

& "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
    -verb:sync `
    -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" `
    -dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"
Run Code Online (Sandbox Code Playgroud)

根据我的口味,这仍然有点太长,所以我会使用一些名字很好的变量:

$msdeployPath = "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe"
$verbArg = '-verb:sync'
$sourceArg = '-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web"'
$destArg = '-dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"'

& $msdeployPath $verbArg $sourceArg $destArg
Run Code Online (Sandbox Code Playgroud)

  • 与其他建议相比,我更喜欢变量名称,因为它对于非 powershell 专家来说可能是最易读的选项。如果我看到使用泼溅的教程/设置指南,如果没有关于泼溅的子教程,我将完全迷失在发生的事情中。同样,反引号似乎很脆弱,并且可能不如简单的经过验证的真实 PS 变量那么为人所知。 (2认同)

小智 12

如果你有一个功能:

$function:foo | % Invoke @(
  'bar'
  'directory'
  $true
)
Run Code Online (Sandbox Code Playgroud)

如果您有一个cmdlet:

[PSCustomObject] @{
  Path  = 'bar'
  Type  = 'directory'
  Force = $true
} | New-Item
Run Code Online (Sandbox Code Playgroud)

如果您有申请:

{foo.exe @Args} | % Invoke @(
  'bar'
  'directory'
  $true
)
Run Code Online (Sandbox Code Playgroud)

要么

icm {foo.exe @Args} -Args @(
  'bar'
  'directory'
  $true
)
Run Code Online (Sandbox Code Playgroud)


Bad*_*Bad 7

在 PowerShell 5 和 PowerShell 5 ISE 中,还可以仅使用Shift+Enter进行多行编辑(而不是`每行末尾的标准反引号):

PS> &"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" # Shift+Enter
>>> -verb:sync # Shift+Enter
>>> -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" # Shift+Enter
>>> -dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx"
Run Code Online (Sandbox Code Playgroud)


Kir*_*sov 5

将字符串分成多行的另一种方法是在字符串中间放置一个空表达式,然后将其分成多行:

示例字符串:

"stackoverflow stackoverflow stackoverflow stackoverflow stackoverflow"

断线:

"stackoverflow stackoverflow $(
)stackoverflow stack$(
)overflow stackoverflow"
Run Code Online (Sandbox Code Playgroud)

  • 这太丑了!:) 2023 年仍然没有更好的解决方案吗? (4认同)