3

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 argument

Would anyone be able to assist?
Thank you for your time and much appreciate any help.

param
(
    [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
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
Mike
  • 133
  • 2
  • 4
  • 13
  • Possible duplicate of [Positional Parameter error in powershell script](https://stackoverflow.com/questions/39407004/positional-parameter-error-in-powershell-script) – Jim G. May 17 '18 at 16:14

1 Answers1

4

Try this for the last line

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

Update due to comment: To show you that it is working see the GIF below - so you may check it again or insert some debug output to see where things go wrong with your script

GIF showing working solution

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
  • Thanks Dax! I updated that line and looks like it's still throwing the same "Start-Process : A positional parameter cannot be found that accepts argument" error – Mike Aug 11 '16 at 16:10
  • See my update - was my fault, didn't read all of the line :/ The path to the script is an argument to powershell.exe as well – DAXaholic Aug 11 '16 at 16:17
  • Thank you Dax, that worked perfectly! Realy appreciate the help. – Mike Aug 11 '16 at 16:32
  • Ahh, so I checked this a bit more it looks like the error is no longer being thrown but the arguments don't appear to be passing to "StopServices.ps1". In StopServices.ps1" I have another set of params and tehn some commands that does teh work. param ( [string]$targetserver = $args[0], [string]$module = $args[1] ) set-service $module -ComputerName $targetserver -StartupType Disabled -Status Stopped -PassThru – Mike Aug 11 '16 at 17:32
  • See my updated answer with the GIF showing a working sample -> there has to be something different with your scripts; insert some debug output etc. to see whether the parameters are passed and if not please check again against my sample shown in the GIF. If the parameters are passed then you have another issue within your called script :) Hope that helps – DAXaholic Aug 11 '16 at 17:48
  • Thanks again Dax. It did help alot. I ran my scripts locally making a the same call and it seems to work. It seems to not work when the files and run and stored on my remote server. – Mike Aug 11 '16 at 20:36