Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Synchronous Remote Execution

Closed Thread
 
Thread Tools Display Modes
Old 09-06-2007   #1 (permalink)
Alan
Guest
 
Posts: n/a

Synchronous Remote Execution

I am using wmi to remotely run executables and batch files. I can run them
just fine but i can't block or wait until the executable or batch file is
completed with an exit code, so i dont know if (a) whether or not my
executable or batch file completed without errors and (b) what the exit code
is. My questions are [1] Is there a way to wait for remote executables and
batch files to complete before moving on? [2] if so, How can we return the
exit code from the executable or batch file to powershell?

Thanks in advance for the help.
 
Old 09-06-2007   #2 (permalink)
Brandon Shell
Guest
 
Posts: n/a

Re: Synchronous Remote Execution

Two things...

One, I recommend pinging before you do any WMI connections. I use this
function Ping-Server {
Param([string]$server)
$pingresult = Get-WmiObject win32_pingstatus -f "address='$Server'"
if($pingresult.statuscode -eq 0) {$true} else {$false}
}

Two, another option is to use Runspaces or System.Diagnostics.Process
You can use runspaces as illustrated here
http://jtruher.spaces.live.com/blog/...628D!130.entry
or System.Diagnostics.Process as illustrated here
http://blogs.msdn.com/powershell/arc...owershell.aspx

Also... the exit code should have been returned to powershell by the cmd and
would be considered output.

"Alan" <Alan@xxxxxx> wrote in message
news:0AB7BF8F-4B84-4BEB-BC5E-3A59D1B054CE@xxxxxx
Quote:

>I am using wmi to remotely run executables and batch files. I can run them
> just fine but i can't block or wait until the executable or batch file is
> completed with an exit code, so i dont know if (a) whether or not my
> executable or batch file completed without errors and (b) what the exit
> code
> is. My questions are [1] Is there a way to wait for remote executables
> and
> batch files to complete before moving on? [2] if so, How can we return the
> exit code from the executable or batch file to powershell?
>
> Thanks in advance for the help.
 
Old 09-06-2007   #3 (permalink)
Kiron
Guest
 
Posts: n/a

Re: Synchronous Remote Execution

# Use Out-Null to wait for execution to complete.

<command> | out-null

# PowerShell has $? and $LASTEXITCODE variables

Help about_automatic_variables

$?
Contains True if last operation succeeded and False otherwise.

$LASTEXITCODE
Contains the exit code of the last Win32 executable execution.

--
Kiron

 
Old 09-06-2007   #4 (permalink)
Alan
Guest
 
Posts: n/a

Re: Synchronous Remote Execution

Ok i wrote a execute process method that creates a new process by executing
my executable or batch file that i give as parameter:

function global:ExecuteProcess([string]$computer, [string]$fullPath=$(throw
"Check parameters: arg1:machinename arg2:exeFullPath."))
{
$newProcess = new-object System.Management.ManagementClass
"\\$computer\root\cimv2:win32_process"

$ret = $newProcess.Create($fullPath)
exit $ret.returnvalue
}

When $newProcess.Create($fullPath) executes i just get a return code of
whether the process was created or not. I need the exit code of the
executable not whether or not the process was created or not. Is there
another way with WMI to execute process remotely?

If i use System.Diagnostics.Process i can't create a new process remotly.

-alan


"Brandon Shell" wrote:
Quote:

> Two things...
>
> One, I recommend pinging before you do any WMI connections. I use this
> function Ping-Server {
> Param([string]$server)
> $pingresult = Get-WmiObject win32_pingstatus -f "address='$Server'"
> if($pingresult.statuscode -eq 0) {$true} else {$false}
> }
>
> Two, another option is to use Runspaces or System.Diagnostics.Process
> You can use runspaces as illustrated here
> http://jtruher.spaces.live.com/blog/...628D!130.entry
> or System.Diagnostics.Process as illustrated here
> http://blogs.msdn.com/powershell/arc...owershell.aspx
>
> Also... the exit code should have been returned to powershell by the cmd and
> would be considered output.
>
> "Alan" <Alan@xxxxxx> wrote in message
> news:0AB7BF8F-4B84-4BEB-BC5E-3A59D1B054CE@xxxxxx
Quote:

