Hi Powershell Team,
I am trying to Create an array of Hash table.
Currently, I get the result of the below command to a csv file.
#Array Declaration
[array]$Output = @()
Foreach ($User in $UserCSV)
{
#Search for my user in a file, if exists in AD, get following properties
$output += Get-ADUser $SA_name -Properties UserPrincipalName, CanonicalName, Description, Title, userWorkstations, Enabled, LockedOut, PasswordExpired,PasswordLastSet, PasswordNeverExpires,whenChanged,whenCreated -ErrorAction Stop | Select UserPrincipalName, CanonicalName, Description, Title, userWorkstations, Enabled, LockedOut, PasswordExpired,PasswordLastSet, PasswordNeverExpires,whenChanged,whenCreated
#Else I want to form my own Key Value pairs for the similar to the output of Get-ADUser
$output += @(@{"UserPrincipalName"="NA"}, @{"CanonicalName"="NA"}, @{"Description"="NA"}, @{"Title"="NA"}, @{"userWorkstations"="NA"},
@{"Enabled"="NA"}, @{"LockedOut"="NA"}, @{"PasswordExpired"="NA"}, @{"PasswordLastSet"="NA"}, @{"PasswordNeverExpires"="NA"}, @{"whenChanged"="NA"},
@{"whenCreated"="NA"})
}
#Put all these stuff into another excel.
# If User was present, it should output his properties, else, it should output NA against his properties.
#Currently, if I don't do this, i'm getting lesser rows for user properties resulting in mis-match in the final csv
Import-Csv $ImportFile -Delimiter ',' |
Foreach { $_.Status = $Status[$i++] ; $_ } | Select-Object *,@{Name='UserPrincipalName';Expression={$($Output[$i++]).Title}} |
Export-Csv $ExportFile -Delimiter ',' -NoTypeInformation
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
Lets say the user does NOT exist in AD, then I need to output NA against his properties.
In the final CSV, I would require the output as shown below:
-Adarsh
UPDATE #2:
After a bit of to and fro, got it to work for 1 key-value pair via below snippet:
$output += @{"UserPrincipalName"="NA"}
Import-Csv $ImportFile -Delimiter ',' |
Foreach { $_.Status = $Status[$i++] ; $_.UserPrincipalName = $($Output[$j++]).UserPrincipalName; $_}|
Hopefully, should work if I add other properties as well.
Will update soon.
$output
+= @{"UserPrincipalName"="NA"}#, @{"CanonicalName"="NA"}
- Edited by AdarshPrasad 7 hours 49 minutes ago UPDATE 2