PowerShell Get-Service

Since I have written a process that checks all of our SQL Services I have been researching other methods and might have found a quicker way using Get-Service but would require an addition step to pass the arguments accordingly on a server by server basis. Potentially I could setup a nightly process to query all of the sql related services for all our environments and save the results to a table. Then loop through the records by server type (Production, Development, Test, Staging, etc…) for any service with a startup -eq “Auto” with a state -ne “running”. If configured properly this could potentially report a service outage for a given server. This option is great for getting information about a single service but one thing I read was that you can pass multiple arguments for the service name; however, this method doesn’t format it well. function GetServiceInfo ($server, $service) { Get-Service -Name $service -ComputerName $server } GetServiceInfo Server01 SQLWriter This option loads the results into an array and then formats the hash table nicely especially when sending multiple arguments for the service name. function GetServiceInfo ($server, $service) { $svcs = @(Get-Service -Name $service -ComputerName $server) $svcs | ft -AutoSize } GetServiceInfo Server01 SQLWriter, SQLServerAgent, SQLBrowser, ReportServer, MSSQLServerOlapService, MSSQLServer, MSDTSServer110 The only problem you will have with this particular method is with the error message. It will not accurately report a “permissions issue” as Get-WmiObject would. It will falsely report that it cannot find any service with the service name: ‘service name would go here’ instead of reporting that it encountered a permissions issue against the said server. That’s only...

PowerShell Change Service Startup

In the event you need to change the service startup from either “Manual”, “Automatic”, or “Disabled” on a remote server, PowerShell makes it extremely easy and here’s one of likely many ways you can do it. function ChangeServiceStartup ($server, $service, $startup) { Set-Service -Name $service -Computername $server -StartupType $startup } ChangeServiceStart Server01 SQLWriter...

Getting acquainted with PowerShell

PowerShell (POSH) is an area in which I have not explored yet. Colin Smith (blog | twitter) and Zach Mattson (blog | twitter) both presented topics about POSH and unfortunately I could not attend either, but that’s a different story. Today I felt I needed to take a break from my certification studies and start getting familiar with Microsoft’s new scripting language. We have a POSH user group here in Arizona which is appropriately called the Arizona PowerShell Users Group. Though I haven’t attended a meeting yet the subjects that are covered is nonetheless intriguing. Jason Helmick (blog | twitter) and Mike Pfeiffer (blog | twitter) are the officers of the user group and their blogs contain a lot of information on how to get started. Using Jason’s example from his video post: QuickBit: Executing scripts in PowerShell I saw him execute a cool script that utilized the system speech and it literally spoke what he had written. I used the script editor and ran the following: script syntax [void][System.Reflection.Assembly]::LoadWithPartialName("System.speech") $Speak=new-object System.Speech.Synthesis.SpeechSynthesizer $Speak.speak("SQL Saturday 47 Rocks!") Another thing that Jason pointed out that I saw as really handy was the fact you can output the results to a grid view. Yeah a grid view! script syntax Get-Service | Out-GridView The cool thing about output to grid is the fact that you have filtering capabilities and sorting. That’s a big win in my eyes! results I really dig command-line administration and I should have dove right into this but I didn’t. Now I need to begin my search for some good books and reference sites which Jason also refers to...