Search code examples
powershellargumentsstartup

Start() Error In Powershell (Starting a Shortcut)


I have created a Powershell script to do a timed restart on my server. This is a snippet of my code and more specifically the startServers Function.

I have added the global variables into the function in an effort to have an example.

Basically each server name is the name of a shortcut that starts an exe in a separate directory. The names are exactly that and have no .lnk after (I have tried both ways).

The aff array is for processor affinity.

This script should:

  1. For Each Server
  2. Create a New Sysem Diag Process with start info
  3. Start the process
  4. Set affinity

This is not the case: I get an error revolving around the "arg0" and it not being able to start the process. I suppose if anyone has some further explanation of what is going on with args0 and what it is would be helpful.

Code Snippet:

function startServers
{
    #Added For Example Purposes 
    $servers = @( "public", "private" ) 
    $aff = @( 196, 56 )
    #End

    $i = 0
    foreach($s in $servers) 
    {
    $app_name = "$s"
    $a = $aff[$i]
    $app_arguments = "arg0"
    $pinfo = New-Object System.Diagnostics.ProcessStartInfo
    $pinfo.Arguments = $app_arguments
    $pinfo.FileName = $app_name
    $p = New-Object System.Diagnostics.Process
    $p.StartInfo = $pinfo
    $p.Start()
    $p.ProcessorAffinity=$a
    $i = $i + 1
        #Start "$s"
        Write-Output "Starting $s"
    }
}

The Error I Get


Solution

  • function startServers {
        $i = 0
        $global:pids = @( )
    
        foreach($s in $servers)
        {
            $a = $aff[$i]
            $Starter = Start-Process -FilePath $s -WindowStyle Minimized -PassThru  
            $Process = Get-Process -Id $Starter.ID
            $global:pids += $Process.ID
            $Process.ProcessorAffinity = $a
            $CurrentTime = updateTime
            Write-Output "Starting $s at $CurrentTime"
            $i = $i + 1
            Start-Sleep -s 10
        }
        return
    }
    

    This is how I ended up solving the problem. I switched over to tracking the process by PID.

    I used -PassThru on my Start-Process and I made it a variable.

    I then made a variable that would hold the PID after it was started called Process.

    Then you do the basic $foo.ProcessorAffinity = foo and you get your result.