我正在运行一个命令来将机器添加到域中,一切正常。
我现在想从参数值中取$string1
中的值,有没有办法做到这一点?
$rg = "resourcegroup"
$machines = Get-AzureRmVM -ResourceGroupName $rg
$string1 ='{
"Name" : "domain.local",
"User": "domain.local\\domainadmin",
"Restart" : "true",
"Options" : "3",
}'
$string2 = '{"Password" : "@@@@@@@@@@@@@B"}'
$machines | ForEach { Set-AzureRmVMExtension -ResourceGroupName $rg
-ExtensionType "JSONADDomainExtension" -Name "joindomain"
-Publisher "Microsoft.Compute" -TypeHandlerVersion "1.0" -VMName $_.Name
-Location "uk west" -SettingString $string1 -ProtectedSettingString $String2 }
答案 0 :(得分:1)
构建自定义对象并将其转换为JSON。这样你就可以避免任何和所有转义问题,并且因为输入是实际的PowerShell代码,所以改变对象的任何部分都是微不足道的:
[pscustomobject] @{
Name = 'domain.local'
User = 'domain.local\domainadmin'
Restart = 'true'
Options = '3'
} | ConvertTo-Json
产生以下结果:
{
"Name": "domain.local",
"User": "domain.local\\domainadmin",
"Restart": "true",
"Options": "3"
}
答案 1 :(得分:0)
诀窍似乎是通过加倍来摆脱花括号。
$string1 ='{{
"Name" : "{0}",
"User": "{1}",
"Restart" : "{2}",
"Options" : "{3}"
}}' -f 'domain.local', 'domain.local\\domainadmin', 'true', '3'
答案 2 :(得分:0)
只需连接字符串和参数变量:
param (
[string]$name
)
$string1 ='{
"Name" : "' + $name +'",
"User": "domain.local\\domainadmin",
"Restart" : "true",
"Options" : "3",
}'