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

24 浏览
0 Comments

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

admin 更改状态以发布 2023年5月23日
0
0 Comments

另一种更清洁的参数传递方式是使用splatting(展开)

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

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

然后像这样调用您的命令:

Get-WmiObject @params

Microsoft Docs: 了解 Splatting

TechNet Magazine 2011: Windows PowerShell: Splatting

看起来它在 Powershell 2.0 及以上版本中可用

0
0 Comments

回溯撇号字符,即

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

空格很重要。所需格式为空格 ` 回车

0