0

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 !

Joel
  • 669
  • 7
  • 25

1 Answers1

1

This happens because how powershell interpreter reads what you give to it. basically a . isnt considered a part of the powershell variable. its considered a string character. hence it gives you back you variable and adds .Front.VHDDesintationPath to it. try this:

Write-Verbose "First we copy $Source\$TemplateName.vhdx into $($Specs.Front.VHDDesintationPath)\$NewVmName.vhdx" -Verbose
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • It worked! thanks. I tried with just parenthesis, but I missed the extra $ symbol. Thanks! – Joel Nov 13 '18 at 15:31