Chapters/working-with-json/snippets.ps1

# json
{
    "Name":  "bits",
    "DisplayName":  "Background Intelligent Transfer Service",
    "Status":  4
}



# more json
[
    {
        "Name":  "BITS",
        "DisplayName":  "Background Intelligent Transfer Service",
        "Status":  4
    },
    {
        "Name":  "Bluetooth Device Monitor",
        "DisplayName":  "Bluetooth Device Monitor",
        "Status":  4
    },
    {
        "Name":  "Bluetooth OBEX Service",
        "DisplayName":  "Bluetooth OBEX Service",
        "Status":  4
    },
    {
        "Name":  "BrokerInfrastructure",
        "DisplayName":  "Background Tasks Infrastructure Service",
        "Status":  4
    },
    {
        "Name":  "Browser",
        "DisplayName":  "Computer Browser",
        "Status":  4
    },
    {
        "Name":  "BthHFSrv",
        "DisplayName":  "Bluetooth Handsfree Service",
        "Status":  1
    },
    {
        "Name":  "bthserv",
        "DisplayName":  "Bluetooth Support Service",
        "Status":  4
    }
]



# nested objects
{
    "Name":  "bits",
    "DisplayName":  "Background Intelligent Transfer Service",
    "Status":  4,
    "RequiredServices":  [
                     {
                         "CanPauseAndContinue":  false,
                         "CanShutdown":  false,
                         "CanStop":  false,
                         "DisplayName":  "Remote Procedure Call (RPC)",
                         "DependentServices":  null,
                         "MachineName":  ".",
                         "ServiceName":  "RpcSs",
                         "ServicesDependedOn":  "DcomLaunch RpcEptMapper",
                         "ServiceHandle":  null,
                         "Status":  4,
                         "ServiceType":  32,
                         "StartType":  2,
                         "Site":  null,
                         "Container":  null
                     },
                     {
                         "CanPauseAndContinue":  false,
                         "CanShutdown":  false,
                         "CanStop":  true,
                         "DisplayName":  "COM+ Event System",
                         "DependentServices":  "igfxCUIService1.0.0.0
                          COMSysApp SENS BITS"
,
                         "MachineName":  ".",
                         "ServiceName":  "EventSystem",
                         "ServicesDependedOn":  "rpcss",
                         "ServiceHandle":  null,
                         "Status":  4,
                         "ServiceType":  32,
                         "StartType":  2,
                         "Site":  null,
                         "Container":  null
                     }
                         ]
}



# convert to json
get-ciminstance win32_computersystem | convertto-json



# now into a file
get-ciminstance win32_computersystem | convertto-json | 
out-file wmics.json
get-ciminstance win32_computersystem | convertto-json | 
set-content .\wmics2.json 



# smaller data
get-ciminstance win32_computersystem | convertto-json -compress



# filter first
get-ciminstance win32_computersystem -computername $env:computername | 
select PSComputername,Manufacturer,
@{Name="MemoryGB";Expression={$_.totalPhysicalmemory/1GB -as [int]}},
Number* | ConvertTo-Json



# creates
{
    "PSComputerName":  "CLIENT01",
    "Manufacturer":  "LENOVO",
    "MemoryGB":  8,
    "NumberOfLogicalProcessors":  4,
    "NumberOfProcessors":  1
}



# custom objects
[pscustomobject]@{
  Path = "C:\Scripts"
  LastModified = "1/1/2017"
  Count = 20
  Types = @(".ps1","psm1","psd1","json","xml")
} | ConvertTo-Json



# creates
{
    "Path":  "C:\\Scripts",
    "LastModified":  "1/1/2017",
    "Count":  20,
    "Types":  [
                  ".ps1",
                  "psm1",
                  "psd1",
                  "json",
                  "xml"
              ]
}



# metadata
[pscustomobject]@{
  Created = (Get-Date)
  Comment = "config data for script tool"
},
[pscustomobject]@{
  Path = "C:\Scripts"
  LastModified = "1/1/2017"
  Count = 20
  Types = @(".ps1","psm1","psd1","json","xml")
} | ConvertTo-Json 



# creates
[
    {
        "Created":  {
                        "value":  "\/Date(1483398036020)\/",
                        "DisplayHint":  2,
                        "DateTime":  "Monday, January 2, 2017 6:00:36 PM"
                    },
        "Comment":  "config data for script tool"
    },
    {
        "Path":  "C:\\Scripts",
        "LastModified":  "1/1/2017",
        "Count":  20,
        "Types":  [
                      ".ps1",
                      "psm1",
                      "psd1",
                      "json",
                      "xml"
                  ]
    }
]



# slight change
[pscustomobject]@{
  Created = (Get-Date).Tostring()
  Comment = "config data for script tool"
},



# now results
{
        "Created":  "1/2/2017 6:03:40 PM",
        "Comment":  "config data for script tool"
    }



# json snippet
 {
        "Name":  "wuauserv",
        "DisplayName":  "Windows Update",
        "Status":  1,
        "MachineName":  "chi-dc04",
        "Audit":  "12/01/16"
    },



# convert from json
$in = get-content c:\work\audit.json | convertfrom-json

$in | get-member

$in[0..2]

$in[0..2] | Select Name,Displayname,
@{Name="Status";Expression = { $_.Status -as
System.ServiceProcess.ServiceControllerStatus]}},
@{Name="Audit";Expression= { $_.Audit -as [datetime]}},
@{Name="Computername";Expression = {$_.Machinename}}



# again
get-content c:\work\audit.json | 
convertfrom-json | Select Name,Displayname,
@{Name="Status";Expression = { $_.Status -as [System.ServiceProcess.ServiceControllerStatus]}},
@{Name="Audit";Expression= { $_.Audit -as [datetime]}},
@{Name="Computername";Expression = {$_.Machinename}}



# fix
get-content c:\work\audit.json | 
convertfrom-json | 
foreach { $_ | Select Name,Displayname,
@{Name="Status";Expression = { $_.Status -as [System.ServiceProcess.ServiceControllerStatus]}},
@{Name="Audit";Expression= { $_.Audit -as [datetime]}},
@{Name="Computername";Expression = {$_.Machinename}}
}