VMware Cloud Community
usmabison
VMware Employee
VMware Employee

vcsa error when using powershell to encapsulate the CLI deployment

Greetings all! 

I'm updating an older powershell script I used to deploy VC. In essence, it mounts the VCSA ISO, extracts the embedded_vCSA_on_ESXi.json file, creates a $config variable ($config = (get-content -raw $VCSA_JSON_ORIG) | convertfrom-json),

updates the $config using current environment specific variables, 

then writes it back to json ($VCSA_JSON = $config | convertto-json | set-content –path "$VCSA_JSON")

and finally executes the script (Invoke-Expression “$($DriveLetter):\vcsa-cli-installer\win32\vcsa-deploy.exe install --no-ssl-certificate-verification --accept-eula $VCSA_JSON -v”
)

It throws a (somewhat, IMHO) cryptic error about previous version of the install command and running without a subcommand ...

But ... when I simply grab the embedded_vCSA_on_ESXi.json from the ISO, and manually edit the file, and pass that json to the script, it works as expected (for --precheck-only, --verify-template, as well as the full script execution).

I looked through this group for similar errors and found Solved: vcsa-deploy.exe JSON syntax validator broken on vC... - VMware Technology Network VMTN

My issue, might be slightly different ... I think it is simply when the converts back to json format using convertto-json cmdlet, there is a syntax issue .. and it may be something related to the square brackets, as that is the only difference, I can see between the two json files (the one created using the convertfrom-json and convertto-json cmdlets, which does not work) and the extracted json which does work.

Soooo ... my question then becomes how can i use the convertto-json cmdlet to properly process and pass the square brackets used in the json file? Or ... is there another clever way to pass this info back (w/proper syntax) so I can in the same powershell script extract the json from the iso (to future-proof future version changes of this file), pass in the environment-specific values, and write to json and execute the cli deployment? What follows within the powershell script is the VC configuration.

I've included the two different json files and some of the VCDeployScript ...

Thanks in advance!

And ... I posted this (a week ago) in the vCenter Server Discussions board ... no response, which left me to think that maybe I posted to the wrong board 😞 

0 Kudos
9 Replies
LucD
Leadership
Leadership

Did you use the Depth parameter on the ConvertTo-Json cmdlet?
The default is 2, which might be too low.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
usmabison
VMware Employee
VMware Employee

thank you, Luc, for the quick reply! I added the Depth parameter and set it to 100, to no avail ... The error is a bit cryptic IMO complaining about syntax of the vcsa-deploy.exe ... but when I pass the json formatted with the square brackets (for the DNS, and CEIP entries), the script works as expected. As if the square brackets are being lost in conversion (from or to) json ...

 

0 Kudos
LucD
Leadership
Leadership

With the Depth parameter does the JSON file still miss the square brackets?
Note that there is a Depth parameter on the ConvertFrom-Json and ConvertTo-Json cmdlets.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
usmabison
VMware Employee
VMware Employee

It does. The default on the convert-from is 1024, so I didn't bother to set it. I did notice however that it was introduced in powershell 6.2.  .. still got same error from pwsh core though ... 

0 Kudos
LucD
Leadership
Leadership

Could you try changing that line for the DNS servers to

$config.'new_vcsa'.network.dns_servers = @($DNS1)

That way you for the property to be an array


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
usmabison
VMware Employee
VMware Employee

I did and that seemed to work! But the error persists, unfortunately. The only difference I can see between the json file that works and the one that fails is the new_vcsa section seems to have multiple tabs after converting back to json ... the png files from earlier denote the difference ... I'm just not sure where the tabs are being introduced to the file ...

I just saw (when comparing both json files) that the one that gets created is formatted with Windows-based Carriage Returns (CRLF) and the one that works uses Unix based (LF) ... I am using a windows machine to instantiate the deployment script (not using ps core) ...

so the script $VCSA_JSON = $config | convertto-json | set-content –path "$VCSA_JSON" line is the issue ... Now - to figure out how to write the json properly ... 

 

And giving creedence to the post Solved: Re: vcsa-deploy.exe JSON syntax validator broken o... - VMware Technology Network VMTN ... I am also looking to produce the json without BOM ... 

0 Kudos
LucD
Leadership
Leadership

On the Set-Content cmdlet try adding the -NoNewline switch.
That prohibits the cmdlet from appending a newline after each object.

Also take note of the Encoding parameter, it could be that you need to specify that one for the VCSA install command to accept the file.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
usmabison
VMware Employee
VMware Employee

Thank you, Luc  .. Will do. 

0 Kudos
usmabison
VMware Employee
VMware Employee

As a follow up, a quick addition to script was able to remove the CRs (thanks to another colleague) ... to obtain the desired result. Thanks again to @LucD @jslusher 

https://stackoverflow.com/questions/19127741/replace-crlf-using-powershell

$path = "C:\Users\abc\Desktop\File\abc.txt"
(Get-Content $path -Raw).Replace("`r`n","`n") | Set-Content $path -Force

 

0 Kudos