Mike Mike - 1 year ago 218
PowerShell Question

Passing variable arguments using PowerShell's Start-Process cmdlet

Good evening everyone,

I'm using a command line that passes arguments to as variables in the following scripts to be run in another ps1 that I'm calling from within this script. Whenever I try to pass the arguments from the command line I get the following error though

Start-Process : A positional parameter cannot be found that accepts

Would anyone be able to assist?

Thank you for your time and much appreciate any help.

[string]$targetserver = $args[0], #target server
[string]$module = $args[1], #module name

function Get-Script-Directory
return Split-Path $script:MyInvocation.MyCommand.Path

Start-Process powershell.exe (Join-Path (Get-Script-Directory) "...\StopServices.ps1") -ArgumentList $targetserver $module

Answer Source

Try this for the last line

$scriptPath = Join-Path (Get-Script-Directory) "...\StopServices.ps1"
Start-Process powershell.exe -ArgumentList "-file $scriptPath", $targetserver, $module
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download