代码之家  ›  专栏  ›  技术社区  ›  asgerhallas

如何在PowerShell中跨多行拆分长命令

  •  163
  • asgerhallas  · 技术社区  · 16 年前

    如何在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"
    
    6 回复  |  直到 8 年前
        1
  •  231
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    尾随反勾字符,即,

    &"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"
    
        2
  •  48
  •   Oliver    12 年前

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

    将参数和值定义为如下哈希表:

    $params = @{ 'class' = 'Win32_BIOS';
                 'computername'='SERVER-R2';
                 'filter'='drivetype=3';
                 'credential'='Administrator' }
    

    然后这样叫你的指挥官:

    Get-WmiObject @params
    

    Windows PowerShell: Splatting .

    Looks like it works with Powershell 2.0 and up.

        3
  •  35
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    啊,如果你有一个很长的字符串想要分解,比如说HTML,你可以通过 @ 在外部的每一侧 " -像这样:

    $mystring = @"
    Bob
    went
    to town
    to buy
    a fat
    pig.
    "@
    

    你得到的正是:

    Bob
    went
    to town
    to buy
    a fat
    pig.
    

    如果你在使用 Notepad++ ,它甚至可以作为字符串块正确突出显示。

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

    $myvar = "Site"
    $mystring = @"
    <a href="http://somewhere.com/somelocation">
    Bob's $myvar
    </a>
    "@
    

    你会得到确切的信息:

    <a href="http://somewhere.com/somelocation">
    Bob's Site
    </a>
    

    但是,如果您在那样的@-字符串中使用双引号,则记事本++不会意识到这一点,并且会根据具体情况,像没有引用或引用一样关闭语法颜色。

    更好的是:无论您在何处插入$变量,它都会得到解释!(如果您需要在文本中使用美元符号,您可以使用这样的勾号将其转义为:`$not-a-variable`。)

    注意!如果你不投决赛 "@ 在 最开始的时候 ,它将失败。我花了一个小时才发现我不能在我的代码中缩进它!

    这是 MSDN 关于主题: Using Windows PowerShell “Here-Strings”

        4
  •  14
  •   Aaron Jensen    13 年前

    您可以使用倒勾运算符:

    & "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"
    

    这对我来说还是有点太长了,所以我会使用一些有名的变量:

    $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
    
        5
  •  9
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    如果您有一个函数:

    $function:foo | % Invoke @(
      'bar'
      'directory'
      $true
    )
    

    如果你有 cmdlet :

    [PSCustomObject] @{
      Path  = 'bar'
      Type  = 'directory'
      Force = $true
    } | New-Item
    

    如果您有申请:

    {foo.exe @Args} | % Invoke @(
      'bar'
      'directory'
      $true
    )
    

    或

    icm {foo.exe @Args} -Args @(
      'bar'
      'directory'
      $true
    )
    
        6
  •  0
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    在PowerShell5和PowerShell5 ISE中,也可以仅使用 换档 + 进入 用于多行编辑(而不是标准的倒勾号 ` 每行末尾):

    PS> &"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"
    
    推荐文章