Powershell在这里 - 字符串扩展

时间:2014-09-25 18:46:24

标签: powershell powershell-v3.0

这里串

有一些关于Powershell'here-string'的例子,但我很难遇到'here-string'扩展。所以我发布这个有一些帮助。

如果要添加一些带换行符的文字,单引号和双引号都不需要转义,也不需要像"`r`n"这样的换行符。 'here-strings'在PowerShell中得到了拯救。 他们应该从

开始

@“
   和换行符应该以换行符结束   “@

For example:                        |Result:
                                    |
@"                                  |
Hello world! 09/25/2014 11:39:56    |      Hello world! 09/25/2014 11:39:56
'(this will appear as is)'          |      '(this will appear as is)'
!                                   |      !
"@                                  |

2 个答案:

答案 0 :(得分:12)

以下是如何引入CmdLet和日期变量来显示当前日期,如下所示:
例如,以下是我们想要实现的目标:

Hello world! 09/25/2014 11:39:56
'(this will appear as is)'
!

以下是:

@"
Hello world! $(Get-Date)
'(this will appear as is)'
!
"@

或使用变量:

$myDate = Get-Date
@"
Hello world! ${myDate}
'(this will appear as is)'
!
"@

答案 1 :(得分:0)

这篇文章很有帮助,我在这里寻找在SQL查询中扩展PowerShell here-string中的变量。

以下代码片段将获取服务器列表,然后在Foreach-Object中循环,允许为每个查询替换服务器名称!

感谢@Matt代表hash-table help

$servers = Get-Content c:\scripts\list.txt

$servers | ForEach-Object{
 $items = @{}
 $items.Server = $_

$query = @"
 WHERE svrName = $($items.Server)
"@
}