Tchotchke Tchotchke - 1 year ago 77
PowerShell Question

Passing a CSV between multiple PowerShell scripts

I have a bunch of individual scripts to build various reports based off of a weekly Nessus scan. In each individual script I perform this:

Function Get-FileName($initialDirectory)
[System.Reflection.Assembly]::LoadWithPartialName("") | Out-Null

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "csv (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null


# Get the scan CSV
$inputData = Get-FileName "C:\users\$env.username\Downloads"
$data = Import-Csv $inputData

From here I do whatever work necessary in the individual script. I'd like to set it up to where I grab the file once and just pass the CSV between scripts:

$data = Import-Csv $inputData

#Call Installed Software script
". .\Build Software List (Test).ps1 $data"

#Call Most Prolific Vulnerabilities
#This continues until I've called all scripts

I've tried a few different ways to pass the CSV but none have worked yet. Can someone clue me in on what I'm missing?


Answer Source

In Build Software List (Test).ps1, make sure a positional parameter will accept the input:


# process items in $Data here

Then invoke it like:

& '.\Build Software List (Test).ps1' $Data

If you remove the spaces in the file name, you can void the call operator (&) and quotes:

.\Build-SoftwareList.ps1 $Data
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download