> >I am using wmi to remotely run executables and batch files. I can run them
> > just fine but i can't block or wait until the executable or batch file is
> > completed with an exit code, so i dont know if (a) whether or not my
> > executable or batch file completed without errors and (b) what the exit
> > code
> > is. My questions are [1] Is there a way to wait for remote executables
> > and
> > batch files to complete before moving on? [2] if so, How can we return the
> > exit code from the executable or batch file to powershell?
> >
> > Thanks in advance for the help.
>
>
 
Old 09-06-2007   #5 (permalink)
Shay Levi
Guest
 
Posts: n/a

Re: Synchronous Remote Execution

> exit $ret.returnvalue

You're calling on exit prior to return value

function stam{
"before exit"
exit
"after exit"
}

now if you execute "stam", your console will be closed and the script will
terminate, try it.


Shay
http://scriptolog.blogspot.com


Quote:

> Ok i wrote a execute process method that creates a new process by
> executing my executable or batch file that i give as parameter:
>
> function global:ExecuteProcess([string]$computer,
> [string]$fullPath=$(throw
> "Check parameters: arg1:machinename arg2:exeFullPath."))
> {
> $newProcess = new-object System.Management.ManagementClass
> "\\$computer\root\cimv2:win32_process"
> $ret = $newProcess.Create($fullPath)
> exit $ret.returnvalue
> }
> When $newProcess.Create($fullPath) executes i just get a return code
> of whether the process was created or not. I need the exit code of
> the executable not whether or not the process was created or not. Is
> there another way with WMI to execute process remotely?
>
> If i use System.Diagnostics.Process i can't create a new process
> remotly.
>
> -alan
>
> "Brandon Shell" wrote:
>
Quote:

>> Two things...
>>
>> One, I recommend pinging before you do any WMI connections. I use
>> this
>> function Ping-Server {
>> Param([string]$server)
>> $pingresult = Get-WmiObject win32_pingstatus -f "address='$Server'"
>> if($pingresult.statuscode -eq 0) {$true} else {$false}
>> }
>> Two, another option is to use Runspaces or System.Diagnostics.Process
>> You can use runspaces as illustrated here
>> http://jtruher.spaces.live.com/blog/...628D!130.entry
>> or System.Diagnostics.Process as illustrated here
>> http://blogs.msdn.com/powershell/arc...aging-processe
>> s-in-powershell.aspx
>> Also... the exit code should have been returned to powershell by the
>> cmd and would be considered output.
>>
>> "Alan" <Alan@xxxxxx> wrote in message
>> news:0AB7BF8F-4B84-4BEB-BC5E-3A59D1B054CE@xxxxxx
>>
Quote:

>>> I am using wmi to remotely run executables and batch files. I can
>>> run them
>>> just fine but i can't block or wait until the executable or batch
>>> file is
>>> completed with an exit code, so i dont know if (a) whether or not my
>>> executable or batch file completed without errors and (b) what the
>>> exit
>>> code
>>> is. My questions are [1] Is there a way to wait for remote
>>> executables
>>> and
>>> batch files to complete before moving on? [2] if so, How can we
>>> return the
>>> exit code from the executable or batch file to powershell?
>>> Thanks in advance for the help.
>>>

 
Old 09-06-2007   #6 (permalink)
Alan
Guest
 
Posts: n/a

Re: Synchronous Remote Execution

Yes i know i am exiting with the return value of whether the process was
created. i want to exit with the exit code of the executable

"Shay Levi" wrote:
Quote:
Quote:

> > exit $ret.returnvalue
>
> You're calling on exit prior to return value
>
> function stam{
> "before exit"
> exit
> "after exit"
> }
>
> now if you execute "stam", your console will be closed and the script will
> terminate, try it.
>
>
> Shay
> http://scriptolog.blogspot.com
>
>
>
Quote:

