Starting a process remotely in Powershell, getting% ERRORLEVEL% on Windows

A bit of background:

I am trying to start and stop some performance counters remotely at the beginning of the test, and then stop them at the end of the test. I do this from an automatic test environment from a Win2003 machine, the test environment executes commands without starting the console, some of the systems under test work with Win2008. I wrote scripts to select performance counters based on the roles assigned to the servers.

My problem (s):

  • logman cannot start or stop counters on machines that run a later version of the OS.
  • psexec can be used to run logman remotely, but psexec likes to move periodically when it starts from the test environment. It works fine manually from the command line. I assume this is because the calling process does not provide the console or some similar awkwardness. I can not do it (GRRRR)
  • I wrote a PowerShell script that remotely starts logman using WMI win32_process and is called from a script package, this works fine. However, the test environment solves the pass and fail scenarios based on %ERRORLEVEL% and the contents of stderr, but WMI win32_process does not give me access to any of them. Therefore, if the counters do not start, the test will plow anyway and spend all the time.

I am looking for a solution that will allow me to execute a program on a remote machine, check the program return code and / or pipe stderr back to the caller. For reasons of simplicity, you need to write it in the tools available in the Win32k3 box with vanilla. I would prefer not to use a collapsed collection of scripts that upload things to log files and then read them again.

Someone had a similar problem and solved it, or at least suggested it?

+4
source share
1 answer

For reasons of simplicity, you need to be written in accessible tools on the Win2k3 vanilla field. I would really prefer not to use a minimized collection of scripts that dump things into the log files and then read them again.

PowerShell is not a native tool in Windows 2003. Do you still want to mark this PowerShell question and look for an answer? Anyway, I will give you a PowerShell answer.

 $proc = Invoke-WmiMethod -ComputerName Test -Class Win32_Process -Name Create -ArgumentList "Notepad.exe" Register-WmiEvent -ComputerName test -Query "Select * from Win32_ProcessStopTrace Where ProcessID=$($proc.ProcessId)" -Action { Write-Host "Process ExitCode: $($event.SourceEventArgs.NewEvent.ExitStatus)" } 

This requires PowerShell 2.0 on the system where you use these scripts. On your remote Windows 2003 system, PowerShell is not really required.

PS: If you need a crash course in WMI and PowerShell, read my eGuide: WMI Query Language through PowerShell

+9
source

Source: https://habr.com/ru/post/1343635/


All Articles