
With this post, we finish publishing a translation of a series of articles on managing Windows services using PowerShell, which is published on
4sysops.com . In a
previous post , we looked at how to manage Windows services in Server 2012 using the CIM cmdlets introduced in PowerShell 3.0. In this post, we will look at how to manage service accounts using the WMI and CIM cmdlets.
So, under the cut is a translation of an article from the portal 4sysops.com.
Managing Services the PowerShell way - Part 8Previous articles:Manage Windows services using PowerShell. Part 1. Get the status of servicesManage Windows services using PowerShell. Part 2. Stop, start, pause.Manage Windows services using PowerShell. Part 3. Configuring Services with WMI and CIMManage Windows services using PowerShell. Part 4. Changing Services Using WMIManage Windows services using PowerShell. Part 5. CIM CmdletsWe use WMI
To change the password for the service account, we need a reference to the service object. And I guess this is a service with a user account.
PS C:\> get-wmiobject win32_service -filter "name='yammmsvc'" | Select name,startname name startname ---- --------- YammmSvc .\Jeff
')
In
this post, we considered that for these purposes you can use the Change () method to change the password of the service account. Also remember that the
Invoke-WmiMethod parameters do not follow the order documented on MSDN.
PS C:\> $svc = get-wmiobject win32_service -filter "name='yammmsvc'" PS C:\> $svc.GetMethodParameters("change") __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 11 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : DesktopInteract : DisplayName : ErrorControl : LoadOrderGroup : LoadOrderGroupDependencies : PathName : ServiceDependencies : ServiceType : StartMode : StartName : StartPassword : PSComputerName :
The password is on the 11th place. This means that we need to insert empty values for the previous 10 parameters that do not change.
PS C:\> $svc | Invoke-WmiMethod -Name Change -ArgumentList @($null,$null,$null,$null,$null,$null,$null,$null,$null,$null,"P@ssw0rd") __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 0 PSComputerName :
0 as the return value means the success of our action. But remember that the change will not take effect until we restart the services. If you forgot how to do this, then see one of the
previous posts .
If you want to change the name of the account, you need to clarify it in the previous parameter.
PS C:\> $svc | Invoke-WmiMethod -Name Change -ArgumentList @($null,$null,$null,$null,$null,$null,$null,$null,$null,"LocalSystem","P@ssw0rd")
We also need to set the initial password for system accounts.
We use CIM
However, it is much easier to use CIM cmdlets to solve such problems. Once again, we change the account and password for the service.
PS C:\> Get-CimInstance win32_service -filter "name='yammmsvc'" | Invoke-CimMethod -Name Change -Arguments @{StartName=".\Jeff";StartPassword="P@ssw0rd"}
And the returned value should be 0. Enter the startname in the format MACHINE \ USERNAME or DOMAIN \ USERNAME. In my case, Jeff is a local account. If you only want to change the password, you only need to correct the argument in the hash table.
Note that you can execute the query and modify it using
Invoke-CimMethod . The object in it is not necessary to transfer.
PS C:\> Invoke-CimMethod -Name Change -Arguments {StartName=".\Jeff";StartPassword="P@ssw0rd"} -Query "Select * from Win32_Service where name='yammmsvc'" –Computername JeffPC
Although it was possible to run the command locally, I decided to show its launch on a remote computer. Below is an example for changing a service on multiple computers.
PS C:\> Invoke-CimMethod -Name Change -Arguments @{StartPassword="P@ssw0rd"} -Query "Select * from Win32_service where name='MyCustomService'" –computername $computers | out-file c:\work\results.txt
As you can see, I reset the password for the MyCustomService service running on all computers listed in the $ computers variable. The results are saved in a text file, in which the computer name and the returned value are indicated. Of course, services need to be restarted for the changes to take effect.
Summing up
If you need to manage your service account, then you will need to use WMI — via WMI or CIM cmdlets. The latter is much easier to use.
Previous articles:1. Get the status of services2. Stop, start, pause3. Configure services using WMI and CIM4. Changing Services Using WMI5. CIM cmdlets