Tchotchke
Tchotchke

Reputation: 399

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("System.windows.forms") | Out-Null

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

Clear-Host

# 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?

Thanks!

Upvotes: 1

Views: 230

Answers (2)

Eris
Eris

Reputation: 7638

I don't think you want to dot-source here.

Replace this line:

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

With:

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

This tells powershell to execute the script with the arguments, instead of importing it as part of the currently running script.

However, even if you did this, it would try and add the data to the argument list. Since $data is an object, and not a filename, you will probably want to use the pipeline instead:

$data | & ".\Build Software List (Test).ps1"

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174825

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

param(
    [Parameter(Mandatory=$true,Position=0)]
    [psobject[]]$Data
)

# 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

Upvotes: 1

Related Questions