![]() The above code will display the same services with SQL in their names. $sqlservices = Get-service -ComputerName VMServer01 -Name *SQL* To start lets make sure we are getting all of the services by name in our foreach: # Get anything with SQL in its name Imagine scheduling this to run automatically instead of working at 1am. We can use a foreach loop to stop and set all of the services startup to manual or disabled before the maintenance window. For this SQL service example we can imagine that we are performing some maintenance or patching and all of the SQL services need to be stopped and disabled during a few reboots on a certain date. We will use a foreach loop to select each service by name and set the appropriate values. Now that we understand the basics for Set-Service the next step in our process is to control multiple services. Set-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' -Status Running -StartupType Manual Stop and Start using Set-Service Set-service -ComputerName VMServer01 -Name SQLBrowser -StartupType Disabled Set-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' -Status Stopped Using Get-Service try using the following examples to stop, start and set the startup type. The only thing we need is the Name of the service for this cmdlet to work. In conjunction with the Get-Service cmdlet we grab services from a remote server and use this cmdlet to manage its parameters or current state. It can set the parameters for a service like the startuptype or just stop and restart services. Set-Service is also a simple command that does pretty much what it says. How can we use this? The Set-Service Cmdlet Using ‘Name” just returns the Name and no details. ![]() ![]() The same detail is now stored in the object but it doesn’t seem very useful yet. Now we can get the same info by using the dot notation, after executing the above, try this: $sqlservices.Name Lets assign the results of our “anything” name SQL request into a variable called “$sqlservices” $sqlservices = Get-service -ComputerName VMServer01 -Name '*SQL*' In order to deal with multiple services and/or instances we can assign the results to a variable and work with that instead of all this repeating code. ( Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' ).DisplayName Multiple Services Status and StartupType ( Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' ).StartType ( Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' ).Status ( Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' ).DependentServices Try the following: ( Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' ).MachineName I like to assign the results to a variable and work with it using the dot notation but we can also just add parenthesis. There is more that we can collect from the objects returned by this cmdlet. If you are working with a default instance than the name will just be MSSQLSERVER. Also note that I had to use the single quotes because of the dollar sign ($) identifying the SQL instance. The DependantServices option goes the other way and shows what services depend on the specified. You will notice here the SQL Agent has a requirement for the SQL Service but MSSQL does not. Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' -DependentServices Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' -RequiredServices We can find the service dependencies easily: Get-service -ComputerName VMServer01 -Name 'SQLAgent$VMSQL01' -RequiredServices One last option you should be aware of is certain services have dependencies such as the SQL Agent which needs SQL Service to be running for example. Then when you have it you can complete the name and retrieve only that services details. In this case the asterisk is useful to find multiple services where you are not sure of the exact name. Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' Get-service -ComputerName VMServer01 -Name SQLBrowser What if we want to see only the SQL services or just one service by name? Get-service -ComputerName VMServer01 -Name *SQL* How to SSH using Windows Powershell in cPanel Server Get-service -ComputerName VMServer01Īs you can see this is simple and useful for a single machine if you want to browse the running services.
0 Comments
Leave a Reply. |