在Powershell中远程启动进程,在Windows中获得%ERRORLEVEL% [英] Starting a process remotely in Powershell, getting %ERRORLEVEL% in Windows

查看:272
本文介绍了在Powershell中远程启动进程,在Windows中获得%ERRORLEVEL%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景:

我正在尝试在测试开始时远程启动和停止一些性能计数器,然后在测试结束时停止它们.我正在从Win2003机器的自动化测试框架中执行此操作,该测试框架无需启动控制台即可执行命令,某些被测系统正在运行Win2008.我已经编写了脚本,以根据分配给服务器的角色来选择性能计数器.

I'm trying to start and stop some performance counters remotely at the start of a test, then stop them at the end of the test. I'm doing this from an automated test framework from a Win2003 machine, the test framework executes commands without launching a console, some of the system under test is running Win2008. I've written scripts to choose the performance counters based on roles assigned to the servers.

我的问题:

  1. logman无法在运行更高版本操作系统的计算机上启动或停止计数器.
  2. psexec可以用于远程运行logman,但是从测试框架运行时,psexec喜欢间歇性地挂起.从命令行手动运行正常.我猜这是因为调用过程没有提供控制台,或者有些类似的尴尬. (GRRRR)我对此无能为力
  3. 我编写了一个PowerShell脚本,该脚本使用WMI的win32_process远程执行logman并从批处理脚本中调用它,这很好.但是,测试框架根据%ERRORLEVEL%和stderr的内容来确定通过和失败的情况,但是WMI的win32_process不允许我访问任何一个.因此,如果计数器无法启动,则测试将继续进行,并浪费每个人的时间.
  1. logman can't start or stop counters on machines that run a later version of the OS.
  2. psexec can be used to run logman remotely, but psexec likes to hang intermittently when run from the test framework. It runs fine manually from the command line. I'm guessing that this is because the calling process doesn't provide a console, or some similar awkwardness. There's not much I can do about this (GRRRR)
  3. I wrote a PowerShell script that executes logman remotely using the WMI's win32_process and called it from a batch script, this works fine. However, the test framework decides pass and fail scenarios based on the %ERRORLEVEL% and the content of stderr, but WMI's win32_process does not give me access to either. So if the counters fail to start, the test will plough on anyway and waste everyone's time.

我正在寻找一种解决方案,该解决方案允许我在远程计算机上执行程序,检查程序的返回代码和/或将stderr传递回调用方.为了简单起见,需要使用香草Win2k3盒中提供的工具编写该文件.我真的不希望使用复杂的脚本集合,这些脚本将内容转储到日志文件中,然后再次读出来.

I'm looking for a solution that will allow me to execute a program on a remote machine, check the return code of the program and/or pipe stderr back to the caller. For reasons of simplicity, it needs to be written in tools that are available on a vanilla Win2k3 box. I'd really prefer not to use a convoluted collection of scripts that dump things into log files then reading them back out again.

有人有类似的问题可以解决吗,或者至少有建议?

Has anyone had a similar problem, and solved it, or at least have a suggestion?

推荐答案

为简单起见,它需要 用可用的工具编写 在香草Win2k3盒子上.我真的会 宁愿不要使用令人费解的 转储东西的脚本集合 进入日志文件,然后将其读回 再次出来.

For reasons of simplicity, it needs to be written in tools that are available on a vanilla Win2k3 box. I'd really prefer not to use a convoluted collection of scripts that dump things into log files then reading them back out again.

PowerShell在Windows 2003中不是本机工具.您是否仍要标记此问题PowerShell并寻找答案?无论如何,我会给您PowerShell的答案.

PowerShell isn't a native tool in Windows 2003. Do you still want to tag this question PowerShell 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)" }

这要求您在运行这些脚本的系统上使用PowerShell 2.0.您的Windows 2003远程系统实际上并不需要PowerShell.

This requires PowerShell 2.0 on the system where you are running these scripts. Your Windows 2003 remote system does not really need PowerShell.

PS:如果您需要有关WMI和PowerShell的速成课程,请阅读我的电子指南:通过PowerShell的WMI查询语言

PS: If you need a crash course on WMI and PowerShell, do read my eGuide: WMI Query Language via PowerShell

这篇关于在Powershell中远程启动进程,在Windows中获得%ERRORLEVEL%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