Json 无法使用写入输出打印字符串内的变量值
这是我的输入字符串(Json 无法使用写入输出打印字符串内的变量值,json,string,powershell,Json,String,Powershell,这是我的输入字符串($inputStr)的外观: { "CloudId" : "67f8f457-1c4a-4622-a743-638318af04e3", "ComputerName" : "Computer1", "DeploymentErrorInfo" : { "IsConditionallyTerminating" : null, "MomAlertSeverity" : null, "DisplayableErr
$inputStr
)的外观:
{
"CloudId" : "67f8f457-1c4a-4622-a743-638318af04e3",
"ComputerName" : "Computer1",
"DeploymentErrorInfo" : {
"IsConditionallyTerminating" : null,
"MomAlertSeverity" : null,
"DisplayableErrorCode" : null,
"IsSuccess" : null,
"DetailedCode" : null,
"IsMomAlert" : null,
"DetailedSource" : null,
"CloudProblem" : null,
"IsTerminating" : null,
"DetailedErrorCode" : null,
"ExceptionDetails" : null,
"Code" : null,
"ShowDetailedError" : null,
"RecommendedActionCLI" : null,
"ErrorCodeString" : null,
"IsDeploymentBlocker" : null,
"ErrorType" : null,
"RecommendedAction" : null,
"Problem" : null,
"MessageParameters" : null
},
"GrantedToList" : [],
"ID" : "00000000-0000-0000-0000-000000000000",
"LocalAdminUserName" : "administrator",
"Name" : "Computer1",
"NewVirtualNetworkAdapterInput" : [],
"OperatingSystemInstance" : {
"Architecture" : null,
"Edition" : null,
"Description" : null,
"Version" : null,
"Name" : null,
"OSType" : null,
"ProductType" : null
},
"Owner" : {
"UserName" : null,
"RoleID" : null,
"RoleName" : null
},
"StampId" : "23e6799c-33d4-45ea-8e4f-49ec7d5f26e0",
"StartVM" : true,
"VMNetworkAssignments" : [],
"VMTemplateId" : "fdc73f71-1c6d-4e8f-9d02-21c7f4756c2d"
}
我使用convertfromjson
命令将其转换为对象
$paramObject = ConvertFrom-Json -InputObject $inputStr
我想让它打印“VM Computer1已准备就绪”,但它不工作,它实际上打印整个字符串:
Write-Host "VM $paramObject.Name is ready" # prints the entire thing
Write-Host 'VM $paramObject.Name is ready' # prints command
Write-Host $paramObject.Name # prints VM Name so I know I am able to get the VM Name.
使用字符串插值时,无论何时要访问对象的成员,都需要使用:
Write Host“VM$($paramObject.Name)已准备就绪”
否则,PowerShell会将
.Name
部分视为普通文本。使用字符串插值时,您需要在访问对象成员时使用:
Write Host“VM$($paramObject.Name)已准备就绪”
否则,PowerShell将把
.Name
部分视为普通文本。您可以更仔细地查看第一个写入主机
命令的输出,从而了解基本问题。也就是说,在对象关闭}
之后,您会在输出中直接看到.Name
,这表明成员选择没有正确工作。通过更仔细地查看第一个写入主机
命令的输出,您可能会看到基本问题。也就是说,在对象关闭后直接在输出中看到.Name
,
,表明成员选择没有正确工作。