powershell script reading parameters from txt -
i have script takes 2 parameters (name , location). put name , location txt file per post here powershell parameters file. got prompted put in value 2nd parameter:
import-csv 'c:\temp\paramtest.txt' | % { c:\temp\script\paramtest.ps1 @_ }
cmdlet paramtest.ps1 @ command pipeline position 1 supply values following parameters: param2:**
this .txt like:
"param","param2" "foo","c:\temp" "bar","c:\temp" "foobar","c:\temp"
and powershell script plain:
param ( [parameter(mandatory=$true,position=1)] [string]$param, [parameter(mandatory=$true,position=2)] [string]$param2 ) $greeting='hello ' + $param + ' , ' + $param2 write-output $greeting
any appreciated.
when import file import-csv
cmdlet, objects of type pscustomobject
back.
the splatting operator (@
) expects hashtable, not pscustomobject
.
powershell 3.0+
to import parameters txt file, use convertfrom-stringdata
cmdlet return them hashtables instead:
get-content -path .\test.txt -readcount 2 | foreach-object { $splat = convertfrom-stringdata $($_ -join [environment]::newline) .\paramtest.ps1 @splat }
and format text file this:
text.txt
param=foo param2=c:\\temp param=bar param2=c:\\temp param=foobar param2=c:\\temp
powershell 2.0
if working powershell 2.0, or if need retain csv format, can work refencing values pscustomobject
new hashtable , splat that:
import-csv .\test.txt |foreach-object { $splat = @{} $_.psobject.properties |foreach-object { $splat[$_.name] = $_.value } .\paramtest.ps1 @splat }
Comments
Post a Comment