Ryo Saeba
Ryo Saeba

Reputation: 45

Powershell passing named parameters from file

i have a problem that is troubling my mind.

I want to automatically execute powershell scripts with named arguments but within another powershell script (that will act as a script deamon).

For example:

One of the scripts that get called has this parameters

param(
[int]$version,
[string]$user,
[string]$pass,
[string]$domain,

)

The powershell script deamon now loads the file and arguments like this

$argumentsFromScript = [System.IO.File]::ReadAllText("C:\params.txt") $job = Start-Job { & "ps1file" $arguments} 

The params.txt contains the data like this

-versionInfo 2012 -user admin -pass admin -domain Workgrup

But when i try to execute this code obviously the whole $argumentsFromScript variable will be seen as parameter 1 (version) and i end up with an error, that "-versionInfo 2012 -user admin -pass admin -domain Workgrup" cannot be converted to Int32...

Do you guys have any idea how i can accomplish this task? The Powershell deamon does not know anything about the parameters. He just needs to execute scripts with given named parameters. The params.txt is just an example. Any other file (csv,ps1,xml,etc) would be fine, i just want to automatically get the named parameters passed to the script.

Thank you in advance for any help or advice..

Upvotes: 0

Views: 1474

Answers (2)

user4018458
user4018458

Reputation:

I guess you want this:

$ps1 = (Resolve-Path .\YourScript.ps1).ProviderPath
$parms = (Resolve-Path .\YourNamedParameters.txt).ProviderPath

$job = sajb -ScriptBlock {
    param($ps1,$parms) 
    iex "$ps1 $parms"
} -ArgumentList @(
    $ps1,
    [string](gc $parms)
)

# if you wanna see the outcome
rcjb $job -Wait

Upvotes: 0

mjolinor
mjolinor

Reputation: 68243

Try this:

@'
param ([string]$logname,[int]$newest)
get-eventlog -LogName $logname -Newest $newest
'@ | sc c:\testfiles\testscript.ps1

 '-logname:application -newest:10' | sc c:\testfiles\params.txt

$script = 'c:\testfiles\testscript.ps1'
$arguments = 'c:\testfiles\params.txt'

$sb = [scriptblock]::Create("$script $(get-content $argumentlist)")

Start-Job -ScriptBlock $sb

Upvotes: 1

Related Questions