> > Ok i wrote a execute process method that creates a new process by
> > executing my executable or batch file that i give as parameter:
> >
> > function global:ExecuteProcess([string]$computer,
> > [string]$fullPath=$(throw
> > "Check parameters: arg1:machinename arg2:exeFullPath."))
> > {
> > $newProcess = new-object System.Management.ManagementClass
> > "\\$computer\root\cimv2:win32_process"
> > $ret = $newProcess.Create($fullPath)
> > exit $ret.returnvalue
> > }
> > When $newProcess.Create($fullPath) executes i just get a return code
> > of whether the process was created or not. I need the exit code of
> > the executable not whether or not the process was created or not. Is
> > there another way with WMI to execute process remotely?
> >
> > If i use System.Diagnostics.Process i can't create a new process
> > remotly.
> >
> > -alan
> >
> > "Brandon Shell" wrote:
> >
Quote:

> >> Two things...
> >>
> >> One, I recommend pinging before you do any WMI connections. I use
> >> this
> >> function Ping-Server {
> >> Param([string]$server)
> >> $pingresult = Get-WmiObject win32_pingstatus -f "address='$Server'"
> >> if($pingresult.statuscode -eq 0) {$true} else {$false}
> >> }
> >> Two, another option is to use Runspaces or System.Diagnostics.Process
> >> You can use runspaces as illustrated here
> >> http://jtruher.spaces.live.com/blog/...628D!130.entry
> >> or System.Diagnostics.Process as illustrated here
> >> http://blogs.msdn.com/powershell/arc...aging-processe
> >> s-in-powershell.aspx
> >> Also... the exit code should have been returned to powershell by the
> >> cmd and would be considered output.
> >>
> >> "Alan" <Alan@xxxxxx> wrote in message
> >> news:0AB7BF8F-4B84-4BEB-BC5E-3A59D1B054CE@xxxxxx
> >>
> >>> I am using wmi to remotely run executables and batch files. I can
> >>> run them
> >>> just fine but i can't block or wait until the executable or batch
> >>> file is
> >>> completed with an exit code, so i dont know if (a) whether or not my
> >>> executable or batch file completed without errors and (b) what the
> >>> exit
> >>> code
> >>> is. My questions are [1] Is there a way to wait for remote
> >>> executables
> >>> and
> >>> batch files to complete before moving on? [2] if so, How can we
> >>> return the
> >>> exit code from the executable or batch file to powershell?
> >>> Thanks in advance for the help.
> >>>
>
>
>
 
Old 09-06-2007   #7 (permalink)
Shay Levi
Guest
 
Posts: n/a

Re: Synchronous Remote Execution

I dont think you can. Also note that this processes will run in background
of the target computer, they wont
be visible locally on the target. You can make your process write its status
to a shared dir (i.e., text file)
and monitor that file. Anyway, only two properties are availa

PS C:\Scripts> ([wmiClass]"\\.\ROOT\CIMV2:win32_process").Create("calc")
| fl *


__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ProcessId : 1872
ReturnValue : 0


Shay
http://scriptolog.blogspot.com


Quote:

> Yes i know i am exiting with the return value of whether the process
> was created. i want to exit with the exit code of the executable
>
> "Shay Levi" wrote:
>
Quote:
Quote:

>>> exit $ret.returnvalue
>>>
>> You're calling on exit prior to return value
>>
>> function stam{
>> "before exit"
>> exit
>> "after exit"
>> }
>> now if you execute "stam", your console will be closed and the script
>> will terminate, try it.
>>
>> Shay
>> http://scriptolog.blogspot.com
Quote:

