这是我的输入字符串($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"
}
我正在使用ConvertFrom-Json
命令将其转换为对象。
$paramObject = ConvertFrom-Json -InputObject $inputStr
我想要它打印'VM Computer1 is ready'
但它不起作用,它实际上打印整个字符串:
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.
答案 0 :(得分:6)
使用字符串插值时,只要您想访问对象的成员,就需要使用sub-expression $(...)
:
Write-Host "VM $($paramObject.Name) is ready"
否则,PowerShell会将.Name
部分视为普通文本。