Showing posts with label get-service. Show all posts
Showing posts with label get-service. Show all posts

Monday, January 19, 2009

when XML denies CSV and HTM

to get the ServiceController objects for all of the services that are running in their own processes, then export this information to a CSV file

PS R:\> get-service
| where { $_.serviceType -eq "Win32OwnProcess" }
| export-csv -notype ./edit3.csv

or,

PS R:> get-service | where { $_.serviceType -eq "Win32OwnProcess" } | export-csv -notype ./edit3.csv


or,

PS R:\> get-service
| where { $_.serviceType -eq "Win32OwnProcess" }
| export-csv -notype ./edit3.htm <--- not applicable for .xml


` edit3.csv ` contains delimited (comma-separted) text.

match and go

to store configuration information

PS R:\> get-service
| where { $_.Name -match "w32time" }


Status Name DisplayName
------ ---- -----------
Stopped W32Time Windows Time

` $_ ` contains the current pipeline object.
if ` -match "" `, then it list out all.

run a cmdlet command from a cmd command

[] to run PowerShell commands from a cmd command prompt

step 1) press WinLogo-key 'R' and type cmd.exe

R:\> powershell.exe -command "get-service"

[] to run PowerShell commands from a Start | Run prompt

step 1) press WinLogo-key 'R' and type

PS> powershell -command "C:\Lindows\co\Administrator\wpsEventsClear.ps1"

[] to call PowerShell from a cmd command prompt that will sort the services

step 1) press WinLogo-key 'R' and type

R:\> powershell.exe -command "get-service | sort Name"

the entire command string is enclosed in quotation marks to prevent cmd from attempting to handle the pipeline.

list services to a text file

[] to output a table containing the names of running services to a file named 'RunningServices.txt'

PS R:\> get-service -ServiceName * |
>> where-object { $_.status -eq "running" } |
>> format-table ServiceName, Status |
>> out-file -filePath "r:\RunningServices.txt"
>>

[] to open with Notepad

PS R:\> .\RunningServices.txt

begin with 'a' through 'f' and 'n' through 'z'

to find all services but exclude those whose service names begin with 'a' through 'f' and 'n' through 'z'

PS R:\> get-service -Name * -Exclude [a-fn-z]* |
>> sort-object Name | format-table Name, DisplayName -auto
>>


` >> ` appears when you press Enter-key before typing.

contain the character sequence

to find running services that contain the character sequence 'sql' in the service's name

PS R:\> get-service "*sql*"
| where-object { $_.status -eq "running" }

or,

PS R:\> get-service
| where-object { $_.status -eq "running" }
| where-object { $_.name -match ".*sql.*" }


Status Name DisplayName
------ ---- -----------
Running MSSQLServer MSSQLServer

something like "net"

to list out all services that are currently running, whose displayName is something like "net"

PS R:\> get-service
| where { $_.status -eq "running" -and $_.displayName -like "*net*" }

Status Name DisplayName
------ ---- -----------
Running clr_optimizatio... .NET Runtime Optimization Service v...
Running LmHosts TCP/IP NetBIOS Helper
Running Netman Network Connections
Running Nla Network Location Awareness (NLA)

list out all services and store them an MS Excel spreadsheet

PS>
$a = new-object -comobject excel.application
$a.Visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$c.Cells.Item(1,1) = "Service Name"
$c.Cells.Item(1,2) = "Service Status"
$i = 2
get-service | foreach-object{ $c.cells.item($i,1) = $_.name; $c.cells.item($i,2) = $_.status; $i=$i+1}
$b.SaveAs("Test.xls")
$a.Quit()

Make sure you have installed MS Excel because it will register it as a COM-type, otherwise it can't load during processing.

list out all services and store them into a web page

step 1) to list and store
PS R:\> get-service | convertto-html > .\a10.html

step 2) to run a web page
PS R:\> .\a10.html

`a10.html` will show a detailed information such as ( Name, CanPauseAndContinue, CanShutdown, CanStop, DisplayName, DependentServices, MachineName, ServiceName, ServicesDependentOn, ServiceHandle, Status, ServiceType, Site Container )

display data in colors

to list out of all services by limiting to name and their status

PS R:\> get-service
| foreach-object { write-host -f yellow -b red $_.name $_.status }

` -f ` also can be coded as ` -foregroundcolor `.
` -b ` also can be coded as ` -backgroundcolor `

display data with DataGrid

to tabulate a list of all services by sorting the services by their state before filling the datagrid, then output the processes

