首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在PowerShell中将长命令拆分到多行

如何在PowerShell中将长命令拆分到多行
EN

Stack Overflow用户
提问于 2010-04-09 22:11:01
回答 4查看 257.5K关注 0票数 271

如何在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"
EN

回答 4

Stack Overflow用户

发布于 2014-06-20 01:53:21

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

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

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

然后像这样调用你的命令集:

Get-WmiObject @params

微软文档:About Splatting

TechNet杂志2011:Windows PowerShell: Splatting

Looks like it works with Powershell 2.0 and up

票数 80
EN

Stack Overflow用户

发布于 2013-08-02 01:44:49

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

& "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
票数 21
EN

Stack Overflow用户

发布于 2014-11-12 07:42:14

如果你有一个函数:

$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
)
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2608144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档