I have the following data structure in a config file:
{
"ProjectName" : "Test",
"Front" : {
"Credentials" : {
"Login" : "Administrator",
"Password" : "1234"
},
"RoleName" : "WebServer",
"TemplateName" : "WS2016",
"VHDSourcePath" : "D:\\VMs\\WS2016\\Virtual Hard Disks",
"VHDDesintationPath" : "D:\\VMs\\new",
"SwitchName" : "JoelSwitch"
}, ...
I use the following script to parse and use this config file:
$Specs = Get-Content -Raw -Path .\Specs.json | ConvertFrom-Json
$NewVmName = $Specs.ProjectName + "_" + "Front"
$TemplateName = $Specs.Front.TemplateName
$Source = $Specs.Front.VHDSourcePath
Write-Verbose "First we copy $Source\$TemplateName.vhdx into
$Specs.Front.VHDDesintationPath\$NewVmName.vhdx" -Verbose
When I access the json structure, it has a weird behavior: on the last command, I use a local variable to capture the Source, and I use the json structure directly for the destination. Here is the output I get :
First we copy D:\VMs\WS2016\Virtual Hard Disks\WS2016.vhdx into @{ProjectName=CSF; Front=;Back=}.Front.VHDDesintationPath\CSF_Front.vhdx
you see that the source is correct compared to the config file, but the second parameter is like an object structure, not the value of destination property.
If I rewrite the script like this, it works:
$Source = $Specs.Front.VHDSourcePath
$Dest = $Specs.Front.VHDDesintationPath
Write-Verbose "First we copy $Source\$TemplateName.vhdx into $Dest\$NewVmName.vhdx" -Verbose
How come? Do i need to systematically capture properties in local variables? Why can't I use the structure directly?
Thanks !