Documente Academic
Documente Profesional
Documente Cultură
Search:
Search
News Articles Tech Tools Subscribe Archive Whitepapers Digisub Write for Us! Newsletter Shop
DevOps Cloud Computing Virtualization HPC Linux Windows Security Monitoring Databases all Topics...
Home » Articles » PowerShell Part... Login
The strength of PowerShell lies in its simplicity. The cmdlets are easy to learn, easy to use, and easy to extend into an essential toolset for which there is no equivalent. In this article,
I teach you how to control services, processes, and commands on your local system and on remote systems. PowerShell is in a state of flux; it evolves with each iteration of
Microsoft’s operating systems. Windows 7 and Windows Server 2008 use PowerShell 2.0, which this series covers. Windows 8 and Windows Server 8 use the new PowerShell 3.0
that I’ll cover in a later entry.
Starting and Stopping Services
One of the basic duties of an Administrator is to start, stop, and restart services on systems. PowerShell empowers you to do so locally on a system and on remote systems. Although
manipulating services on a local system takes PowerShell scripting to an overly complex extreme, it demonstrates the syntax and the necessary switches (parameters) required to
control services on remote systems.
Although you can’t directly manipulate services on remote systems, it is possible to do so programmatically. It adds an additional level of complexity to your task, but the result is
worth the trouble. How do you know if a cmdlet has remote system capability? Use the help system to find out what you learned in the first part of this series:
SYNTAX
Get-Service [[-Name] <string[]>] [-ComputerName <string[]>]
[-DependentServices] [-Exclude <string[]>] [-Include <string[]>]
[-RequiredServices] [<CommonParameters>]
Note that one of the optional Get-Service parameters is -ComputerName . This parameter means the Get-Service cmdlet has the ability to extract information from remote systems
named by the -ComputerName switch.
However, you don’t see -ComputerName or anything related to remote computers in Start-Service or Stop-Service service manipulation cmdlets:
SYNTAX
Start-Service [-Name] <string[]> [-Exclude <string[]>] [-Include <string[]>]
[-PassThru] [-Confirm] [-WhatIf] [<CommonParameters>]
Look at an example from my local system. I need to check the status of and start the Windows Defender service if it isn’t already started:
http://www.admin-magazine.com/Articles/PowerShell-Part-2-Manipulating-Services-Processes-and-Commands 1/5
3/21/2018 The Strength of PowerShell » ADMIN Magazine
At line:1 char:14
+ start-service <<<< -DisplayName 'Windows Defender'
+ CategoryInfo : OpenError:
(System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
ServiceCommandException + FullyQualifiedErrorId :
CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
This message means that the service is not only in a Stopped state but is Disabled. A quick visual Services check, as shown in Figure 1, reveals that the Windows Defender Service
is in a Disabled state. You can’t start a disabled service. You have to change its startup type to Automatic or Manual before you can start the service.
Now, you can start the service by issuing the Start-Service cmdlet:
It’s always wise to check the status of a service that you change for confirmation of its current condition:
This is a nice exercise, but it’s easier to start and stop Windows services on a local system via the Computer Management Services application. Starting, stopping, and changing the
service startup type on remote systems in an automated fashion is not so straightforward. As I stated earlier, to perform this function, you have to do so programmatically.
The Windows Telnet Service is disabled by default on all server systems because it is a non-secure protocol. That is to say, the Telnet client and server exchange usernames and
passwords in cleartext, which can be easily captured and used to compromise systems. However, it has all of the elements of a Windows service that’s perfect for demonstration
purposes.
Get-Service : Cannot open Service Control Manager on computer 'XENAPP0'. This operation might require other privileges.
At line:1 char:12
+ Get-Service <<<< -ComputerName XENAPP0
+ CategoryInfo : NotSpecified: (:) [Get-Service], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Power
Shell.Commands.GetServiceCommand
This message tells you that the remote system doesn’t allow remote management. To enable remote management, connect to the system on which you’re trying to run the remote
command, launch a CMD window as Administrator, and run:
C:\Users\Administrator>winrm quickconfig
Answer Yes to the following questions and you should then be able to run remote commands on your systems:
http://www.admin-magazine.com/Articles/PowerShell-Part-2-Manipulating-Services-Processes-and-Commands 2/5
3/21/2018 The Strength of PowerShell » ADMIN Magazine
You see that the Telnet Server service is in a Stopped state and most likely is also Disabled. Your first inclination is to issue a command such as:
Fortunately, Microsoft provides a -ComputerName switch for the Set-Service cmdlet that makes the task of remotely setting its StartupType parameter to Automatic or Manual direct
and easy:
Note that you’re prompted to enter the name of the service (TlntSvr ) to complete the action.
Although the status of the Telnet Server service hasn’t changed, its StartupType is set to Manual . The service is now in a “startable” state. There’s no -ComputerName parameter
available for the Start-Service cmdlet. After a lengthy search, I found the following method to start a service on a remote system:
I checked out several methods that claimed to be successful, but this is the only one that did it for me. After issuing this command, you should receive a single 0 as a return value,
which indicates success. You can again check the status of the Telnet Server service:
Replace Start-Service with Stop-Service in the script to stop the service. This technique works for any Windows service.
For example, if there’s a hung process on a remote system running in a CMD window, you can find it and stop that process with a simple Invoke-Command :
http://www.admin-magazine.com/Articles/PowerShell-Part-2-Manipulating-Services-Processes-and-Commands 3/5
3/21/2018 The Strength of PowerShell » ADMIN Magazine
Note the Id of the cmd process (2208). When you stop a process, you must know its Id and send a signal to the remote system to kill or stop that process Id :
Confirm
Are you sure you want to perform the Stop-Process operation on the following item: cmd(2208)?
[Y] Yes [A] Yes to All [N] No [L] No to All [?] Help (default is "Y"): Y <ENTER>
Now try to run a CMD process by substituting Start-Process and cmd for a process name. The command would look something like:
The -PassThru switch allows you to see output to your screen. Without that switch, you’ll see no output to your screen. You might assume that the script successfully created a CMD
window on the remote system. You’ll find that this did not happen. Actually, it did happen momentarily, but the remote CMD window launches and then dies.
Windows, by design, doesn’t allow you to launch programs in this way. That said, it is still possible to do so programmatically. However, doing so is far outside the scope of an
introductory PowerShell article.
1 2 Next »
Related content
PowerShell Part 1: Retrieving System Information
Windows administrators, like Unix or Linux administrators, look for ways to perform their duties with ease and elegance through automation. PowerShell allows administrators to
gather information, change Registry parameters, work with Active Directory, manipulate services, look at events, work with Windows Management Instrumentation (WMI), and much
more. PowerShell is a Windows administrator’s best friend.
more »
more »
more »
more »
more »
http://www.admin-magazine.com/Articles/PowerShell-Part-2-Manipulating-Services-Processes-and-Commands 4/5
3/21/2018 The Strength of PowerShell » ADMIN Magazine
Sort by Best
Recommend 2 ⤤ Share
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
DevOps
Reinvent your network with DevOps tools and techniques:
• Jekyll – A DIY HTML Engine
• Automated Jenkins CI
Improve your career by showcasing your DevOps skills with the new Linux Professional Institute DevOps Tools Engineer certification.
Newsletter
Newsletter Archive
Topics
12.04 LTS 16 cores 8 cores AMD AMD-V ARB Active Directory Administration Amazon AWS Amazon CloudFront Anaconda Analytics Ansible Apache Apache
Deltacloud Apache benchmarking tool Architecture Review Board Archiving ab acquisition admin tools agedu alert amazon analysis analysis application performance arp
cache poisoning arpspoof
Admin Magazin…
12K likes
Like Page
vice
cle Code
tact
http://www.admin-magazine.com/Articles/PowerShell-Part-2-Manipulating-Services-Processes-and-Commands 5/5