In the previous blog entry, PowerShell: Converting Json-Formatted Strings to/from Json-Objects and Json-Object Depth, it was shown how to correctly create Json-objects from Json-formatted strings and how to create Json-formatted strings from Json-objects by taking into account the depth of the underlying Json-object being manipulated. This post expands on the manipulation of Json-object with PowerShell by demonstrating how to:
- read a Json-formatted string from a file
- convert the Json-formatted string to a Json-object
- modify the Json-object
- covert the Json-object to a Json-formatted string
- save the Json-formatted string to a file
[string] $destinationWindowsVmTemplateFilename =
'TemplateAnyWindowsVM.json'
[string] $content =
Get-Content -Raw -Path $sourceWindowsVmTemplateFilename
[int] $depth = Get-Depth $content
[PSCustomObject] $jsonObject = $content |
if ($jsonObject.resources.properties.
storageProfile.imageReference.sku -ne $windowsSku)
{
$jsonObject.resources.properties.
storageProfile.imageReference.sku = $windowsSku
$jsonObject |
ConvertTo-Json -Depth $depth |
Set-Content $destinationWindowsVmTemplateFilename
}
Get-Content -Raw -Path $sourceWindowsVmTemplateFilename
A culled version of the Json identifies the Windows Sku is as follows:
<#
"resources": [
{
{
"properties": {
"storageProfile": {
"imageReference": {
"sku": "2012-R2-Datacenter",
#>
Based on the pseudo-Json above the sku is accessible as follows:
resources.properties.storageProfile.imageReference.sku
storageProfile.imageReference.sku -ne $windowsSku)
{
$jsonObject.resources.properties.
storageProfile.imageReference.sku = $windowsSku
$jsonObject | ConvertTo-Json -Depth $depth
- 2012-R2-Datacenter
- 2016-Datacenter
- 2019-Datacenter
The Window's SKU returned is simply a string that depends on the version of SharePoint to be installed on the virtual machine (SharePoint 2013, SharePoint 2016 or SharePoint 2019). The function used to return the appropriate Windows SKU is Get-WindowsSku:
function Get-WindowsSku()
{
param (
[ValidateSet(2013, 2016, 2019)]
[Parameter(Mandatory)]
[int] $sharePointVersionYear
)
[string] $sku
switch($sharePointVersionYear){
2013 { $sku = '2012-R2-Datacenter'}
2016 { $sku = '2016-Datacenter'}
2019 { $sku = '2019-Datacenter'}
}
return $sku
}
No comments :
Post a Comment