>>> Ok i wrote a execute process method that creates a new process by
>>> executing my executable or batch file that i give as parameter:
>>>
>>> function global:ExecuteProcess([string]$computer,
>>> [string]$fullPath=$(throw
>>> "Check parameters: arg1:machinename arg2:exeFullPath."))
>>> {
>>> $newProcess = new-object System.Management.ManagementClass
>>> "\\$computer\root\cimv2:win32_process"
>>> $ret = $newProcess.Create($fullPath)
>>> exit $ret.returnvalue
>>> }
>>> When $newProcess.Create($fullPath) executes i just get a return code
>>> of whether the process was created or not. I need the exit code of
>>> the executable not whether or not the process was created or not.
>>> Is
>>> there another way with WMI to execute process remotely?
>>> If i use System.Diagnostics.Process i can't create a new process
>>> remotly.
>>>
>>> -alan
>>>
>>> "Brandon Shell" wrote:
>>>
>>>> Two things...
>>>>
>>>> One, I recommend pinging before you do any WMI connections. I use
>>>> this
>>>> function Ping-Server {
>>>> Param([string]$server)
>>>> $pingresult = Get-WmiObject win32_pingstatus -f "address='$Server'"
>>>> if($pingresult.statuscode -eq 0) {$true} else {$false}
>>>> }
>>>> Two, another option is to use Runspaces or
>>>> System.Diagnostics.Process
>>>> You can use runspaces as illustrated here
>>>> http://jtruher.spaces.live.com/blog/...628D!130.entry
>>>> or System.Diagnostics.Process as illustrated here
>>>> http://blogs.msdn.com/powershell/arc...anaging-proces
>>>> se
>>>> s-in-powershell.aspx
>>>> Also... the exit code should have been returned to powershell by
>>>> the
>>>> cmd and would be considered output.
>>>> "Alan" <Alan@xxxxxx> wrote in message
>>>> news:0AB7BF8F-4B84-4BEB-BC5E-3A59D1B054CE@xxxxxx
>>>>
>>>>> I am using wmi to remotely run executables and batch files. I can
>>>>> run them
>>>>> just fine but i can't block or wait until the executable or batch
>>>>> file is
>>>>> completed with an exit code, so i dont know if (a) whether or not
>>>>> my
>>>>> executable or batch file completed without errors and (b) what the
>>>>> exit
>>>>> code
>>>>> is. My questions are [1] Is there a way to wait for remote
>>>>> executables
>>>>> and
>>>>> batch files to complete before moving on? [2] if so, How can we
>>>>> return the
>>>>> exit code from the executable or batch file to powershell?
>>>>> Thanks in advance for the help.

 
Old 09-06-2007   #8 (permalink)
Shay Levi
Guest
 
Posts: n/a

Re: Synchronous Remote Execution

Sorry, I hit the send key accidentally.

I dont think you can. Also note that this processes will run in background
of the target computer, they wont
be visible locally on the target, you also need this remote processes to
exit on completion (check the remote
computer task manager).

You can make your ExecuteProcess write its status to text file in a shared
directory
or to an eventlog and monitor that file or log.

PS C:\Scripts> ([wmiClass]"\\.\ROOT\CIMV2:win32_process").Create("calc")
| fl *


__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ProcessId : 1872
ReturnValue : 0


Shay
http://scriptolog.blogspot.com


Quote:

> I dont think you can. Also note that this processes will run in
> background
> of the target computer, they wont
> be visible locally on the target. You can make your process write its
> status
> to a shared dir (i.e., text file)
> and monitor that file. Anyway, only two properties are availa
> PS C:\Scripts>
> ([wmiClass]"\\.\ROOT\CIMV2:win32_process").Create("calc") | fl *
>
> __GENUS : 2
> __CLASS : __PARAMETERS
> __SUPERCLASS :
> __DYNASTY : __PARAMETERS
> __RELPATH :
> __PROPERTY_COUNT : 2
> __DERIVATION : {}
> __SERVER :
> __NAMESPACE :
> __PATH :
> ProcessId : 1872
> ReturnValue : 0
> Shay
> http://scriptolog.blogspot.com
Quote:

>> Yes i know i am exiting with the return value of whether the process
>> was created. i want to exit with the exit code of the executable
>>
>> "Shay Levi" wrote:
>>
Quote:

