set-service [-ServiceName] System.String [-DisplayName] System.String [[-Description] System.String] [[-StartupType] {...}]
MSH>(get-service "Alerter") | gm
TypeName: System.ServiceProcess.ServiceController
Name MemberType Definition
---- ---------- ----------
...
set_DisplayName Method System.Void set_DisplayName(String value)
set_MachineName Method System.Void set_MachineName(String value)
set_ServiceName Method System.Void set_ServiceName(String value)
set_Site Method System.Void set_Site(ISite value)
...
errServiceChange = objService.Change( , , , , , ,"Domain\User")
errPasswdChange = objService.Change( , , , , , , ,"Passwd")
wscript.echo(errServiceChange &":"& errPasswdChange)
MSH>get-wmimethodHelp
No WMIClass given
MSH>gwm win32_service change
win32_service : change :
The Change method modifies a service. The Win32_LoadOrderGroup parameter represents a grouping of system services defining execution depend
encies. The services must be initiated in the order specified by the Load Order Group as the services are dependent on each other. These de
pendent services require the presence of the antecedent services in order to function correctly.It returns one of the following integer val
ues:
0 - The request was accepted.
1 - The request is not supported.
2 - The user did not have the necessary access.
3 - The service cannot be stopped because other services that are running are dependent on it.
4 - The requested control code is not valid, or it is unacceptable to the service.
5 - The requested control code cannot be sent to the service because the state of the service (Win32_BaseService:State) is equal to 0, 1, o
r 2.
6 - The service has not been started.
7 - The service did not respond to the start request in a timely fashion.
8 - Unknown failure when starting the service.
9 - The directory path to the service executable was not found.
10 - The service is already running.
11 - The database to add a new service is locked.
12 - A dependency for which this service relies on has been removed from the system.
13 - The service failed to find the service needed from a dependent service.
14 - The service has been disabled from the system.
15 - The service does not have the correct authentication to run on the system.
16 - This service is being removed from the system.
17 - There is no execution thread for the service.
18 - There are circular dependencies when starting the service.
19 - There is a service running under the same name.
20 - There are invalid characters in the name of the service.
21 - Invalid parameters have been passed to the service.
22 - The account, which this service is to run under is either invalid or lacks the permissions to run the service.
23 - The service exists in the database of services available from the system.
24 - The service is currently paused in the system.
Other - For integer values other than those listed above, refer to Win32 error code documentation.
change Parameters :
Name = DesktopInteract
Type = boolean
Optional = False
Name = DisplayName
Type = string
Optional = False
Name = ErrorControl
Type = uint8
Optional = False
Name = LoadOrderGroup
Type = string
Optional = False
Name = LoadOrderGroupDependencies
Type = string
Optional = False
Name = PathName
Type = string
Optional = False
Name = ServiceDependencies
Type = string
Optional = False
Name = ServiceType
Type = uint8
Optional = False
Name = StartMode
Type = string
Optional = False
Name = StartName
Type = string
Optional = False
Name = StartPassword
Type = string
Optional = False
Sample Script :
# win32_service change-Method Sample Script
# Created by Get-WmiMethodHelp
# /\/\o\/\/ 2005
# Fill InParams values before Executing
# InParams that are Remarked (#) are Optional
$Class = "win32_service"
$Method = "change"
$Computer = "."
#win32_service Key Properties :
$Name = [string]
$filter = "Name = '$Name'"
$MC = get-WMIObject $class -computer $Computer -filter $filter
$InParams = $mc.GetMethodParameters($Method)
$InParams["DesktopInteract"] = [boolean]
$InParams["DisplayName"] = [string]
$InParams["ErrorControl"] = [uint8]
$InParams["LoadOrderGroup"] = [string]
$InParams["LoadOrderGroupDependencies"] = [string]
$InParams["PathName"] = [string]
$InParams["ServiceDependencies"] = [string]
$InParams["ServiceType"] = [uint8]
$InParams["StartMode"] = [string]
$InParams["StartName"] = [string]
$InParams["StartPassword"] = [string]
"Calling win32_service : change with Parameters :"
$inparams.get_properties() | select name,Value
$R = $mc.InvokeMethod($Method, $inParams, $Null)
"Result : "
$R
MSH>
#win32_service Key Properties :
$Name = [string]"Alerter"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"\root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where Name = 'Alerter'",,48)
For each objService in colItems
errServiceChange = objService.Change( , , , , , ,".\mow","test")
next
wscript.echo(errServiceChange )
$InParams["StartName"] = [string]".\mow"
$InParams["StartPassword"] = [string]"test"
October 2005 November 2005 December 2005 January 2006 February 2006 March 2006 April 2006 May 2006 June 2006 July 2006 August 2006 September 2006 October 2006 November 2006 December 2006