Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - combining arrays

I am new to powershell and in need of help. My first script for work is automate the new and termed users in AD environment.

A CSV dump will be done once daily from our Peoplesoft system. I use Import-CSV and create 3 arrays (new, term and processed).

The trouble I'm having is with combining the 3 arrays once i loop through all the users and try putting it back into the file. The code breaks at the $New += $Term lines. I believe this is due to the fact that there is only 1 record of each user type (new, term and processed) in my test file (I know, add more users…can't. This may be a real world outcome for any particular day). Below is my sample code:

#Get Credentials from user
$c = Get-Credential

#Get Date for $Term array population
$e = Get-Date -format M/d/yyyy

#Set file location and variable for said file
$File = "c:\users\nmaddux\desktop\adduserstuff\test.csv"

#Import record sets for New and Term users
$New   = @()
$Term  = @()
$Procd = @()
$New   = Import-Csv $File | Where-Object {
           $_.TermDate -eq "" -and $_.LastName -ne "" -and $_.Processdate -eq ""
         }
$Term  = Import-Csv $File | Where-Object {
           $_.TermDate -ne "" -and $_.Processdate -eq "" -and $_.TermDate -le $e
         }
$Procd = Import-Csv $File | Where-Object { $_.Processdate -ne "" }

#Process both new and term users provided there are records to process for each
If ($New -ne $NULL -and $Term -ne $NULL) {
  # Some code to process users
}

$new += $term
$new += $Procd
$new | Export-Csv $file -NoTypeInformation -ErrorAction SilentlyContinue

So it will export but only partial results.

error - Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.

like image 212
user2077056 Avatar asked Feb 15 '13 21:02

user2077056


2 Answers

If Import-Csv only returns 1 result, then you are correct that your variable is assumed NOT to be an array, then concatenation will fail. This is not change by the fact that you have pre-initialized your variables with @(). In fact, that step isn't necessary.

To force the result to be treated as an array, you can either wrap your whole Import-Csv line in @(), or do something similar afterward.

$new = @( Import-Csv $File | Where-Object {...} )
# or
$new = Import-Csv $File | Where-Object {...}
$new = @($new)
like image 169
latkin Avatar answered Oct 06 '22 01:10

latkin


So you are importing the same CSV file 3 times? isn't it better to import it once and then set the arrays to be filtered "views" of it?

Sort of like this. You should also be able to use the "Count" value from each array as well to say whether 1 or more results were returned.

#Get Credentials from user
$c = Get-Credential

#Get Date for $Term array population
$e = Get-Date -format M/d/yyyy

#Set file location and variable for said file
$File = "c:\users\nmaddux\desktop\adduserstuff\test.csv"

#Import record sets for New and Term users
[array]$New
[array]$Term
[array]$Procd

[array]$Import = Import-Csv $File
[array]$New = $Import | ? {$_.TermDate -eq "" -and $_.LastName -ne "" -and $_.Processdate -eq ""}
[array]$Term = $Import | ? {$_.TermDate -ne "" -and $_.Processdate -eq "" -and $_.TermDate -le $e}
[array]$Procd = $Import | ? {$_.Processdate -ne ""}

#Process both new and term users provided there are records to process for each
if (($New.Count -gt 0) -and ($Term.Count -gt 0))
{
    # Some code to process users
}

$new += $term
$new += $Procd
$new | Export-Csv $file -NoTypeInformation -ErrorAction SilentlyContinue
like image 22
HungryHippos Avatar answered Oct 06 '22 02:10

HungryHippos