>>>> exit $ret.returnvalue
>>>>
>>> You're calling on exit prior to return value
>>>
>>> function stam{
>>> "before exit"
>>> exit
>>> "after exit"
>>> }
>>> now if you execute "stam", your console will be closed and the
>>> script
>>> will terminate, try it.
>>> Shay
>>> http://scriptolog.blogspot.com
>>>> Ok i wrote a execute process method that creates a new process by
>>>> executing my executable or batch file that i give as parameter:
>>>>
>>>> function global:ExecuteProcess([string]$computer,
>>>> [string]$fullPath=$(throw
>>>> "Check parameters: arg1:machinename arg2:exeFullPath."))
>>>> {
>>>> $newProcess = new-object System.Management.ManagementClass
>>>> "\\$computer\root\cimv2:win32_process"
>>>> $ret = $newProcess.Create($fullPath)
>>>> exit $ret.returnvalue
>>>> }
>>>> When $newProcess.Create($fullPath) executes i just get a return
>>>> code
>>>> of whether the process was created or not. I need the exit code of
>>>> the executable not whether or not the process was created or not.
>>>> Is
>>>> there another way with WMI to execute process remotely?
>>>> If i use System.Diagnostics.Process i can't create a new process
>>>> remotly.
>>>> -alan
>>>>
>>>> "Brandon Shell" wrote:
>>>>
>>>>> Two things...
>>>>>
>>>>> One, I recommend pinging before you do any WMI connections. I use
>>>>> this
>>>>> function Ping-Server {
>>>>> Param([string]$server)
>>>>> $pingresult = Get-WmiObject win32_pingstatus -f
>>>>> "address='$Server'"
>>>>> if($pingresult.statuscode -eq 0) {$true} else {$false}
>>>>> }
>>>>> Two, another option is to use Runspaces or
>>>>> System.Diagnostics.Process
>>>>> You can use runspaces as illustrated here
>>>>> http://jtruher.spaces.live.com/blog/...628D!130.entry
>>>>> or System.Diagnostics.Process as illustrated here
>>>>> http://blogs.msdn.com/powershell/arc...managing-proce
>>>>> s
>>>>> se
>>>>> s-in-powershell.aspx
>>>>> Also... the exit code should have been returned to powershell by
>>>>> the
>>>>> cmd and would be considered output.
>>>>> "Alan" <Alan@xxxxxx> wrote in message
>>>>> news:0AB7BF8F-4B84-4BEB-BC5E-3A59D1B054CE@xxxxxx
>>>>>> I am using wmi to remotely run executables and batch files. I
>>>>>> can
>>>>>> run them
>>>>>> just fine but i can't block or wait until the executable or batch
>>>>>> file is
>>>>>> completed with an exit code, so i dont know if (a) whether or not
>>>>>> my
>>>>>> executable or batch file completed without errors and (b) what
>>>>>> the
>>>>>> exit
>>>>>> code
>>>>>> is. My questions are [1] Is there a way to wait for remote
>>>>>> executables
>>>>>> and
>>>>>> batch files to complete before moving on? [2] if so, How can we
>>>>>> return the
>>>>>> exit code from the executable or batch file to powershell?
>>>>>> Thanks in advance for the help.

 
Old 09-07-2007   #9 (permalink)
Oisin Grehan
Guest
 
Posts: n/a

Re: Synchronous Remote Execution

On Sep 6, 3:44 pm, Alan <A...@xxxxxx> wrote:
Quote:

> Yes i know i am exiting with the return value of whether the process was
> created. i want to exit with the exit code of the executable
>
If you start a remote win32 executable like calc.exe, it will
immediately return and you will not get an exit code. However, without
getting into details, look at this:

C:\>cmd /c start /wait calc.exe

( ... at this point, calc.exe starts and the c:\> prompt does not
return until calc.exe is closed ... )

C:\>echo %errorlevel%
0

C:\>cmd /c start /wait doesnotexist.exe
The system cannot find the file doesnotexist.exe.

( immediately returns)

C:\>echo %errorlevel%
1

It's not powershell, but I think you can probably work out the rest
given this pattern and by using Wmi Win32_Proces::Create

Hope this helps,

- Oisin

 
 
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Intel CPU Bugs allow Remote Code Execution dmex Vista News 12 2 Weeks Ago 07:56 AM
Optimize Execution Lorin Vista General 0 02-20-2008 10:49 AM
Execution problem Tcs PowerShell 4 09-01-2007 11:50 PM
Remote Desktop Connection - Data Execution Prevention - Failure Mike from Connecticut (CEA) Vista General 0 07-11-2007 09:52 AM
execution policy - local|remote Fil PowerShell 15 12-01-2006 07:23 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49