Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, June 21, 2016 3:59 PM
Hello Everyone,
I posted a question similar to this earlier yesterday and it was my mistake of not being specific enough, hopefully you guys can so kindly help me today. I am working with a JSON file which has a nested array and I need to be able to call a line of information whether it be in VNX, TWX, XCX or KIS, and when it calls a line in one of these it needs to call it based on the information on either line 1,2 ,3 or 4. Whether it be the first number which is a mix of letters and numbers(serial #) or IP address.
**I am new to powershell and doing self teaching, please bare with me**
$json = (Get-Content C:\catalog.json -Raw)$json| ConvertFrom-Json
This displays all of the information but thats not what I want, I want to be able to implement a For-loop which runs through and picks out a devices(VNX, TWX, XCX, KIS) serial number and displays the information
#This is the Sample File with sample information plugged in
{
"VNX": [
{"VNX": "45T45T45T45","iP": "213","user": "sysadmin","password": "hi"},
{"VNX": "45T45T45T23E","iP": "213","user": "sysadmin","password": "hi"},
{"VNX": "3R43RD54T5T5","iP": "213","user": "sysadmin","password": "hi"},
{"VNX": "5Y6Y6YTRY565","iP": "213","user": "sysadmin","password": "hi"}],
"VPX":[
{"VPX": "U98U8U8U8","iP": "213","user": "sysadmin","password": "hi"},
{"VPX": "3E98U8U98","iP": "213","user": "sysadmin","password": "hi"},
{"VPX": "6YY5Y65I4","iP": "213","user": "sysadmin","password": "hi"},
{"VPX": "3R43R5T45","iP": "213","user": "sysadmin","password": "hi"}],
"Centera": [
{"CEN": "f438u9u9u","iP": "213","user": "sysadmin","password": "hi"},
{"CEN": "23ee2d09i","iP": "213","user": "sysadmin","password": "hi"},
{"CEN": "90i329id9","iP": "213","user": "sysadmin","password": "hi"},
{"CEN": "32d9iid0i","iP": "213","user": "sysadmin","password": "hi"}],
"VMX":[
{"VMX": "3d2d23dfe","iP": "213","user": "sysadmin","password": "hi"},
{"VMX": "1w21w2wf4","iP": "213","user": "sysadmin","password": "hi"},
{"VMX": "3e32e888u","iP": "213","user": "sysadmin","password": "hi"},
{"VMX": "z38ue328u","iP": "213","user": "sysadmin","password": "hi"}]
}
I might add that all of this information will be coming from a JSON file saved externally.
This is where I stand in terms of thinking where to go with this
$devices = $x |
Get-Member -MemberType Properties |
Select-Object -ExpandProperty Name
ForEach($devices in $devices) {
If(#dont know what to put in here....){
Write-Output "$devices #stuck......"
}
}
ForEach($devices in $devices) {
If(#dont know what to put in here....){
Write-Output "$devices #stuck......"
}
}
ForEach($devices in $devices) {
If(#dont know what to put in here....){
Write-Output "$devices #stuck......"
}
}
ForEach($devices in $devices) {
If(#dont know what to put in here....){
Write-Output "$devices #stuck......"
}
}
#Just putting as many would be deemed necessary
Like I said I am learning this as I go along pretty much but I am somewhat stuck on getting this to work, any direction or help is appreciated. Feel free to tell me if it is completely wrong, I am open to it, ill take it as constructive criticism :)
All replies (10)
Tuesday, June 21, 2016 7:10 PM ✅Answered
This is one way to fix your json:
$json=@'
[
{"Name":"VNX", "ID": "45T45T45T45", "iP": "213", "user": "sysadmin", "password": "hi"},
{"Name":"VNX", "ID": "45T45T45T23E", "iP": "213", "user": "sysadmin", "password": "hi"},
{"Name":"VNX", "ID": "3R43RD54T5T5", "iP": "213", "user": "sysadmin", "password": "hi"},
{"Name":"VNX", "ID": "5Y6Y6YTRY565", "iP": "213", "user": "sysadmin", "password": "hi"},
{"Name":"VPX", "ID": "U98U8U8U8", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"VPX", "ID": "3E98U8U98", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"VPX", "ID": "6YY5Y65I4", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"VPX", "ID": "3R43R5T45", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"CEN", "ID": "f438u9u9u", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"CEN", "ID": "23ee2d09i", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"CEN", "ID": "90i329id9", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"CEN", "ID": "32d9iid0i", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"VMX", "ID": "3d2d23dfe", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"VMX", "ID": "1w21w2wf4", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"VMX", "ID": "3e32e888u", "iP": "213", "user": "sysadmin", "password": "hi"} ,
{"Name":"VMX", "ID": "z38ue328u", "iP": "213", "user": "sysadmin", "password": "hi"}
]
'@ | ConvertFrom-Json
$json | ft
Why use json when you can build objects in PowerSHell.
\(ツ)_/
Tuesday, June 21, 2016 5:23 PM
Same question as this one you asked: https://social.technet.microsoft.com/Forums/windowsserver/en-US/e5339aec-8ba0-4c4f-9286-88e93489c9ae/iterating-through-json-file-powershell?forum=winserverpowershell#b8623e03-8723-43aa-a668-004ef27389c5
Same answer.
\(ツ)_/
Tuesday, June 21, 2016 5:26 PM
Also the json example you have posted is broken. At minimum it is an array of arrays of arrays with no root so you will have to pick it apart by knowing in advance or by converting the hashes to objects.
\(ツ)_/
Tuesday, June 21, 2016 5:49 PM
$json=@'
{
"VNX": [
{ "VNX": "45T45T45T45", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "VNX": "45T45T45T23E", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "VNX": "3R43RD54T5T5", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "VNX": "5Y6Y6YTRY565", "iP": "213", "user": "sysadmin", "password": "hi" }],
"VPX":[
{ "VPX": "U98U8U8U8", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "VPX": "3E98U8U98", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "VPX": "6YY5Y65I4", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "VPX": "3R43R5T45", "iP": "213", "user": "sysadmin", "password": "hi" }],
"Centera": [
{ "CEN": "f438u9u9u", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "CEN": "23ee2d09i", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "CEN": "90i329id9", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "CEN": "32d9iid0i", "iP": "213", "user": "sysadmin", "password": "hi" }],
"VMX":[
{ "VMX": "3d2d23dfe", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "VMX": "1w21w2wf4", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "VMX": "3e32e888u", "iP": "213", "user": "sysadmin", "password": "hi" },
{ "VMX": "z38ue328u", "iP": "213", "user": "sysadmin", "password": "hi" }]
}
'@ | ConvertFrom-Json
$names = $json.PsObject.Properties | select -expand name
0..($names.Count-1) | %{ $name = $names[$_]; $json.$name | select @{ N = 'Object'; e = { $name } }, * } | ft
\(ツ)_/
Tuesday, June 21, 2016 7:05 PM
Thanks for replying but Im not sure if I am conveying the question properly but I dont need all the Device names to show up once I run the command. Perhaps I can give you an example....lets say, that A company needs to send out a report to other staff members letting them know which device(VMX, VPX, etc) is currently down on the system. Lets say the currently have it hardcoded and want to create a loop which will run through a JSON file and fill and pull the single device from lets say Device VMX and it pulls out the specific one (listed below) and sends it to them, that way the other people in the company know which device is down and needs to be looked at, and it displays the IP address and login information along with the device name...hopefully that cleared it up a bit and it makes more sense....
so the answer to the other question, if we dont use For-Loops for Powershell, and JSON , what can we use?? Im very confused now
{"VMX": "z38ue328u","iP": "213","user": "sysadmin","password": "hi"}]
Tuesday, June 21, 2016 7:09 PM
The json is broken. It cannot be iterated correctly because it does not convert correctly. I showed how to get as close as is possible but you will still lose most of the records.
\(ツ)_/
Tuesday, June 21, 2016 7:46 PM
The JSON file converts just fine on the ISE on my desktop, i dont understand how it is broken? I would build in Powershell, but that is a requirement for this task that I am trying to accomplish unfortunately, and I dont know how to go about it
Tuesday, June 21, 2016 8:02 PM
So this is a homework assignment. Yes the json converts without errors but most of the data is missing from the PowerShell object.
Why would anyone ask to use json in PowerShell. It is there so we can pass data back and forth to JavaScript tasks on a web server. Your hand coded json just repeatedly overwrites the objects when converted.
The methods I showed are the only ones that will work. The object is not a collection so you cannot use it in a loop.
\(ツ)_/
Wednesday, June 22, 2016 3:58 PM
Its an assignment, so now the person has told me that a For loop isnt necessary but going through the file still remains the idea.
Wednesday, June 22, 2016 4:02 PM
Its an assignment, so now the person has told me that a For loop isnt necessary but going through the file still remains the idea.
Well we cannot do your homework for you and I already shoed you how to use PS with json. You have to fix the json.
\(ツ)_/