Powershell Day 9

Day 9 Process and Service

Service:

For Powershell we can also work with services. You can see which all services running on your system using services.msc, services can be start /pause or stop. Using Powershell we can easily work on services which generally takes time to open up service also can work on multiple services at the same time.

To get the list of services on your system. Get-service is the command

PS C:\>get-service

Now as you know you can use deferent properties on it to see specific services like get the list of services which are currently in running step, the command would be

PS C:\> get-service | where {$_.Status -eq “Running”}

Now what if we want to stop/start some serive(s),

PS C:\> get-service “SqlBrowser” | Start-service

Or

PS C:\> get-service | where ($_.Name –eq “SqlBrowser” }| Start-service

This command will start “SqlBrowser” service

remember my mistake if we don’t spacify the condition or our condition is not exact you may end of start/stop on all the sevices-  which is very dangerous

so Powershell has a cool command option as –whatif or –Confirm  which provides information about whatif the given command run, and confirm would you like to run this command (y/n..) respectively.

PS C:\> Start-service | where ($_.Name –eq “SqlBrowser” }–whatif

Start-service and Stop-service is cmdlet commands

To get more information about service related commands

PS C:\> gcm *service |select-object name

Name

—-

Get-Service

New-Service

Restart-Service

Resume-Service

Set-Service

Start-Service

Stop-Service

Suspend-Service

Process:

Every application which is running on the system run as a process like notepad- when you open a notepad at background it runs notepad.exe –notepad process –which you can confirm with Task Manager(taskmgr) –Ctrl+Alt+Del ->Task Manager…

Simillar to Services, we can work with processes like get list of processes start/stop…

*by mistake if you go stop process for all you system will crash

So remember these all commands are good but dangerous as well. So use it with caution.

PS C:\> get-process

Processes can be stopped only so another command is

PS C:\>stop-process

Remember to use –whatif or –confirm option before using this commands

Exporting to different objects

There are diffent objects we can export our output by default the output will be to “host” screen we can convertto

PS C:\> get-process | convertTo-html

Now as a sql server DBA – we do grouping of the query with group by command similarly we can group the similar objects output together using Group-object command

PS C:\> get-service | Group-object status

This will group all the running services in one group and stopped services in another

Advertisements
This entry was posted in Powershell and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s