How To Start A Remote Computer Repair Service
How Can I Use Windows PowerShell to Outset a Service on a Remote Reckoner?
Hey, Scripting Guy! How can I use Windows PowerShell to kickoff a service on a remote estimator?
— NA
Hey, NA. Y'all know, a few years ago a professor named Francis Fukuyama wrote a volume called The End of History and the Last of Homo, a volume which stated (well, sort of) that at present that the Common cold War was over nothing peculiarly interesting and exciting would e'er happen again. What do the Scripting Guys have to say to Dr. Fukuyama? Just this: ha!
Why are the Scripting Guys (of all people!) willing to challenge one of the foremost intellects of our generation? Well, for ane thing, nosotros're willing to bet that Francis Fukuyama doesn't read this column very oftentimes. More than important, however, is the fact that we have proof that interesting and exciting things withal exercise happen. Later on all, not only are nosotros in the midst of the Hey, Scripting Guy! 500 th column commemoration, but now we're answering our first-e'er Windows PowerShell question. Take that, Francis Fukuyama!
Note. Y'all say you lot've never fifty-fifty heard of Windows PowerShell? You say you lot have no idea what you can even do with Windows PowerShell? Nosotros say y'all should take a await at the new Script Center series What Tin I Practice With Windows PowerShell? |
Every bit you noted, NA, you lot're trying to utilize the Start-Service cmdlet to restart a service on a remote computer, something like this (for educational purposes, we've simplified your script a little):
Get-WmiObject -estimator atl-fs-01 Win32_Service -Filter "Name='Alerter'" | Commencement-Service
In other words, you're trying to connect to the Alerter service on the reckoner atl-fs-01, and then "pipage" that object to the Showtime-Service cmdlet. Equally you noted, you're able to connect to the Alerter service on the remote computer without whatever trouble. When yous call Kickoff-Service, withal, the command fails, even though the Start-Service cmdlet works just fine on your local computer.
And that's the trouble right there. Windows PowerShell is an interesting and powerful new engineering, merely (at the fourth dimension we're writing this cavalcade of grade) it'southward still in beta; among other things, that ways that some of the features that are expected to appear in the released version of the product aren't ready just nevertheless. Unfortunately, 1 of those features is the ability to run cmdlets against remote computers. Anytime you should be able to beginning a service on a remote computer using a control similar to this:
Kickoff-Service alerter -reckoner atl-fs-01
But non today.
So and then why are you able to get information near the Alerter service on the remote computer? Well, as it turns out, the Get-WMIObject cmdlet is an exception to the rule: because it piggybacks onto WMI, Get-WMIObject can do anything WMI can do. That'southward why Go-WMIObject can operate confronting remote computers: rather than relying on Windows PowerShell cmdlets to brand the connectedness and comport out its tasks Get-WMIObject uses WMI to take intendance of concern.
A general rule of thumb: you can use Go-WMIObject to work with remote computers, providing, of course, that you're using WMI backdrop and methods. Otherwise, you lot're limited to working against the local computer when using Windows PowerShell. (And, aye, that will modify, although we can't say for sure when remote figurer capabilities will be added to Windows PowerShell.)
So does that mean we can use Go-WMIObject and a standard WMI method (like, say, StartService) to get-go a service on a remote figurer? As a matter of fact information technology does, although fifty-fifty here at that place's a chip of a trick involved. Let's testify you the Windows PowerShell script/control that volition start a service on a remote figurer, and so run into if we can explain how information technology works:
(Get-WmiObject -computer atl-fs-01 Win32_Service -Filter "Proper noun='Alerter'").InvokeMethod`
("StartService",$null)
There are really ii parts to this control, the part that connects to the Alerter service and the function that starts the service. Let's start with Part 1, the part that connects the states to the WMI service:
(Get-WmiObject -estimator atl-fs-01 Win32_Service -Filter "Name='Alerter'")
What we're doing hither is connecting to the WMI service on the computer atl-fs-01 and retrieving all instances of the Win32_Service class where the Proper name property is equal to Alerter. What we take hither is equivalent to the post-obit VBScript code:
Set objWMIService = GetObject("winmgmts:\\atl-fs-01") Fix colServiceList = objWMIService.ExecQuery _ ("Select * From Win32_Service Where Proper noun='Alerter'")
Notice that we don't call the ExecQuery method in our Windows PowerShell command; instead nosotros utilize the -Filter parameter, specifying that the only services we want back are those where the Name is equal to Alerter.
Good question: why did nosotros put parentheses around our Get-WMIObject statement? We did that to ensure that Windows PowerShell treats the returned object as an object, that is, as something we can call a method on. And one time we have an object nosotros can and then follow the closing parenthesis with a dot (.), simply like we would using VBScript. In our Windows PowerShell command we do this:
(Get-WmiObject -calculator atl-fs-01 Win32_Service -Filter "Name='Alerter'").InvokeMethod`
("StartService",$null)
Whereas in VBScript we'd practise something like this (most ofttimes inside a For Each loop):
objItem.StartService
Got that? OK. At present information technology gets tricky. Equally you can see, in the second part of our command, we don't directly telephone call the StartService method. Instead, we do this:
InvokeMethod("StartService",$null)
What's the bargain with that? Well, as information technology turns out, nosotros do get an object back when we telephone call Get-WMIObject; in this case, however, it'due south a .NET Framework object (specifically, a System.Management.ManagementObject#root\cimv2\Win32_Service object). We tin't phone call the StartService method for one simple reason: this type of object doesn't have a StartService method. Instead, we phone call InvokeMethod, passing ii parameters:
• | "StartService", the proper noun of the WMI method nosotros desire to invoke. |
• | $Zero, which simply indicates a Null value. |
InvokeMethod expects two parameters: the method to be called, followed by any additional arguments needed by the method. In this case there are no method arguments; however, InvokeMethod expects ii parameters whether those parameters exist or not. To satisfy InvokeMethod we pass a nil value as the second parameter.
That's another good question: how did we know that the object we got back was a Arrangement.Management.ManagementObject#root\cimv2\Win32_Service object, and that this object didn't support the StartService method? Well, nosotros'd like to tell you that this is because we're all actually smart and nosotros know anything; nevertheless, we didn't think anyone would buy that. Therefore, nosotros'll tell you the truth: we grabbed the object in question and ran it through the Get-Member cmdlet in order to become back a list of its backdrop and methods. In other words, nosotros did this:
Get-WmiObject Win32_Service -Filter "Name='Alerter'" | Become-Member
From there we just engaged in a little old-fashioned trial and mistake until we figured out how to start the service.
A caveat or two. The command we came up with works just fine, but nosotros wouldn't go so far as to say that it's the best way to do this, or even that information technology's the only way to do this. If anyone knows of a improve way to carry out this same task using Windows PowerShell, please permit us know; later all, we're nevertheless learning about this new engineering ourselves. (Francis Fukuyama, we're specially interested in hearing from yous!)
Too, in the interest of keeping this discussion at a relatively superficial level, we skipped over some technical details concerning what Windows PowerShell does, and why. But those aren't of import right now; for now, nosotros but wanted you to give a bones idea of how Windows PowerShell works, every bit well as why it doesn't work (at least non even so) in certain situations. Oh: and likewise evidence y'all how to use Windows PowerShell to start a service on a remote reckoner. That was all we were hoping to achieve today.
Well, that and show to the world that interesting and exciting things still happen. And we consider that to exist a job well-done.
Source: https://devblogs.microsoft.com/scripting/how-can-i-use-windows-powershell-to-start-a-service-on-a-remote-computer/
Posted by: merrillfrenjudipt.blogspot.com
0 Response to "How To Start A Remote Computer Repair Service"
Post a Comment