Windows Vista Forums

How To: Start a cmdlet without waiting for it to finish
  1. #1


    Brandon Shell Guest

    How To: Start a cmdlet without waiting for it to finish

    Goal:
    -----
    I have a process that requires that a complete a series of steps on a group
    of servers. Right now I fill this need with multiple batch/vbs scripts. I
    would like to attemp this is powershell, but I have some obsticles I need to
    work around.



    1) Check for Users on Server (easy enough)

    !!!Problem!!! -> I dont want to wait for a server to reboot to continue
    Can I launch a reboot on all of them and have a ping function that loops?
    2) Reboot Server and verify it comes back

    3) Copy crap to server (easy)
    4) Check Server against a model server (done)
    5) Use Server (done)



      My System SpecsSystem Spec

  2. #2


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    RE: How To: Start a cmdlet without waiting for it to finish

    You could simply start a new process of powershell.exe and run the Cmdlet
    inside this session. Here is an example running Get-Process Cmdlet:

    $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    $StartInfo.FileName = "$pshome\powershell.exe"
    #remove '-NoExit' to close the console after finishing
    #remove '-NoProfile' to process the PowerShell profile before running the
    Cmdlet
    $StartInfo.Arguments = "-NoExit -NoProfile -Command Get-Process"
    $StartInfo.WorkingDirectory = "C:\"
    $StartInfo.LoadUserProfile = $true
    $StartInfo.UseShellExecute = $true
    [System.Diagnostics.Process]::Start($StartInfo)


    --
    greetings
    dreeschkind

    "Brandon Shell" wrote:

    > Goal:
    > -----
    > I have a process that requires that a complete a series of steps on a group
    > of servers. Right now I fill this need with multiple batch/vbs scripts. I
    > would like to attemp this is powershell, but I have some obsticles I need to
    > work around.
    >
    > 1) Check for Users on Server (easy enough)
    >
    > !!!Problem!!! -> I dont want to wait for a server to reboot to continue
    > Can I launch a reboot on all of them and have a ping function that loops?
    > 2) Reboot Server and verify it comes back
    >
    > 3) Copy crap to server (easy)
    > 4) Check Server against a model server (done)
    > 5) Use Server (done)
    >
    >
    >


      My System SpecsSystem Spec

  3. #3


    Brandon Shell Guest

    Re: How To: Start a cmdlet without waiting for it to finish

    Can you also start functions if they are in Profile?
    "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
    news:A64D9626-6720-4E63-9CF2-0D07A74A80E8@microsoft.com...
    > You could simply start a new process of powershell.exe and run the Cmdlet
    > inside this session. Here is an example running Get-Process Cmdlet:
    >
    > $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    > $StartInfo.FileName = "$pshome\powershell.exe"
    > #remove '-NoExit' to close the console after finishing
    > #remove '-NoProfile' to process the PowerShell profile before running the
    > Cmdlet
    > $StartInfo.Arguments = "-NoExit -NoProfile -Command Get-Process"
    > $StartInfo.WorkingDirectory = "C:\"
    > $StartInfo.LoadUserProfile = $true
    > $StartInfo.UseShellExecute = $true
    > [System.Diagnostics.Process]::Start($StartInfo)
    >
    >
    > --
    > greetings
    > dreeschkind
    >
    > "Brandon Shell" wrote:
    >
    >> Goal:
    >> -----
    >> I have a process that requires that a complete a series of steps on a
    >> group
    >> of servers. Right now I fill this need with multiple batch/vbs scripts. I
    >> would like to attemp this is powershell, but I have some obsticles I need
    >> to
    >> work around.
    >>
    >> 1) Check for Users on Server (easy enough)
    >>
    >> !!!Problem!!! -> I dont want to wait for a server to reboot to continue
    >> Can I launch a reboot on all of them and have a ping function that loops?
    >> 2) Reboot Server and verify it comes back
    >>
    >> 3) Copy crap to server (easy)
    >> 4) Check Server against a model server (done)
    >> 5) Use Server (done)
    >>
    >>
    >>




      My System SpecsSystem Spec

  4. #4


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    Re: How To: Start a cmdlet without waiting for it to finish

    Yes, simply replace the Cmdlet 'Get-Process' from my example with any
    powershell script or function you like.

    --
    greetings
    dreeschkind

    "Brandon Shell" wrote:

    > Can you also start functions if they are in Profile?
    > "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
    > news:A64D9626-6720-4E63-9CF2-0D07A74A80E8@microsoft.com...
    > > You could simply start a new process of powershell.exe and run the Cmdlet
    > > inside this session. Here is an example running Get-Process Cmdlet:
    > >
    > > $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    > > $StartInfo.FileName = "$pshome\powershell.exe"
    > > #remove '-NoExit' to close the console after finishing
    > > #remove '-NoProfile' to process the PowerShell profile before running the
    > > Cmdlet
    > > $StartInfo.Arguments = "-NoExit -NoProfile -Command Get-Process"
    > > $StartInfo.WorkingDirectory = "C:\"
    > > $StartInfo.LoadUserProfile = $true
    > > $StartInfo.UseShellExecute = $true
    > > [System.Diagnostics.Process]::Start($StartInfo)
    > >
    > >
    > > --
    > > greetings
    > > dreeschkind
    > >
    > > "Brandon Shell" wrote:
    > >
    > >> Goal:
    > >> -----
    > >> I have a process that requires that a complete a series of steps on a
    > >> group
    > >> of servers. Right now I fill this need with multiple batch/vbs scripts. I
    > >> would like to attemp this is powershell, but I have some obsticles I need
    > >> to
    > >> work around.
    > >>
    > >> 1) Check for Users on Server (easy enough)
    > >>
    > >> !!!Problem!!! -> I dont want to wait for a server to reboot to continue
    > >> Can I launch a reboot on all of them and have a ping function that loops?
    > >> 2) Reboot Server and verify it comes back
    > >>
    > >> 3) Copy crap to server (easy)
    > >> 4) Check Server against a model server (done)
    > >> 5) Use Server (done)
    > >>
    > >>
    > >>

    >
    >
    >


      My System SpecsSystem Spec

  5. #5


    Karl Prosser Guest

    Re: How To: Start a cmdlet without waiting for it to finish

    i created a set up cmdlets creating a "background pipeline", however i
    haven't updated this since MSH days. i need to do that, and get the code in
    the community project.

    http://www.karlprosser.com/coder/?p=39

    i believe jim truher has recently done something quite similar but script
    based rather than C# cmdlet based.

    -Karl

    "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
    news:F87FFD64-F981-47E2-8621-84C126690BC2@microsoft.com...
    > Yes, simply replace the Cmdlet 'Get-Process' from my example with any
    > powershell script or function you like.
    >
    > --
    > greetings
    > dreeschkind
    >
    > "Brandon Shell" wrote:
    >
    >> Can you also start functions if they are in Profile?
    >> "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
    >> news:A64D9626-6720-4E63-9CF2-0D07A74A80E8@microsoft.com...
    >> > You could simply start a new process of powershell.exe and run the
    >> > Cmdlet
    >> > inside this session. Here is an example running Get-Process Cmdlet:
    >> >
    >> > $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    >> > $StartInfo.FileName = "$pshome\powershell.exe"
    >> > #remove '-NoExit' to close the console after finishing
    >> > #remove '-NoProfile' to process the PowerShell profile before running
    >> > the
    >> > Cmdlet
    >> > $StartInfo.Arguments = "-NoExit -NoProfile -Command Get-Process"
    >> > $StartInfo.WorkingDirectory = "C:\"
    >> > $StartInfo.LoadUserProfile = $true
    >> > $StartInfo.UseShellExecute = $true
    >> > [System.Diagnostics.Process]::Start($StartInfo)
    >> >
    >> >
    >> > --
    >> > greetings
    >> > dreeschkind
    >> >
    >> > "Brandon Shell" wrote:
    >> >
    >> >> Goal:
    >> >> -----
    >> >> I have a process that requires that a complete a series of steps on a
    >> >> group
    >> >> of servers. Right now I fill this need with multiple batch/vbs
    >> >> scripts. I
    >> >> would like to attemp this is powershell, but I have some obsticles I
    >> >> need
    >> >> to
    >> >> work around.
    >> >>
    >> >> 1) Check for Users on Server (easy enough)
    >> >>
    >> >> !!!Problem!!! -> I dont want to wait for a server to reboot to
    >> >> continue
    >> >> Can I launch a reboot on all of them and have a ping function that
    >> >> loops?
    >> >> 2) Reboot Server and verify it comes back
    >> >>
    >> >> 3) Copy crap to server (easy)
    >> >> 4) Check Server against a model server (done)
    >> >> 5) Use Server (done)
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>




      My System SpecsSystem Spec

  6. #6


    James Truher Guest

    Re: How To: Start a cmdlet without waiting for it to finish

    yep - I posted something last week: http://jtruher.spaces.live.com/

    a script for "background jobs"

    jim

    --
    James Truher[MSFT]
    Program Manager - Windows PowerShell
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, no confers rights.
    Visit the Windows PowerShell Team blog at:
    http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at:
    http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    "Karl Prosser" <monadzWITHOUTTHIS@gmail.com> wrote in message
    news:%23u8ASHYzGHA.744@TK2MSFTNGP05.phx.gbl...
    >i created a set up cmdlets creating a "background pipeline", however i
    >haven't updated this since MSH days. i need to do that, and get the code in
    >the community project.
    >
    > http://www.karlprosser.com/coder/?p=39
    >
    > i believe jim truher has recently done something quite similar but script
    > based rather than C# cmdlet based.
    >
    > -Karl
    >
    > "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
    > news:F87FFD64-F981-47E2-8621-84C126690BC2@microsoft.com...
    >> Yes, simply replace the Cmdlet 'Get-Process' from my example with any
    >> powershell script or function you like.
    >>
    >> --
    >> greetings
    >> dreeschkind
    >>
    >> "Brandon Shell" wrote:
    >>
    >>> Can you also start functions if they are in Profile?
    >>> "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
    >>> news:A64D9626-6720-4E63-9CF2-0D07A74A80E8@microsoft.com...
    >>> > You could simply start a new process of powershell.exe and run the
    >>> > Cmdlet
    >>> > inside this session. Here is an example running Get-Process Cmdlet:
    >>> >
    >>> > $StartInfo = new-object System.Diagnostics.ProcessStartInfo
    >>> > $StartInfo.FileName = "$pshome\powershell.exe"
    >>> > #remove '-NoExit' to close the console after finishing
    >>> > #remove '-NoProfile' to process the PowerShell profile before running
    >>> > the
    >>> > Cmdlet
    >>> > $StartInfo.Arguments = "-NoExit -NoProfile -Command Get-Process"
    >>> > $StartInfo.WorkingDirectory = "C:\"
    >>> > $StartInfo.LoadUserProfile = $true
    >>> > $StartInfo.UseShellExecute = $true
    >>> > [System.Diagnostics.Process]::Start($StartInfo)
    >>> >
    >>> >
    >>> > --
    >>> > greetings
    >>> > dreeschkind
    >>> >
    >>> > "Brandon Shell" wrote:
    >>> >
    >>> >> Goal:
    >>> >> -----
    >>> >> I have a process that requires that a complete a series of steps on a
    >>> >> group
    >>> >> of servers. Right now I fill this need with multiple batch/vbs
    >>> >> scripts. I
    >>> >> would like to attemp this is powershell, but I have some obsticles I
    >>> >> need
    >>> >> to
    >>> >> work around.
    >>> >>
    >>> >> 1) Check for Users on Server (easy enough)
    >>> >>
    >>> >> !!!Problem!!! -> I dont want to wait for a server to reboot to
    >>> >> continue
    >>> >> Can I launch a reboot on all of them and have a ping function that
    >>> >> loops?
    >>> >> 2) Reboot Server and verify it comes back
    >>> >>
    >>> >> 3) Copy crap to server (easy)
    >>> >> 4) Check Server against a model server (done)
    >>> >> 5) Use Server (done)
    >>> >>
    >>> >>
    >>> >>
    >>>
    >>>
    >>>

    >
    >




      My System SpecsSystem Spec

  7. #7


    Adam Milazzo Guest

    Re: How To: Start a cmdlet without waiting for it to finish

    James Truher wrote:
    > yep - I posted something last week: http://jtruher.spaces.live.com/
    >
    > a script for "background jobs"
    >
    > jim


    It was a great post, but you wouldn't happen to know how to solve the
    problem of the new jobs running in a default environment, with no
    variables, functions, or aliases from the current environment, would
    you? :-)

    I have a lot of helper functions defined in my profile, and it's
    somewhat of a bummer to have to expand them all inline if I want to run
    a background job.

    Not to mention that some commands that I want to run in the background
    actually depend on the existence of certain variables...

    -- Adam

      My System SpecsSystem Spec

  8. #8


    James Truher Guest

    Re: How To: Start a cmdlet without waiting for it to finish

    I followed up on the blog how to do this. Essentially, the script block can
    look like:

    new-job { . "C:\documents and settings\james\...profile.ps1; <stuff> }
    or you can add this to the new-job script (as the blog shows)



    --
    James Truher[MSFT]
    Program Manager - Windows PowerShell
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, no confers rights.
    Visit the Windows PowerShell Team blog at:
    http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at:
    http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    "Adam Milazzo" <adamm@san.rr.com> wrote in message
    news:%23gf3CWG0GHA.3656@TK2MSFTNGP04.phx.gbl...
    > James Truher wrote:
    >> yep - I posted something last week: http://jtruher.spaces.live.com/
    >>
    >> a script for "background jobs"
    >>
    >> jim

    >
    > It was a great post, but you wouldn't happen to know how to solve the
    > problem of the new jobs running in a default environment, with no
    > variables, functions, or aliases from the current environment, would you?
    > :-)
    >
    > I have a lot of helper functions defined in my profile, and it's somewhat
    > of a bummer to have to expand them all inline if I want to run a
    > background job.
    >
    > Not to mention that some commands that I want to run in the background
    > actually depend on the existence of certain variables...
    >
    > -- Adam




      My System SpecsSystem Spec

  9. #9


    Adam Milazzo Guest

    Re: How To: Start a cmdlet without waiting for it to finish

    James Truher wrote:
    > I followed up on the blog how to do this. Essentially, the script block can
    > look like:
    >
    > new-job { . "C:\documents and settings\james\...profile.ps1; <stuff> }
    > or you can add this to the new-job script (as the blog shows)


    Yeah, I can do this. I'm already doing something similar to preserve the
    current working directory...

    A problem is that I function "setupEnlistment" that, given a project
    root path, configures the shell for one of the several project
    enlistments I have (and it does so by setting the path, scanning for
    enlistment files, setting a bunch of environment and shell variables,
    and some other things...)

    I'd also have to call "setupEnlistment" with the right path after
    re-running the profile. I guess I can get 90% of the way there with this
    approach, though.

    One question -- is there a function that escapes a string so that it can
    be inserted literally into a PowerShell string? So that when I'm
    constructing strings of PowerShell code like this, I can be confident
    that a string containing a quote, dollar sign, backtick, etc, won't
    break the code generation?

      My System SpecsSystem Spec

How To: Start a cmdlet without waiting for it to finish problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
CTP3, new cmdlet Start-Process issue Roman Kuzmin PowerShell 4 30 Dec 2008
VBS waiting for program to finish hb21l6 VB Script 3 19 Jul 2008
waiting for function/process to finish... Daz VB Script 6 30 Jun 2008
Invoking Cmdlet Get-Location from cmdlet,cant get Currnt Directory Vikram PowerShell 2 05 Jun 2008
I get a message to insert disk in drive to finish installing a program everytime I start the computer.Please read none Vista performance & maintenance 0 07 Mar 2008