PS>
[void][System.reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$form = new-object System.Windows.Forms.Form
$DataGridView = new-object System.windows.forms.DataGridView
$Form.Text = "My First Datagrid"
$array= new-object System.Collections.ArrayList

$array.AddRange( @( get-service | write-output ) )
// $array.AddRange( @( get-process | sort-object company | write-output ) )

$DataGridView.DataSource = $array
$DataGridView.Dock = "fill"
$DataGridView.AllowUsertoResizeColumns=$True
$form.Controls.Add($DataGridView)
$form.showdialog()


For Get-Service cmdlet, the column headers in datagrid shows (Name, CanPauseAndContinue, CanShutdown, CanStop, DisplayName, ServiceName, Status)

For Get-Process cmdlet, the column headers in datagrid shows ( _NounName, Name, Handles, VM, WS, PM, NPM, Path, Company, CPU, FileVersion, ProductVersion, Description, Product, BasePriority, HandleCount, Id, etc. ) or, get-process | fl *

depend on WMI service vs WMI service depend on

[] to list out all services that depend on WMI service

PS> (Get-Service winmgmt).DependentServices

Status Name DisplayName
------ ---- -----------
Stopped wscsvc Security Center
Stopped SharedAccess Windows Firewall/ICS

[] to list out all services that WMI service depend on

PS> (Get-Service winmgmt).ServicesDependedOn

Status Name DisplayName
------ ---- -----------
Running RPCSS Remote Procedure Call (RPC)

'start', 'stop' mssqlserver

[] to start a service by calling the ServiceController.Start method

PS R:\> (Get-Service mssqlserver).Start()

MSDE7 icon at system tray turned from 'Red' to 'Green'

[] to stop a service by calling the ServiceController.Stop method

PS R:\> (Get-Service mssqlserver).Stop()

MSDE7 icon at system tray turned from 'Green' to 'Red'

grouping services

[] to get a report of all services and count total number of 'running' and 'stopped' status

PS> get-service | group-object status

Count Name Group
----- ---- -----
58 Stopped {System.ServiceProcess.ServiceController...}
29 Running {System.ServiceProcess.ServiceController..}

[] to show all services in the "Stopped" group

PS R:\> (get-service | group-object status)[0].group

[] to show all services in the "Running" group

PS R:\> (get-service | group-object status)[1].group

services depend on services

[] to list the services on the computer that have dependent services

PS R:\> get-service | where-object {$_.DependentServices}

Status Name DisplayName
------ ---- -----------
Running dmserver Logical Disk Manager
Running EventSystem COM+ Event System
Stopped lanmanserver Server

[] to display the complete list of dependent services of MSSQLSERVER service

PS R:\> (get-service mssqlserver).dependentservices

Status Name DisplayName
------ ---- -----------
Stopped SQLServerAgent SQLServerAgent

[] to display the number of dependent services that each service has

PS> get-service | where-object {$_.DependentServices} | format-list -property Name, DependentServices, @{Label="NoOfDependentServices"; Expression={$_.dependentservices.count}}

Name : lanmanworkstation
DependentServices : {RpcLocator, Netlogon, Messenger, Browser...}
NoOfDependentServices : 5

use of a pipe for plumber work?

to display only the services that are currently running

PS R:\> get-service | where-object {$_.Status -eq "Running"}

or,

PS R:\> get-service | where { $_.Status -eq "Running" }


` | ` is a pipeline operator, which passes the results to the Where-Object cmdlet, which selects the services with a Status property that equals "Running".

` -eq ` represents ' equal to '.

` Status ` is only one property of service objects.

` $_ ` is the current object operator, which indicates that the cmdlet will example the Status property of each service as it goes through the pipeline.

beginning with an 'n'

[] to list out the service name (not the displayName) beginning with 'n'

PS> get-service -include n*

[] to list out the service (displayName) beginning with 'n'

PS> get-service -displayName n*

[] to display only the service names that begin with 'n'

1) gets the services (name, not DisplayName) on the system and then stores them in the $input1 variable

PS R:\> $input1 = get-service

2) gets the services in the $input1 variable, but includes only those that begin with 'n'

PS R:\> get-service -inputobject $input1 -include n*

'-InputOutput` parameter passes the objects stored in the variable to Get-Service.

when 'Stopped' comes before 'Running'...

to sort in ascending order by status value

PS R:\> gsv | sort-object -property status

Stopped ... <---- sort mechanism takes this as integer value of '1'
Running ... <---- sort mechanism takes this as integer value of '4'

'gsv' is the built-in alias of 'get-service'

create the $out1 variable

to create the $out1 variable (will be removed once PS window is exit)

1) stores the service object in it

PS> get-service browser -OutVariable out1

2) append the service object to the content of $out1

PS> get-service wmi -OutVariable +out1

3) display the contents of $out1

PS> $out1

restart multiple services

to restart multiple services by getting a list of services, filter them, and then perform the restart

PS> Get-Service | Where-Object -FilterScript {$_.CanStop} | Restart-Service