I have a powershell script like that:
Param(
[Parameter(Mandatory=$True)]
[String]
$NEWPARAMVALUE,
[Parameter(Mandatory=$True)]
[String]
$PARAM_NAME
)
$FILEPATH = “arm_template.json”
$JSON = Get-Content $FILEPATH | Out-String | ConvertFrom-Json
$JSON.$PARAM_NAME = $NEWPARAMVALUE
$JSON | ConvertTo-Json -Depth 10 | Set-Content $FILEPATH
When I used this script without $PARAM_NAME variable (I just put a parameter as a plain text, example: $JSON.resources.properties.parameters.daasServiceBaseUrl.defaultValue) - it worked.
I tried different ways:
$JSON.resources.$PARAM_NAME (where $PARAM_NAME = 'properties.parameters.daasServiceBaseUrl.defaultValue')
$JSON"."$PARAM_NAME"
I think that's because of the point between two env variables. Could please someone help with it?
Update:
Error that I get from Azure DevOps:
The property
| 'properties.parameters.parametername.defaultValue' cannot
| be found on this object. Verify that the property exists and
| can be set.
How I tried it manually:
$env:PARAM_NAME="resources.properties.parameters.daasServiceBaseUrl.defaultValue"
./test.ps1 -NEWPARAMVALUE "[parameters('DaaS_Contract_Daily_Trigger_properties_Daas-UI-to-Contract_parameters_daasServiceBaseUrl')]" -PARAM_NAME $env:PARAM_NAME
What I got:
Line |
12 | $JSON.$PARAM_NAME = $NEWPARAMVALUE
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception setting "resources.properties.parameters.daasServiceBaseUrl.defaultValue": "The property
| 'resources.properties.parameters.daasServiceBaseUrl.defaultValue' cannot be found on this object. Verify that the property exists and can be
| set."
My JSON:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"dataFactoryName": {
"type": "string",
"defaultValue": ""
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Data Factory location matches Resource group location"
}
},
"project": {
"type": "string",
"defaultValue": "ct",
"metadata": {
"description": "Azure Tag used to track projects"
}
},
"environment": {
"type": "string",
"defaultValue": "default",
"metadata": {
"description": "Azure Tag used to track environments"
}
},
"sleepTime": {
"type": "int",
"defaultValue": "1"
},
"DaaS_Contract_Daily_Trigger_properties_Daas-UI-to-Contract_parameters_daasServiceBaseUrl": {
"type": "string",
"defaultValue": "http://digital-daas-service-om-sqa.nplabsusk8s.com/daas"
}
},
"resources": [
{
"apiVersion": "2018-06-01",
"name": "[parameters('dataFactoryName')]",
"location": "[parameters('location')]",
"tags": {
"project": "[parameters('project')]",
"environment": "[parameters('environment')]"
},
"type": "Microsoft.DataFactory/factories",
"identity": {
"type": "SystemAssigned"
},
"properties": {}
},
{
"name": "[concat(parameters('dataFactoryName'), '/pipeline1')]",
"type": "Microsoft.DataFactory/factories/pipelines",
"apiVersion": "2018-06-01",
"properties": {
"activities": [
{
"name": "Wait1",
"type": "Wait",
"dependsOn": [],
"userProperties": [],
"typeProperties": {
"waitTimeInSeconds": "[parameters('sleepTime')]"
}
},
{
"name": "Copy Delivery Request",
"type": "Copy",
"dependsOn": [
{
"activity": "Delete old Req File",
"dependencyConditions": [
"Succeeded"
]
}
],
"policy": {
"timeout": "7.00:00:00",
"retry": 0,
"retryIntervalInSeconds": 30,
"secureOutput": false,
"secureInput": false
},
"userProperties": [],
"typeProperties": {
"source": {
"type": "RestSource",
"httpRequestTimeout": "00:01:40",
"requestInterval": "00.00:00:00.010",
"requestMethod": "GET"
},
"sink": {
"type": "JsonSink",
"storeSettings": {
"type": "AzureBlobFSWriteSettings"
},
"formatSettings": {
"type": "JsonWriteSettings",
"quoteAllText": true
}
},
"enableStaging": false
},
"inputs": [
{
"referenceName": "Rest_Json_File",
"type": "DatasetReference",
"parameters": {
"relativeURL": {
"value": "#{pipeline().parameters.daasServiceRelURL}/#{variables('createdDate')}",
"type": "Expression"
},
"BaseUrl": {
"value": "#pipeline().parameters.daasServiceBaseUrl",
"type": "Expression"
}
}
}
]
}
],
"parameters": {
"deliveryReqFileName": {
"type": "string",
"defaultValue": "delivery_request.json"
},
"daasServiceBaseUrl": {
"type": "string",
"defaultValue": "http://digital-daas-service-om-sqa.nplabsusk8s.com/daas"
}
},
"annotations": []
},
"dependsOn": []
}
]
}
When you pass a string "a.b.c.d" as the right-hand operand of the . member access operator, PowerShell will attempt to resolve the exact member name a.b.c.d, not "a, then b, then c, then d".
While this could be solved by executing Invoke-Expression against "${JSON}.${PARAM_NAME}", that also opens your script up to arbitrary execution of whatever the user might pass in as $PARAM_NAME.
To avoid that, you'll need to manually:
Split $PARAM_NAME into individual member names, like $names = $PARAM_NAME.Split('.'), then
Resolve each property name except for the last one manually
Assign to $resolvedObject."$last"
Here's how that might look in a generic helper function that takes care of step 2 and 3:
function Set-PropertyChained
{
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[ValidateScript({$_ -isnot [ValueType]})]
[object]$RootObject,
[Parameter(Mandatory = $true, Position = 1)]
[string[]]$MemberChain,
[Parameter(Mandatory = $true, Position = 2)]
[object]$Value,
[switch]$PassThru
)
begin {
$resolutionChain = #($MemberChain |Select -SkipLast 1)
$terminalMember = $($MemberChain |Select -Last 1)
}
process {
$obj = $RootObject
# Member resolution up until the parent of the last property
foreach($memberName in $resolutionChain){
Write-Vebose "Resolving $memberName on $obj"
$obj = $obj.$memberName
}
# Set the value of the last property on its parent
Write-Vebose "Setting $memberName on $obj to $Value"
$obj.$terminalMember = $Value
if($PassThru){
# Write modified object back if requested
Write-Output $RootObject -NoEnumerate
}
}
}
Then in your script:
Param(
[Parameter(Mandatory=$True)]
[String]
$NEWPARAMVALUE,
[Parameter(Mandatory=$True)]
[String]
$PARAM_NAME
)
$FILEPATH = “arm_template.json”
$JSON = Get-Content $FILEPATH | Out-String | ConvertFrom-Json
# Step 1, split the resolution chain into individual components
$MemberNames = $PARAM_NAME.Split('.')
# Step 2 + 3, resolve n-2 members in chain, then assign to n-1
$JSON | Set-PropertyChained -MemberChain $MemberNames -Value $NEWPARAMVALUE
$JSON | ConvertTo-Json -Depth 10 | Set-Content $FILEPATH
Related
I have JSON including multiple nested records. I want to add records with comma separated and store it in a CSV file.
JSON Body
{
"projectVitals": {
"productName": "Enterprise",
"name": "WhiteSource Bolt",
"token": "61a48eab05356f149828c0e",
"creationDate": "2022-10-17 09:08:46",
"lastUpdatedDate": "2023-01-25 06:37:32"
},
"libraries": [
{
"keyUuid": "a89b-40759d783dc3",
"keyId": 145110423,
"type": "NUGET_PACKAGE_MODULE",
"languages": "Nuget",
"references": {
"url": "https://api.nuget.org/packages/system.text.encodings.web.5.0.1.nupkg",
"homePage": "https://github.com/dotnet/runtime",
"genericPackageIndex": "https://api.nuget.org/packages/System.Text.Encodings.Web/5.0.1"
},
"matchType": "SHA1",
"sha1": "05cd84c678cddd1de0c",
"name": "system.text.encodings.web.5.0.1.nupkg",
"artifactId": "system.text.encodings.web.5.0.1.nupkg",
"version": "5.0.1",
"groupId": "System.Text.Encodings.Web",
"licenses": [
{
"name": "MIT",
"url": "http://www.opensource.org/licenses/MIT",
"profileInfo": {
"copyrightRiskScore": "THREE",
"patentRiskScore": "ONE",
"copyleft": "NO",
"royaltyFree": "YES"
},
"references": [
{
"referenceType": "NuGet package (details available in nuget gallery)",
"reference": "https://index.whitesourcesoftware.com/gri/app/reader/resource/content/asString/33131621-c9e5-4c87-ac1d-b988bbef1e0a"
}
]
}
],
"vulnerabilities": []
},
{
"keyUuid": "936f-5daddbcc37b2",
"keyId": 69037902,
"type": "DOT_NET_AS_GENERIC_RESOURCE",
"languages": ".NET",
"references": {
"url": "https://api.nuget.org/packages/system.runtime.interopservices.runtimeinformation.4.3.0.nupkg",
"genericPackageIndex": ""
},
"matchType": "SHA1",
"sha1": "32d3122a48aa379904",
"name": "System.Runtime.InteropServices.RuntimeInformation-4.6.24705.01.dll",
"artifactId": "System.Runtime.InteropServices.RuntimeInformation-4.6.24705.01.dll",
"version": "4.6.24705.01",
"groupId": "System.Runtime.InteropServices.RuntimeInformation",
"licenses": [
{
"name": "Microsoft .NET Library",
"url": "http://microsoft.com/web/webpi/eula/aspnetcomponent_rtw_enu.htm",
"riskLevel": "unknown",
"references": [
{
"referenceType": "Details available in GitHub repository",
"reference": "https://dot.net/"
},
{
"referenceType": "Details available in GitHub repository",
"reference": "https://dotnet.microsoft.com/"
}
]
},
{
"name": "MIT",
"url": "http://www.opensource.org/licenses/MIT",
"profileInfo": {
"copyrightRiskScore": "THREE",
"patentRiskScore": "ONE",
"copyleft": "NO",
"royaltyFree": "YES"
},
"references": [
{
"referenceType": "Details available in GitHub repository",
"reference": "https://dot.net/"
}
]
}
],
"vulnerabilities": []
}
]
}
Powershell Script
$pathToInputJsonFile = "C:\Users\abc\Downloads\test.json"
$pathToOutputCSVFile = "C:\Users\abc\Downloads\License3.csv"
$jsonFileContent = Get-Content -Raw -Path $pathToInputJsonFile | Out-String | ConvertFrom-Json
$libraries = $jsonFileContent.libraries
foreach($obj in $libraries)
{
$LibraryName = $obj.name
$LibraryVersion = $obj.version
$LibraryType = $obj.type
$LibraryLanguage = $obj.languages
$LibraryURL = $obj.references.url
$LicenseName = $obj.licenses.name
$LicenseURL = $obj.licenses.url
[PSCustomObject]#{
LibraryName = $LibraryName
LibraryVersion = $LibraryVersion
LibraryType = $LibraryType
LibraryLanguage = $LibraryLanguage
LibraryURL = $LibraryURL
LicenseName = $LicenseName
LicenseURL = $LicenseURL
} | Export-Csv $pathToOutputCSVFile -notype -Append
}
Actual Result
Expected Result
Use the -join operator to join 1 or more strings together with a given separator:
[PSCustomObject]#{
# ...
LicenseName = $LicenseName -join ', '
LicenseURL = $LicenseURL -join ', '
}
I'm trying to deploy multiple virtual machines one at a time with Powershell and a template file but I keep getting this error:
New-AzResourceGroupDeployment : 9:27:40 AM - Error: Code=InvalidTemplate; Message=Deployment template validation
failed: 'Template parameter JToken type is not valid. Expected 'String, Uri'. Actual 'Object'. Please see
https://aka.ms/resource-manager-parameter-files for usage details.'.
At C:\Users\jackk\OneDrive\Desktop\AzureAutomation\main.ps1:193 char:9
+ New-AzResourceGroupDeployment #parameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzResourceGroupDeployment], Exception
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDep
loymentCmdlet
New-AzResourceGroupDeployment : The deployment validation failed
At C:\Users\jackk\OneDrive\Desktop\AzureAutomation\main.ps1:193 char:9
+ New-AzResourceGroupDeployment #parameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzResourceGroupDeployment], InvalidOperationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDep
loymentCmdlet
Here is the relevant part of my powershell script:
$location = "eastus2"
$networkInterfaceName = ""
$networkSecurityGroupName = ""
#$networkSecurityGroupRules = #()
$subnetName = $SubnetSelection
$virtualNetworkId = "/subscriptions/1234-1234-123-123-123/resourceGroups/MainGroup/providers/Microsoft.Network/virtualNetworks/Main_Network"
$virtualMachineName = ""
$virtualMachineComputerName = ""
$virtualMachineRG = $resourceGroupSelection
$osDiskType = "StandardSSD_LRS"
$virtualMachineSize = "Standard_B2s"
$adminUsername = "ADMIN"
$adminPassword = ConvertTo-SecureString "Password1234" -AsPlainText -Force
$diagnosticsStorageAccountName = $StorageAccountName
$diagnosticsStorageAccountId = $StorageAccountID
$imageName = $ImageSelection
foreach ($i in 1..5) {
$virtualMachineName = "test-$i".ToString()
$virtualMachineComputerName = "test-$i".ToString()
$rand = Get-Random -Maximum 1000
$networkInterfaceName = "test-$i$rand".ToString()
$networkSecurityGroupName = "test-$i-nsg".ToString()
$paramObject = #{
'location' = $location
'networkInterfaceName' = $networkInterfaceName
'networkSecurityGroupName' = $networkSecurityGroupName
'subnetName' = $SubnetSelection
'virtualNetworkId' = $virtualNetworkId
'virtualMachineName' = $virtualMachineName
'virtualMachineComputerName' = $virtualMachineComputerName
'virtualMachineRG' = $resourceGroupSelection
'osDiskType' = $osDiskType
'virtualMachineSize' = $virtualMachineSize
'adminUsername' = $adminUsername
'adminPassword' = $adminPassword
'diagnosticsStorageAccountName' = $diagnosticsStorageAccountName
'diagnosticsStorageAccountId' = $diagnosticsStorageAccountId
'imageName' = $imageName
}
$parameters = #{
'ResourceGroupName' = $resourceGroupSelection
'TemplateFile' = $PathTemplate
'TemplateParameterObject' = $paramObject
'Verbose' = $True
}
New-AzResourceGroupDeployment #parameters
}
Assume any variables not defined in this snippet are strings defined elsewhere with valid information.
The Template file I am using:
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "String"
},
"networkInterfaceName": {
"type": "String"
},
"networkSecurityGroupName": {
"type": "String"
},
"networkSecurityGroupRules": {
"type": "Array",
"defaultValue": []
},
"subnetName": {
"type": "String"
},
"virtualNetworkId": {
"type": "String"
},
"virtualMachineName": {
"type": "String"
},
"virtualMachineComputerName": {
"type": "String"
},
"virtualMachineRG": {
"type": "String"
},
"osDiskType": {
"type": "String"
},
"virtualMachineSize": {
"type": "String"
},
"adminUsername": {
"type": "String"
},
"adminPassword": {
"type": "SecureString"
},
"diagnosticsStorageAccountName": {
"type": "String"
},
"diagnosticsStorageAccountId": {
"type": "String"
},
"imageName": {
"type": "String"
}
},
"variables": {
"nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]",
"vnetId": "[parameters('virtualNetworkId')]",
"subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
},
"resources": [
{
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2019-07-01",
"name": "[parameters('networkInterfaceName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"subnet": {
"id": "[variables('subnetRef')]"
},
"privateIPAllocationMethod": "Dynamic"
}
}
],
"networkSecurityGroup": {
"id": "[variables('nsgId')]"
}
}
},
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2019-02-01",
"name": "[parameters('networkSecurityGroupName')]",
"location": "[parameters('location')]",
"properties": {
"securityRules": "[parameters('networkSecurityGroupRules')]"
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2019-07-01",
"name": "[parameters('virtualMachineName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
],
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('virtualMachineSize')]"
},
"storageProfile": {
"osDisk": {
"createOption": "fromImage",
"managedDisk": {
"storageAccountType": "[parameters('osDiskType')]"
}
},
"imageReference": {
"id": "[parameters('imageName')]"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
}
]
},
"osProfile": {
"computerName": "[parameters('virtualMachineComputerName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]",
"windowsConfiguration": {
"enableAutomaticUpdates": true,
"provisionVmAgent": true
}
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true,
"storageUri": "[concat('https://', parameters('diagnosticsStorageAccountName'), '.blob.core.windows.net/')]"
}
}
}
}
],
"outputs": {
"adminUsername": {
"type": "String",
"value": "[parameters('adminUsername')]"
}
}
}
I have tried everything I could think of and I think I've narrowed the issue down to networkSecurityGroupRules. I took this template file from a valid deployment in Azure and the parameter file that came with networkSecurityGroupRules had the value of the that set to []. So I created a default value for it but it does not work. I've also tried declaring it as a empty array in powershell and passing it through with the other paramObjects but I get the same error. I've tried ArrayList as well to no avail. I have also filled in a parameter.json file with the exact values I pass to it in $paramObjects and it works flawlessly. If I change the defaultValue to null, then on deployment it will ask me to fill in networkSecurityGroupRules[0], networkSecurityGroupRules[1], etc.. I really don't know where the issue is so any help would be really appreciated, thanks.
Found the issue. Everything in the 'paramObject' must be in quotes. For example, 'location' = $location must be 'location' = "$location"
I am creating an arm template to deploy data sets in ADF, for that i need to update an existing json file with new key value pairs based on my input file. how do i add new key value pairs to json file using powershell.
Any help on this is really appreciated..
If am using "Add-Member" it is updating with new "key" and "value" for all properties in the structure like below.but i want new key and value to be added after another value pair like i have shown in the far below code highlighted with "Need to add this"
{
"name": "VIN",
"type": "String"
"newkey1" : "newvalue1"
"newkey2" : "newvalue2"
},
{
"name": "MAKE",
"type": "String"
"newkey1" : "newvalue1"
"newkey2" : "newvalue2"
},
My Code should look some thing like this.. "Need to add this" is the key value pairs i am intending to add in a for each loop as long as i have inputs from another text file.
{
"name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
"type": "Microsoft.DataFactory/factories/datasets",
"apiVersion": "2018-06-01",
"properties": {
"linkedServiceName": {
"referenceName": "AzureDataLakeStore1",
"type": "LinkedServiceReference"
},
"annotations": [],
"type": "AzureDataLakeStoreFile",
"structure": [
{
"name": "VIN",
"type": "String"
},
{
"name": "MAKE",
"type": "String"
},
{
"Need to add this": "Need to add this",
"Need to add this": "Need to add this"
},
{
"Need to add this": "Need to add this",
"Need to add this": "Need to add this"
},
{
"Need to add this": "Need to add this",
"Need to add this": "Need to add this"
},
{
"Need to add this": "Need to add this",
"Need to add this": "Need to add this"
}
],
"typeProperties": {
"format": {
"type": "TextFormat",
"columnDelimiter": "|",
"rowDelimiter": "\n",
"quoteChar": "\"",
"nullValue": "\"\"",
"encodingName": null,
"treatEmptyAsNull": true,
"skipLineCount": 0,
"firstRowAsHeader": false
},
"fileName": "[parameters('Veh_Obj_properties_typeProperties_fileName')]",
"folderPath": "[parameters('Veh_Obj_properties_typeProperties_folderPath')]"
}
},
"dependsOn": [
"[concat(variables('factoryId'), '/linkedServices/AzureDataLakeStore1')]"
]
},
You don't need Add-Member, you simply need to "append" to the existing array in .properties.structure (technically, you're creating a new array that includes the new elements).
Here's a simplified example:
# Sample JSON.
$json = #'
{
"name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
"properties": {
"type": "AzureDataLakeStoreFile",
"structure": [
{
"name": "VIN",
"type": "String"
},
{
"name": "MAKE",
"type": "String"
}
],
}
}
'#
# Convert from JSON to a nested custom object.
$obj = $json | ConvertFrom-Json
# Append new objects to the array.
$obj.properties.structure += [pscustomobject] #{ name = 'newname1' },
[pscustomobject] #{ name = 'newname2' }
# Convert back to JSON.
$obj | ConvertTo-Json -Depth 3
The above yields:
{
"name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
"properties": {
"type": "AzureDataLakeStoreFile",
"structure": [
{
"name": "VIN",
"type": "String"
},
{
"name": "MAKE",
"type": "String"
},
{
"name": "newname1"
},
{
"name": "newname2"
}
]
}
}
$CompanyJSON = #"
{
"name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
"type": "Microsoft.DataFactory/factories/datasets",
"apiVersion": "2018-06-01",
"properties": {
"linkedServiceName": {
"referenceName": "AzureDataLakeStore1",
"type": "LinkedServiceReference"
},
"annotations": [],
"type": "AzureDataLakeStoreFile",
"structure": [
{
"name": "VIN",
"type": "String"
},
{
"name": "MAKE",
"type": "String"
}
],
"typeProperties": {
"format": {
"type": "TextFormat",
"columnDelimiter": "|",
"rowDelimiter": "\n",
"quoteChar": "\"",
"nullValue": "\"\"",
"encodingName": null,
"treatEmptyAsNull": true,
"skipLineCount": 0,
"firstRowAsHeader": false
},
"fileName": "[parameters('Veh_Obj_properties_typeProperties_fileName')]",
"folderPath": "[parameters('Veh_Obj_properties_typeProperties_folderPath')]"
}
},
"dependsOn": [
"[concat(variables('factoryId'), '/linkedServices/AzureDataLakeStore1')]"
]
}
"#
$AddArray = #('' +
'{' +
' "name": "VIN",' +
' "type": "String",' +
' "newkey1": "newvalue1",' +
' "newkey2": "newvalue2"' +
'}';
'{' +
' "name": "MAKE",' +
' "type": "String",' +
' "newkey1": "newvalue1",' +
' "newkey2": "newvalue2"' +
'}'
)
Add-Type -AssemblyName System.Web.Extensions;
$json = [System.Web.Script.Serialization.JavaScriptSerializer]::new();
$json.MaxJsonLength = 2147483647; #max integer or less
$CompanyObj=$json.Deserialize($CompanyJSON, [System.Object]);
foreach ($chank in $AddArray){
$CompanyObj.properties.structure+=$json.Deserialize($chank, [System.Object]);
}
$CompanyJSON=$json.Serialize($CompanyObj);
Write-Host $CompanyJSON
You'll see \u0027 etc. It's unicode. JSON will understand it.
ConvertFrom-Json is failing to successfully parse my Json into object, despite my object being legal Json.
I'm using the latest version of Powershell.
When I try parsing a google request JSON body like this:
{
"reportScope": {
"agencyId": "11111111111111111",
"advertiserId": "22222222222222222"
},
"reportType": "adGroup",
"columns": [
{
"columnName": "status"
},
{
"columnName": "date"
},
{
"columnName": "account"
},
{
"columnName": "accountType"
},
{
"columnName": "campaign"
},
{
"columnName": "adGroup"
},
{
"columnName": "deviceSegment"
},
{
"columnName": "impr"
},
{
"columnName": "clicks"
},
{
"columnName": "dfaRevenue"
},
{
"columnName": "cost"
},
{
"columnName": "avgPos"
}
],
"timeRange": {
"startDate": "2017-09-13",
"endDate": "2017-08-14"
},
"filters": [
{
"column": {"columnName": "impr"},
"operator": "greaterThan",
"values": "0"
}
],
"downloadFormat": "csv",
"maxRowsPerFile": 6000000,
"statisticsCurrency": "agency",
"verifySingleTimeZone": "false",
"includeRemovedEntities": "false"
}
I get an object like this:
[DBG]: PS Microsoft.PowerShell.Core\FileSystem::\\psDashboard>> $objJson
reportScope : #{agencyId=11111111111111111; advertiserId=22222222222222222}
reportType : adGroup
columns : {#{columnName=status}, #{columnName=date}, #{columnName=account}, #{columnName=accountType}...}
timeRange : #{startDate=2017-09-13; endDate=2017-08-14}
filters : {#{column=; operator=greaterThan; values=0}}
downloadFormat : csv
maxRowsPerFile : 6000000
statisticsCurrency : agency
verifySingleTimeZone : false
includeRemovedEntities : false
[DBG]: PS Microsoft.PowerShell.Core\FileSystem::\\psDashboard>> $objJson.filters
column operator values
------ -------- ------
greaterThan 0
Notice column should contain #{columnNane=imgr} but it instead contains nothing.
Code used for parsing:
$objJson = Get-Content -Path $FileName -Raw | ConvertFrom-Json
Any ideas?
i took your code and was able to get to the object you are looking for I first put it in a Here string #""# and was able to get to it.. then like you I put it in a variable and piped to convertfrom-json and got the ame results.
$test = #"
{
"reportScope": {
"agencyId": "11111111111111111",
"advertiserId": "22222222222222222"
},
"reportType": "adGroup",
"columns": [
{
"columnName": "status"
},
{
"columnName": "date"
},
{
"columnName": "account"
},
{
"columnName": "accountType"
},
{
"columnName": "campaign"
},
{
"columnName": "adGroup"
},
{
"columnName": "deviceSegment"
},
{
"columnName": "impr"
},
{
"columnName": "clicks"
},
{
"columnName": "dfaRevenue"
},
{
"columnName": "cost"
},
{
"columnName": "avgPos"
}
],
"timeRange": {
"startDate": "2017-09-13",
"endDate": "2017-08-14"
},
"filters": [
{
"column": {"columnName": "impr"},
"operator": "greaterThan",
"values": "0"
}
],
"downloadFormat": "csv",
"maxRowsPerFile": 6000000,
"statisticsCurrency": "agency",
"verifySingleTimeZone": "false",
"includeRemovedEntities": "false"
}
"#
$t = $test | ConvertFrom-Json
$t.filters.column
PS C:\WINDOWS\system32> $t.filters
column operator values
------ -------- ------
#{columnName=impr} greaterThan 0
PS C:\WINDOWS\system32> $t.filters.column
columnName
----------
impr
Using get content and using convertfrom-json.
PS C:\WINDOWS\system32> $test.filters
column operator values
------ -------- ------
#{columnName=impr} greaterThan 0
PS C:\WINDOWS\system32> $test.filters.column
columnName
----------
impr
could it be your version of convertFrom-json my module version is:
PS C:\WINDOWS\system32> gcm convertfrom-json
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet ConvertFrom-Json 3.1.0.0 Microsoft.PowerShell.Utility
I am trying to use PowerShell to extract value from JSON object, I have the following JSON:
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"clusterName": {
"value": "hbasehd35"
},
"location": {
"value": "East US 2"
},
"clusterType": {
"value": "hbase"
},
"clusterVersion": {
"value": "3.5"
},
"clusterWorkerNodeCount": {
"value": 5
},
"subnetName": {
"value": "hbase-subnet"
},
"headNodeSize": {
"value": "Standard_D12_v2"
},
"workerNodeSize": {
"value": "Standard_D12_v2"
},
"zookeeperSize": {
"value": "Large"
},
"clusterStorageAccountName": {
"value": "hbasestorage"
},
"storageAccountType": {
"value": "Standard_GRS"
},
"Environment": {
"value": "test"
}
}
}
Here I want to extract clusterStorageAccountName from this file using powershell, and assign it to variable.
Anyone know how to do this ?
Use the Get-Content cmdlet to read the file, convert it using the ConvertFrom-Json cmdlet and just access the property you want:
$yourVariable = (Get-Content 'yourJsonFilePath.json' | ConvertFrom-Json).parameters.clusterStorageAccountName.value
I don't know why, but the approach from message above don't work for me. But works just getting key that you need by key after "ConvertFrom-Json" command, e.g.:
$yourVariable = (Get-Content 'yourJsonFilePath.json' | ConvertFrom-Json).clusterStorageAccountName