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"}
$output += @{"UserPrincipalName"="NA"}#, @{"CanonicalName"="NA"}
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
UPDATE #3:
Corrected the Syntax by self.
Was trying to access it as multiple array elements via Name-Expression pair-like syntax as I needed to place a key-value pair as an Array Element.
This was because earlier, these values were placed in another csv via below command:
#Import a few values & add empty columns into a temporary CSV for now
Select SystemName, Name, @{Name=ServiceAccount;Expression={$_.StartName}}, state,startmode, @{Name="Status";Expression={""} },
@{Name="UserPrincipalName";Expression={""}} , @{Name="CanonicalName";Expression={""}} } |
Corrected line looks like:
#Get the values here
$output += @{"UserPrincipalName"="NA";"CanonicalName"="NA"}
#Export it to CSV here
Import-Csv $ImportFile -Delimiter ',' |Foreach { $_.Status = $Status[$i++] ; $_.UserPrincipalName = $($Output[$j]).UserPrincipalName;$_.CanonicalName = $($Output[$j++]).CanonicalName;$_}|
Export-Csv $ExportFile -Delimiter ',' -NoTypeInformation
- Edited by AdarshPrasad 23 hours 41 minutes ago