1

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!

Tchotchke
  • 399
  • 1
  • 2
  • 18

2 Answers2

1

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
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Mathias, you're 2/2 on answering my PS questions. Thank you again for the prompt response! quick follow up question: In my code I used double quotes, not single. Doesn't seem to make a difference here. Is there a difference? – Tchotchke Aug 18 '16 at 19:30
  • @Tchotchke Happy to help :) Not in this case, no. The difference between using `"` and `'` is that the first is *expandable* (ie. you can put variables in there, and the parser will replace them with their values). `'` on the other hand produces a verbatim string. Since there are no variable reference or subexpressions in the file name, it makes no difference in this case – Mathias R. Jessen Aug 18 '16 at 19:37
0

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"
Eris
  • 7,378
  • 1
  • 30
  • 45