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 6:09pm

Sorry but we will not write custom scripts for you.  You can try and post your request here at the bottom of the page: https://gallery.technet.microsoft.com/scriptcenter/site/requests

Free Windows Admin Tool Kit Click here and download it now
September 6th, 2015 6:42pm

Here is a hint at how to do this:

Foreach ($User in $UserCSV){
      $p=@{
             Name=$user.Name
             UserPrincipalName=$null
             CanonicalName =$null
             Description=$null
             ... etc ...
      }
      if($user=Get-ADUser -Filter "Name -eq '$($user.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){
           $p.UserProncipalName=$user.UserProncipalName
           $p.Description=$user.Description
           ... etc ...
      }
      New-Object PsObject -Property $p
}
I left all of the typing to you.

September 6th, 2015 7:06pm

Thanks Jrv!

However, I wanted the correct syntax to generate my array of key-value pairs & access those values in a loop.

My Syntax is quite clearly having an error due to which i'm unable to retrieve the correct values.


Free Windows Admin Tool Kit Click here and download it now
September 6th, 2015 7:07pm

What syntax?  You have no code and no syntax.  You are just posting nonsense.  Please use the following to learn how to use PowerShell. post back when you understand.

https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396

September 6th, 2015 7:40pm

Thank you!

Will try this way as well in case the below does NOT work.

Currently, trying in this simpler way -

Works: 

$output +=  @{"UserPrincipalName"="NA"}

Does NOT work:

$output +=  @{"UserPrincipalName"="NA"}, @{"CanonicalName"="NA"}

Values are getting stored, but when I attempt to access via below manner, 

however, when I attempt to retrieve the values via the below statement, I am NOT able to get them in the right order.

Import-Csv $ImportFile -Delimiter ',' |
    Foreach { $_.Status = $Status[$i++] ; $_.UserPrincipalName = $($Output[$j]).UserPrincipalName;$_.CanonicalName = $($Output[$j++]).CanonicalName; $_}|

    Export-Csv $ExportFile -Delimiter ',' -NoTypeInformation

Free Windows Admin Tool Kit Click here and download it now
September 6th, 2015 8:27pm

I think you need to learn the basics and learn what a hash is.  Just using lots of curly braces does not make a hash.  It just look impressive. Only when you learn the very basic bits will you be able to understand this.
September 6th, 2015 8:36pm

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

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

Other recent topics Other recent topics