3

I have a script that takes 2 parameters (name and location). I put the name and location into a txt file as per this post here Powershell parameters from file. I got prompted to put in value for the 2nd parameter:

Import-Csv 'C:\temp\paramtest.txt' | % { C:\temp\script\paramtest.ps1 @_ }

cmdlet paramtest.ps1 at command pipeline position 1 Supply values for the following parameters: param2:**

This is what my .txt look like:

"param","param2"
"foo","c:\temp"
"bar","c:\temp"
"foobar","c:\temp"

and the Powershell script is just plain:

Param (
    [Parameter(mandatory=$true,Position=1)]
    [string]$param, 
    [Parameter(mandatory=$true,Position=2)]
    [string]$param2
    ) 

$greeting='Hello to ' + $param + ' and ' + $param2
Write-Output $greeting

Any help is appreciated.

2
  • 1
    What version of PowerShell? Commented Jul 16, 2015 at 21:15
  • From Get-Host and $PSVersionTable, I got:Version=4. Commented Jul 17, 2015 at 12:58

1 Answer 1

4

When you import the file with Import-Csv cmdlet, you get objects of type PSCustomObject back.

The splatting operator (@) expects a hashtable, not a PSCustomObject.


PowerShell 3.0+

To import the parameters from the txt file, you could use ConvertFrom-StringData cmdlet to return them as hashtables instead:

Get-Content -Path .\test.txt -ReadCount 2 | ForEach-Object {
    $Splat = ConvertFrom-StringData $($_ -join [Environment]::NewLine)
    .\paramtest.ps1 @Splat
}

And format your text file like this:

text.txt

param=foo
param2=c:\\temp
param=bar
param2=c:\\temp
param=foobar
param2=c:\\temp

PowerShell 2.0

If you are working with PowerShell 2.0, or if you need to retain the csv format, you can do the work yourself by refencing the values from PSCustomObject into a new hashtable and splat that:

Import-Csv .\test.txt |ForEach-Object {
    $Splat = @{}
    $_.psobject.Properties |ForEach-Object { $Splat[$_.Name] = $_.Value }
    .\paramtest.ps1 @Splat
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Mathias for your suggestion. I need to read more into Splatting to understand how the above work so that I can make it easier for user. Eventually, I would have a regular user put the value into the .txt file and cut and paste the command to get this script running - so I need to KISS(Keep It Simple Simple) this procedure and script.
Thank you everyone for giving me guidance on this topic. I had gone with a different approach. I changed my code to:
@lmbNbirt If you found an alternative solution on your own, post it as an answer! :-)
sorry....run out of time for editing....."Thank you everyone for giving me guidance on this topic. I had gone with a different approach. I changed my code to: foreach ($name in get-content "paramtest.txt"){ do-somthing with $name}. The paramtest.txt is now have only one column of value. For now, this solution work, but eventually I will have to re-visit the Splatting and use function to get the script working."
@lmbNbirt As I said: post an answer, not a comment!

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.