Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: Script question for disabling services

HTVi
TV Discussion Newsgroups

Script question for disabling services
Jack.Merchant[ at ]gmail.com 6/27/2007 7:32:28 PM
I want to use the script below with WMI to change the startup type of
some services across our domain on multiple computers. How can I
specify to read the computer name from a text file?

'#############################
'Function ChangeServiceStartOption
'Variables
'ComputerName
'ServiceName - Service Display Name
'StartOption - "Automatic","Manual","Disabled"

'Returns
'0 if service's start option is changed successfully
'1 if it is not changed successfully
'2 if service does not exist on the remote machine
'3 if unable to connect to the remote machine

'NOTE: This function searchs for the Service DISPLAY NAME, not the
actual name.
' To change that, just replace DisplayName with Name


Function ChangeServiceStartOption(ComputerName,ServiceName,
StartOption)
On Error Resume Next

'Create a WMI object to connect to the computer
Set objWMIService = GetObject("Winmgmts:\\" & ComputerName & "\root
\cimv2")
If Err.Number = 0 Then

'Retrieve a collection of Services using the ExecQuery method.
Set colServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service
Where DisplayName = '" & _
ServiceName & "'")

'Check to see if the service was found
If colServices.Count <> 0 Then

'Loop through the collection of Services (should only be one)
For Each Service in colServices

'Change the service start option
errCode = Service.Change( , , , , StartOption)
If errCode = 0 Then
ChangeServiceStartOption = "0"
Else
ChangeServiceStartOption = "1"
End If

Next

'Service Not Found
Else
ChangeServiceStartOption = "2"
End If

Else
ChangeServiceStartOption = "3"
Err.Clear
End If


End Function

'#############################

Thanks

Re: Script question for disabling services
"Richard Mueller [MVP]" <rlmueller-nospam[ at ]ameritech.nospam.net> 6/27/2007 11:50:31 PM
Use the FileSystemObject to read a text file, one line at a time. For
example:
================
Const ForReading = 1

' Specify file of computer NetBIOS names.
strFile = "c:\scripts\computers.txt"

' Open file for read access.
Set objFSO = CreateObjectg("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)

' Read each line of the file.
Do Until objFile.AtEndOfStream
strComputerName = Trim(objFile.ReadLine)
' Skip blank lines.
If (strComputerName <> "") Then
' Perform tasks on this computer.
' ...

End If
Loop

' Clean up.
objFile.Close

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--

<Jack.Merchant[ at ]gmail.com> wrote in message
news:1182972748.567601.112070[ at ]j4g2000prf.googlegroups.com...
[Quoted Text]
>I want to use the script below with WMI to change the startup type of
> some services across our domain on multiple computers. How can I
> specify to read the computer name from a text file?
>
> '#############################
> 'Function ChangeServiceStartOption
> 'Variables
> 'ComputerName
> 'ServiceName - Service Display Name
> 'StartOption - "Automatic","Manual","Disabled"
>
> 'Returns
> '0 if service's start option is changed successfully
> '1 if it is not changed successfully
> '2 if service does not exist on the remote machine
> '3 if unable to connect to the remote machine
>
> 'NOTE: This function searchs for the Service DISPLAY NAME, not the
> actual name.
> ' To change that, just replace DisplayName with Name
>
>
> Function ChangeServiceStartOption(ComputerName,ServiceName,
> StartOption)
> On Error Resume Next
>
> 'Create a WMI object to connect to the computer
> Set objWMIService = GetObject("Winmgmts:\\" & ComputerName & "\root
> \cimv2")
> If Err.Number = 0 Then
>
> 'Retrieve a collection of Services using the ExecQuery method.
> Set colServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service
> Where DisplayName = '" & _
> ServiceName & "'")
>
> 'Check to see if the service was found
> If colServices.Count <> 0 Then
>
> 'Loop through the collection of Services (should only be one)
> For Each Service in colServices
>
> 'Change the service start option
> errCode = Service.Change( , , , , StartOption)
> If errCode = 0 Then
> ChangeServiceStartOption = "0"
> Else
> ChangeServiceStartOption = "1"
> End If
>
> Next
>
> 'Service Not Found
> Else
> ChangeServiceStartOption = "2"
> End If
>
> Else
> ChangeServiceStartOption = "3"
> Err.Clear
> End If
>
>
> End Function
>
> '#############################
>
> Thanks
>


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen