![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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. |
| | #2 (permalink) | ||||||||||||
| 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
| ||||||||||||
| | #3 (permalink) |
| 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 |
| | #4 (permalink) | ||||||||||||||||||||||||
| 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:
| ||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||
| 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:
| ||||||||||||||||||||||||||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||||||||||||||
| | #8 (permalink) | ||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||
| | #9 (permalink) | ||||||||||||
| Guest
Posts: n/a
| Re: Synchronous Remote Execution On Sep 6, 3:44 pm, Alan <A...@xxxxxx> wrote:
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 | ||||||||||||
| |
| |
![]() |
| 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 |