none
spawn powershell with a title and kicking off a script with parameters

    Question

  • I need to run several instances of a long running script in parallel in a way that they can be watched.  I'd like to have  powershellscriptA   kick off n windows of powershellscriptb.  each powershellscriptb window is running against a different host, so I'd like the title of each powershellscriptb window to have the hostname in it.   I  can open a new powershell window with invoke-item, but I can't figure out how to set the title  and kick the powershellscriptb   (with host parameter)  as I go.  Any tips?
    Wednesday, August 19, 2009 10:18 AM

Answers

All replies

  • in the script that you run in the new window to chaneg title, put a line like this (i have included the paramterblock so it can be sent from commandline)

    Powershellscriptb.ps1:

    Param($HostName)
    $Host.UI.RawUI.WindowTitle = "$HostName"


    But i am having great troubles finding a solution forhow to open a new powershell.exe, in a new windows, with paramters
    what you want to do is open the second windows with the following command:

    powershell.exe powershellb.ps1 "Server01"

    but this is not supported by invoke-item.
    my only solution right now, would be to create a .bat file, and have the above line in it, and call that with invoke-item

    Best Regards
    Jakob







    Trainer/Consultant - Coretech A/S - Blog - MCT - MCTS - VB.NET - C#.NET - Powershell - VBScript
    Wednesday, August 19, 2009 11:41 AM
  • Hi,

    There are many ways to do that but I think the easiest is the following one :

    cmd /c start powershell -NoExit -Command {$Host.UI.RawUI.WindowTitle = "$HostName"; .\myscript.ps1 'argument'}

    Hope it helps.

    Grégory Schiro - PowerShell MVP - PowerShell & MOF
    Wednesday, August 19, 2009 2:44 PM
  • $hostname is not a default automatic variable. It might be better to change this to something like this:

    cmd /c start powershell -NoExit -Command {$Host.UI.RawUI.WindowTitle = "$env:computername"; c:\fso\title.ps1 'mred'}
    Tuesday, August 25, 2009 4:51 PM
  • Another alternative is my ps1exec utility:

    http://www.westmesatech.com/misctools.html

    It lets you start a PowerShell script without a cmd.exe console. It can also set the window title and initial window state.

    HTH,

    Bill
    Tuesday, August 25, 2009 5:08 PM
  • Dude ... way cool! You Rock!
    Thursday, August 27, 2009 3:36 PM
  • You can also use this technique from start / run to launch a new instance of Windows PowerShell and pass the parameter to a script. This is seen here where title.ps1 is the name of the script, and mred is the value to pass to the script.

    powershell -NoExit -Command &{$Host.UI.RawUI.WindowTitle = "$env:computername"; c:\fso\title.ps1 'mred'}
    Thursday, August 27, 2009 5:30 PM
  • Hi IamMred,

    You are right, but getting quotes correct can be thorny (particularly when you're running a script that uses parameters that have arguments containing spaces). One of my motivations for writing ps1exec was to make it easier to schedule PowerShell scripts and not have to worry about quoting problems. Of course, this is less of a problem in PowerShell v2 (thanks to the -File parameter), but those running either version of PoSh can use ps1exec. I also like the fact that ps1exec has no console window itself (it's a GUI-mode executable).

    Regards,

    Bill
    Thursday, August 27, 2009 8:02 PM