请考虑以下here-string,我尝试使用模板进行Nagios定义。
$blankDefinition = @"
define host{
use windows-server ; Inherit default values from a template
host_name {0} ; The name we're giving to this host
alias {0} ; A longer name associated with the host
address {1} ; IP address of the host
}
"@
我有一个脚本可以确定哪些服务器不在nagois中。因为它循环我试图让它为我吐出一个定义,以便我可以复制并粘贴到我的Nagios配置中。
$comparison | %{
$blankDefinition -f $($_.serverName),$($_.ipAddress)
}
哪个让我知道以下错误:
Error formatting a string: Input string was not in a correct format..
这样的东西很好用
@"
Testing
One
{0}
Three
"@ -f "Twelve"
然后我发现这是因为其他花括号。有没有一种方法可以使用-f格式化我的字符串,同时保留括号?或者我是否只能使用变量替换?
答案 0 :(得分:20)
添加另一组花括号似乎有效:
$blankDefinition = @"
define host{{
use windows-server ; Inherit default values from a template
host_name {0} ; The name we're giving to this host
alias {0} ; A longer name associated with the host
address {1} ; IP address of the host
}}
"@
$blankDefinition -f 'foo', 'bar', 'foo'
输出:
define host{
use windows-server ; Inherit default values from a template
host_name foo ; The name we're giving to this host
alias foo ; A longer name associated with the host
address bar ; IP address of the host
}