0

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..

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

That sadly lead to the same kind of exception, but i know the idea behind your effort. Thank you
Feel free to use whatever you want, but I've just ran it again, and it runs just fine.

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.