0

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

2
  • Is there a relation between the source csv files? e.g.: the IP Address should match ipv46? Commented Nov 11, 2022 at 12:03
  • HI @iRon no there is no link between the IP Address (that is the management IP address) and the ipv46 (is the IP address of the VIP on the device) I hope that helps Commented Nov 11, 2022 at 12:05

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

thank you for your script. I really like the way it is laid out, very neat and concise. Unfortunately it doesn't appear to be working. It connects to all the devices as expected which is awesome however when it comes to the Export-CSV it makes the CSV but it contains no data. Any suggestions please?
@asthmatic_weasel Remove the assignment to $nsVIPs, so that the foreach will return the data to result instead.
@asthmatic_weasel Ah, yes sorry for that. As jkiiski said, I forgot to remove the $nsVIPs =. Fixed now
Awesome thank you both @Theo and jkiiski. That worked perfectly and actually helped me understand some more of PS power. @{Name = 'Device Name'; Expression = {$device.fqdn}} is an amazing bit of code. I am going to have to go away and fully understand it. I know @ makes an array (I think) and then it pulls the device name.
@Theo Thank you for that. All information is greatly appreciated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.