如何使用wmi在另一台PC上运行脚本 [英] How do i run scripts on another pc using wmi

查看:102
本文介绍了如何使用wmi在另一台PC上运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的笔记本电脑设置为与我如何使用桌面完全相同。大部分事情都要计划,我正在使用利马设备,直到现在已经克服了所有问题。



但是,我现在正在尝试同步我的E:Mails和无法使MS在此处编写的WMI指令的头部或尾部连接到WMI在远程计算机上(Windows) [ ^ ] ...



在我的笔记本电脑和桌面上,我在Outlook Startup中有VBA代码,用于检查Outlook目前正在另一台机器上运行,如果是,那么我想在另一台机器上运行一个脚本来退出它,但这就是我遇到问题的地方。



Outlook正在我的笔记本电脑上运行,所以当我在桌面上启动它时,我想运行关闭它的脚本笔记本电脑,代码如下(实际上这不是我的最终代码,这只是一个测试得到的东西,在这种情况下是记事本,在另一台机器上运行)...



I am trying to set my Laptop up to use identically as to how I use my Desktop. Mostly things are going to plan, I am using a Lima Device and until now have overcome all problems.

However, I am now trying to sync my E:Mails and cannot make head nor tail of the WMI instructions that MS have written here Connecting to WMI on a Remote Computer (Windows)[^] ...

On both my Laptop and my Desktop I have VBA code in the Outlook Startup that checks if Outlook is currently running on the other machine and if it is then I want to run a Script on that other machine to exit it, but this is where I am hitting the problem.

Outlook is running on my Laptop, so when I start it on the Desktop I want to run the Script that closes it on the Laptop, the code is as follows (actually this is NOT my final code, this is just a test to get something, in this case Notepad, to run on the other machine) ...

Sub ExecuteOnOtherMachine()

strComputer = "GARYLAPTOP-PC"
strCommand = "notepad.exe"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")

errReturn = objProcess.Create(strCommand, Null, Null, intProcessID)

If errReturn = 0 Then
  Wscript.Echo "notepad.exe was started with a process ID: " & intProcessID
Else
  Wscript.Echo "notepad.exe could not be started due to error: " & errReturn
End If

End Sub





...但是,在设置objWMIService 行我收到错误,运行时错误70,权限被拒绝 ...我认为这与WMI连接和防火墙规则有关,但是他们似乎是为专家写的,对于像我这样的业余程序员来说根本没有意义!



任何人都可以用简单的基本英语解释我需要做些什么来让这两台运行Windows 7的PC在另一台上执行程序PC(我要运行的程序总是存储在另一台PC上)?!?



我尝试过的方法:



我从MS找到了这个故障排除页面,但它又不适合那些胆小的人... https://technet.microsoft.com/en-us/library/ ff406382.aspx#H22



我允许WMI通过防火墙进行通信(私有)



... However, on the Set objWMIService line I get an error, Run-Time error 70, Permission Denied ... I assume this is connected with the WMI connection and Firewall rules but they seem to be written for experts, it makes no sense at all for an amateur programmer like myself !!!

Can anybody please explain, in plain, basic English, what I need to do to allow these 2 PCs, both running Windows 7, to execute a program on the other PC (the program I want to run is always stored on the other PC) ?!?

What I have tried:

I have found this Troubleshooting page from MS but again it is not for the faint-hearted ... https://technet.microsoft.com/en-us/library/ff406382.aspx#H22

I have allowed WMI to communicate through the Firewall (Private)

推荐答案

您无法在远程计算机上启动用户交互式进程。好吧,你可以,但登录远程机器的用户永远不会看到它出现。这是因为我认为安全问题在您考虑时会变得非常明显。



为了启动远程进程(您的脚本)您的本地帐户必须具有远程计算机的管理员权限,通常这意味着两台计算机必须位于同一个域中。工作组配置有更难的时间,因为两台机器都不信任另一台机器的用户帐户。



远程运行的脚本以启动它的管理员用户身份运行,而不是用户登录远程计算机。



您的脚本可以终止Outlook,虽然它不能保证工作,也不会很好地关闭Outlook。 Outlook会抱怨下次启动它时没有正确关闭,它可能会对它加载的.PST文件进行修复。
You can NOT launch a user interactive process on a remote machine. Well, you can, but the user logged in on the remote machine will never see it appear. This is because of security concerns that I think become quite obvious when you think about it.

In order for you to launch a remote process (your script) your local account MUST have admin permissions on the remote machine, typically this means that both machines must be in the same domain. Workgroup configurations have a much harder time with this because neither machine trusts the user accounts of the other.

The script that is running remotely runs as the admin user that launched it, NOT as the user logged in on the remote machine.

Your script CAN terminate Outlook, though it is NOT guaranteed to work nor will it shutdown Outlook nicely. Outlook will complain about not being shutdown properly the next time you launch it and it'll probably do a repair on the .PST file it loads.


很多问题。首先,你炮轰的命令行是完全错误的。这正是你的运行:

Tons of problems. First, the command line you shelled is completely wrong. This is exactly what you ran:
"\\Gary-PC " "C:\Program Files\PS Tools\PsExec.exe " "\\GARY-PC\GaryPC - O\CloseOutlook.vbs "





首先,您拥有PC名称,什么都不做,除了抛出找不到文件的错误。



接下来,你有了PSEXEC本地副本的路径。它是你的机器本地的不是吗?



你的所有地方都有引号,这使得代码更难以阅读和调试。我建议使用Chr(34)代替双引号。这可能是一个更多的打字,但使代码更容易调试。



然后你有一些路径到你试图运行的.VBS。你有错误顺序的命令行的部分!



帮自己一个忙,并将命令行组装成一个变量,这样你就可以检查它的内容了你试图破壳:



First, you have the PC name, which does nothing at all, except throw the file not found error.

Next, you have the path to a local copy of PSEXEC. It IS local to your machine isn't it?

You've got quote marks all over the place which just makes the code harder to read and debug. I suggest using Chr(34) in place of the double quotes. It may be a little more typing but makes code easier to debug.

Then you've got some path to the .VBS you're trying to run. You've got the pieces of the command line in the wrong order!

Do yourself a favor and assemble the command line into a variable so you can examine it's contents before you try to Shell the thing:

Dim strPCName, strProgramName, strArgument, cmdLine

strPCName = "\\Gary-PC"
strProgramName = "C:\Program Files\PS Tools\PsExec.exe"
strArgument = "\\GARY-PC\GaryPC - O\CloseOutlook.vbs"

cmdLine = Chr(34) & strProgramName & Chr(34) & " " & strPCName & " " & Chr(34) & strArgument & Chr(34)

Shell(cmdLine, vbNormalFocus)


这篇关于如何使用wmi在另一台PC上运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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