Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

And a new member to an existent array

$
0
0

Good morning,

Here i have a little script I wrote.

cls
$Date_Time = $(Get-Date -format g)
$Computers = Import-csv "Computers.csv"
$File = "Exported_File.csv"
$report = @()
foreach ($Computer in $Computers)
{  
    $Obj = New-Object PSObject
    $Obj | Add-Member NoteProperty -Name "Date_Time" -Value $Date_Time
    $Obj | Add-Member NoteProperty -Name "Server" -Value $Computer.Name
    $Type = $Computer.Type  
    switch (($Type))
    {
        "Type_01"
        {                                      
            $Obj | Add-Member NoteProperty -Name "Type_01" -Value $Computer.Data
            $report = $report + $Obj                            
        }
        "Type_02"
        {
            $Obj | Add-Member NoteProperty -Name "Type_02" -Value $Computer.Data
            $report = $report + $Obj                            
        }
        "Type_03"
        {                                  
            $Obj | Add-Member NoteProperty -Name "Type_03" -Value $Computer.Data
            $report = $report + $Obj                
        }  
    }
}
$report | Export-Csv $File -Delimiter "|" -Notype

What i want to do is to create an array with different fields depending of the  value of the $Type variable.
I've tried everything but I always get the same array.
The first loop if the value of the $type variable is Type_02 i get an array with these members Date_Time, Server and Type_02.
Suppose the next loop, the value of the variable $Type is  "Type_03", then i try to add a new member "Type_03" to the existent array, but its not working.
I dont know how to solve this, so any ideas will be appreciated


Viewing all articles
Browse latest Browse all 8583

Trending Articles