Powershell - join arrays

I am new to powershell and need help. My first script to work automates new and named users in the AD environment.

A CSV dump will be executed once a day from our Peoplesoft system. I use Import-CSVand create 3 arrays (new, terms and processed).

The problem I ran into is combining the three arrays when I go through all the users and try to get them back to the file. The code is broken into lines $New += $Term. I believe that this is due to the fact that in my test file there is only 1 entry for each type of user (new, thermal and processed) (I know, add more users and hellip; I can’t. This can be a real result for any particular day ) The following is 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

, , .

error - , [System.Management.Automation.PSObject] "op_Addition".

+5
4

Import-Csv 1 , , , . , @(). , .

, Import-Csv @(), - .

$new = @( Import-Csv $File | Where-Object {...} )
# or
$new = Import-Csv $File | Where-Object {...}
$new = @($new)
+11

, CSV 3 ? , "" ?

. "Count" , , 1 .

#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
+1

, :

$array = @()
$array = gci test.txt
$array.GetType()

[array]$array = @()
$array = gci test.txt
$array.GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True FileInfo System.IO.FileSystemInfo
True True Object [] System.Array

0

, , - , ... $new , csv ADD , import-csv.

$new = @()
$new += Import-Csv $File | Where-Object {
           $_.TermDate -eq "" -and $_.LastName -ne "" -and $_.Processdate -eq ""
         }
0

All Articles