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
- Homedirectory has additional backslashes it should be = \filer4\userk$\kuser2
- 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.