Unable to Create an array of Hash table for populating with 'NA' values for missing data

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








September 6th, 2015 10:09pm

Thanks again jrv. Got the syntax working. Reason I was using wrong syntax was that earlier I had posted key value pairs for a CSV in Name-Expression pairs (Details in Update # 3)

I was of the thought that since I was accessing a key-value pair & pushing it onto an array, the syntax would look that way.

It was late at around 3AM local time when it was posted. Sincere apologies for the 'syntax error' while posting.

Also, I had a comment line # in my Update 2 which might have 'sparked' you off into 'generic fulminate' mode which I do NOT appreciate nor would any genuine learner :)

Thanks!

 
Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 3:25am

Sorry but it is very hard for anyone to understand what you are asking or posting because you use statements that are not correct and complicate it further with very vague and convoluted comments.  I made a best guess but you clearly couldn't understand it due to your lack of knowledge of PowerShell basics. Complaining about being told the truth will not help you to learn. Please try and take a more professional view of criticism. We cannot progress without learning where we are wrong.

September 7th, 2015 5:24am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics