Passing variable arguments using PowerShell's Start-Process cmdlet

自古美人都是妖i 提交于 2019-12-24 01:44:46

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/38900961/passing-variable-arguments-using-powershells-start-process-cmdlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!