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)
小智 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)
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)
小智 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)
在 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)
将字符串分成多行的另一种方法是在字符串中间放置一个空表达式,然后将其分成多行:
示例字符串:
"stackoverflow stackoverflow stackoverflow stackoverflow stackoverflow"
断线:
"stackoverflow stackoverflow $(
)stackoverflow stack$(
)overflow stackoverflow"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
178886 次 |
| 最近记录: |