Coverting hashtable with multiple objects to Json

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 1 year, 5 months ago.

  • Author
    Posts
  • #30500
    Profile photo of Jason McMahan
    Jason McMahan
    Participant

    Good morning,
    I am working on user termination script which ingests of csv file with
    SamAccountName, HomeDirectory,Country (C)

    For each of these items I am then checking the size of their directories, noting that and the earlier information in an ordered hash table and passing it to convertTo-json.

    $Users = Import-Csv -LiteralPath c:\test\users.csv
    Foreach ($User in $Users) {
    $DATE = Get-Date -Format "yyyy-MM-dd"
    $Username = $User.SamAccountName
    $Hdrive = $User.HomeDirectory
    $Filer = ($User.HomeDirectory.Split("\")[2]).ToUpper()
    $Country = $User.Country
    $HdriveSize = "{0:N2}" -f ((Get-ChildItem -LiteralPath $Hdrive -Recurse | Measure-Object -Property length -sum).sum / "1$Unit")
    
    $Properties = [psobject][ordered]@{
    	'REMOVAL_DATE' = $Date; 
    	'USERNAME' = $Username;
    	'HOMEDIRECTORY' = $Hdrive;
    	'HOMEDIRECTORY_SIZE' = $HdriveSize;
    	'HOMEDIRECTORY_UNIT' = $Unit;
    	'COUNTRY' = $Country;
    }
    $Properties# | ConvertTo-Json -Depth 2 | Out-File -Encoding ascii -Append "c:\Test\TerminatedUsers_$date.log"
    

    The problem I have is when it outputs a hash of multiple users and converts to json it outputs as

    {
    "REMOVAL_DATE": "2015-10-07",
    "USERNAME": "quser1",
    "HOMEDIRECTORY": "\\\\filer1\\userq$\\quser1",
    "HOMEDIRECTORY_SIZE": "14.59",
    "HOMEDIRECTORY_UNIT": "GB",
    "COUNTRY": "US"
    }
    {
    "REMOVAL_DATE": "2015-10-07",
    "USERNAME": "kuser2",
    "HOMEDIRECTORY": "\\\\filer4\\userk$\\kuser2",
    "HOMEDIRECTORY_SIZE": "2.49",
    "HOMEDIRECTORY_UNIT": "GB",
    "COUNTRY": "CA"
    }

    The two problems
    1. Homedirectory has additional backslashes it should be = \\filer4\userk$\kuser2
    2. It isn't separating the objects as it should. It should appear like
    [
    {
    "USERNAME": "quser1",
    "HOMEDIRECTORY": "\\filer1\userq$\quser1",
    "HOMEDIRECTORY_SIZE": "14.59",
    "HOMEDIRECTORY_UNIT": "GB",
    "COUNTRY": "US"
    },
    {
    "USERNAME": "kuser2",
    "HOMEDIRECTORY": "\\filer1\\userk$\\kuser2",
    "HOMEDIRECTORY_SIZE": "2.49",
    "HOMEDIRECTORY_UNIT": "GB",
    "COUNTRY": "CA"
    }
    ]

    Any help or suggestions would be greatly appreciated.

  • #30504
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I believe the backslashes are correct. In JavaScript (and thus JSON), backslash is the escape character, and they need to be doubled up.

    To treat the objects as an array, you need to pipe everything at once to ConvertTo-Json, instead of doing one object at a time inside the loop. Something like this:

    $Users = Import-Csv -LiteralPath c:\test\users.csv
    
    $objects = @(
        Foreach ($User in $Users) {
            $DATE = Get-Date -Format "yyyy-MM-dd"
            $Username = $User.SamAccountName
            $Hdrive = $User.HomeDirectory
            $Filer = ($User.HomeDirectory.Split("\")[2]).ToUpper()
            $Country = $User.Country
            $HdriveSize = "{0:N2}" -f ((Get-ChildItem -LiteralPath $Hdrive -Recurse | Measure-Object -Property length -sum).sum / "1$Unit")
        
            [pscustomobject]@{
        	    'REMOVAL_DATE' = $Date; 
        	    'USERNAME' = $Username;
        	    'HOMEDIRECTORY' = $Hdrive;
        	    'HOMEDIRECTORY_SIZE' = $HdriveSize;
        	    'HOMEDIRECTORY_UNIT' = $Unit;
        	    'COUNTRY' = $Country;
            }
        }
    )
    
    $objects | ConvertTo-Json -Depth 2 | Out-File -Encoding ascii -Append "c:\Test\TerminatedUsers_$date.log"
    

You must be logged in to reply to this topic.