Consider the following here-string that I am trying to use a template for my Nagios definitions.
$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
}
"@
I have a script that determines which of my servers are not in nagois that should be. As it loops I'm trying to have it spit out a definion for me so that I can copy and paste into my Nagios config.
$comparison | %{
$blankDefinition -f $($_.serverName),$($_.ipAddress)
}
Which nets me the following error:
Error formatting a string: Input string was not in a correct format..
Something like this works just fine
@"
Testing
One
{0}
Three
"@ -f "Twelve"
So then I found out that it is because of the other curly braces. Is there a way that i can format my string using the -f while keeping the braces? Or do i have to use variable subsitution only?
In languages like C curly braces ( {} ) are used to create program blocks used in flow control. In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks.
In a Java program, everything is subordinate to the top line — the line with class in it. To indicate that everything else in the code is subordinate to this class line, you use curly braces. Everything else in the code goes inside these curly braces.
If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }} . So if you want to print "{42}", you'd use "{{{0}}}".
The clauses in an SQL statement that display between [brackets] are optional. If an optional clause has several components or keywords, they display within the brackets. Curly braces {} in SQL statements indicate that one or more clauses are used together.
Adding another set of curly braces seems to work:
$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'
Output:
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With