Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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) ...

VB
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


... 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)
Posted
Updated 25-Feb-16 8:15am

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.
 
Share this answer
 
Comments
Gary Heath 25-Feb-16 9:54am    
Well I don't need any interaction, I just need to trigger the Script to run, and according to numerous articles this is perfectly doable, except I can't get anything to do it as I am constantly getting Permission Errors.

I have now tried PSexec in PStools, but I can't get it to run because I can't get the structure of the command right, so have tried Powershell, which looks and acts superb, but having set the first part of it, I am again getting Permission Errors !!!

Set-Item : Access is denied.
At line:1 char:9
+ Set-Item <<<< wsman:\localhost\client\trustedhosts Gary-PC
+ CategoryInfo : NotSpecified: (:) [Set-Item], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.SetItemCommand
Dave Kreskowiak 25-Feb-16 10:07am    
You get Access Denied because the account you're logged in as on your machine is not an admin on the remote machine.

PSEXEC has the same security and admin account limitations and the command line is not that hard. It's just "PSEXEC [\\remoteMachineName] [commandline to execute]". For example "PSEXEC \\BWB0013PC msiexec /x {GUID} /q".

You can launch Notepad but it will never show an interface on the remote machine. Open Task Manager on that machine and you'll still see it running.
Gary Heath 25-Feb-16 12:31pm    
Thanks Dave, the reason I couldn't get the PSEXEC format right was because I was inside the VBA program !!! I still get problems though, it's now giving me error 53, File Not Found using the following code ...

Sub ExecuteOnOtherMachine()

Dim strPCName As String
Dim strProgramName As String
Dim strArgument As String

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

Call Shell(strPCName & strProgramName & strArgument, vbNormalFocus)

Call Shell("""" & strPCName & """ """ & strProgramName & """ """ & strArgument & """", vbNormalFocus)

End Sub

... Both the Script commands give the same result & also if I change the strArgument to O:\CloseOutlook.vbs (so it's the actual address on the PC without the Network path) it still gives the identical error :-( ... this is driving me mad !!!
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 "


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:
VB
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)
 
Share this answer
 
Comments
Gary Heath 25-Feb-16 14:51pm    
That line with all the quotes I got online, baffling isn't it :) ... that's why I tried to do it without the quotes and yes, I am just experimenting so I am not surprised there are a multitude of errors / problems !!!

I am a programmer by trade, COBOL, so love trying to work all this stuff out but when you get error messages like "File Not Found" but the message doesn't tell you which file isn't found, I get angry, I get upset and I despair at the standards of the programming these days ... without people like yourself it just wouldn't be worth bothering !!!

I have had a couple of beers now and am just going to watch the football on TV so will give this a try tomorrow and let you know how I get on, thank you very much for all of your help :)
Gary Heath 28-Feb-16 9:01am    
I haven't looked at this for a couple of days, Dave, I've not just disappeared :-) ... will let you know how I get on.
Gary Heath 14-Mar-16 19:08pm    
I have actually abandoned this idea now, Dave, thanks for your help though, I have accepted your answers :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900