Home > Back-end >  Powershell - combining multiple for each into a single output
Powershell - combining multiple for each into a single output

Time:11-11

Good morning everyone,

I am still learning powershell and have a script that my company run's. I have adjusted it slightly and it works well. however...

Overview Script pulls in a CSV file and then for each device on the CSV it logs in and grabs all of the VIP information and then outputs it to a CSV that is dated and titled with the device name.

Ask

I would like adjust the script so that instead of having 50 CSV files I could have a single CSV file with an extra column that would like the device name that all the VIPS are on. How do I do that?

Current Script

$user = "user"
$pass1 = Read-Host "Enter Netscaler Password"
$Devicelist = Import-Csv'C:\Users\user\Documents\20221110\20221109_Citrix_inventory_Master1.csv'

foreach ($device in ($Devicelist | ? { $_.fqdn -match "nsr" -and $_.State -match "Primary" -and $_.Configuration_viewable_with_Techops_login -match "Yes" })) {
    Write-Host "Attempting to connect to $($device.fqdn)"
    $SecurePassword = ConvertTo-SecureString $pass1 -AsPlainText -Force
    $Credential = New-Object System.Management.Automation.PSCredential ($user, $SecurePassword)
    $session = Connect-NetScaler -Hostname $device."IP address" -Credential $Credential -PassThru
    $nsVIPs = Get-NSLBVirtualServer | select name, ipv46, port, curstate
    $nsVIPs | Out-File C:\Users\user\Documents\20221110\VIPS\$(get-date -f yyyyMMdd)"_"$($device.fqdn)-"vips.csv"
}

Current CSV Input File format enter image description here

Current output file format

enter image description here

What I would like to out file format to be

enter image description here

**

What have I tried

**

At the moment nothing except for research, I am guessing that I will need to hold of of the info in an array and then output it at the end. I just don't have a clue how to do that.

Thanks for looking and helping

Neil

CodePudding user response:

You can change the code to first collect all data in one single variable $result and after the loop write all that out in a single CSV file.

To write csv, you need to use Export-Csv, not Out-File, which is intended to write simple text, not objects.

$user           = "user"
$pass1          = Read-Host "Enter Netscaler Password"
$SecurePassword = ConvertTo-SecureString $pass1 -AsPlainText -Force
$Credential     = New-Object System.Management.Automation.PSCredential ($user, $SecurePassword)
$today          = '{0:yyyyMMdd}' -f (Get-Date)
$Devicelist     = Import-Csv -Path 'C:\Users\user\Documents\20221110\20221109_Citrix_inventory_Master1.csv' | 
                  Where-Object { $_.fqdn -match "nsr" -and 
                                 $_.State -match "Primary" -and 
                                 $_.Configuration_viewable_with_Techops_login -match "Yes" }

$result = foreach ($device in $Devicelist) {
    Write-Host "Attempting to connect to $($device.fqdn)"
    $session = Connect-NetScaler -Hostname $device.'IP address' -Credential $Credential -PassThru
    Get-NSLBVirtualServer | 
    Select-Object name, ipv46, port, curstate, @{Name = 'Device Name'; Expression = {$device.fqdn}}
}
# now write out the csv file just once
$result | Export-Csv -Path "C:\Users\user\Documents\20221110\VIPS\$($today)_vips.csv" -NoTypeInformation
  • Related