Home

Google
 

How to grant permissions to specific Windows services


 

  1. Download and install on the target computer the Windows resource kit utility Subinacl.
  2.  Determine the target service name by opening the registry (Start -> Run -> Regedit) and navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<ServiceName> (If the service name contains spaces, the whole parameter should be enclosed in quotation marks.)
  3. Open the CMD prompt and navigate to the folder in which the Subinacl tool is installed (Default: c:\Windows\Program Files\Windows Resource Kit\tools\)

Run the command SUBINACL /SERVICE \\MachineName\ServiceName /GRANT=[DomainName\]UserName[=Access] where ·  The user executing this command must have administrator rights in order for it to complete successfully.

·  If “MachineName” is omitted, the local machine is assumed.

·  If “DomainName” is omitted, the local machine is searched for the account.

·  Although the syntax example indicates a user name, this will work for user groups as well.

·  The values that ‘Access’ can take are:

   F : Full Control
   R : Generic Read
   W : Generic Write
   X : Generic eXecute
   L : Read controL
   Q : Query Service Configuration
   S : Query Service Status
   E : Enumerate Dependent Services
   C : Service Change Configuration
   T : Start Service
   O : Stop Service
   P : Pause/Continue Service
   I : Interrogate Service 
   U : Service User-Defined Control Commands
                                      
  1. ·  If ‘Access’ is omitted then ‘F (Full Control)’ is assumed.

 

To automate the permission changes use the script below:

 

   strDomain   = Wscript.Arguments.Item(0)’domain where computer account is held
   strComputer = Wscript.Arguments.Item(1)’computer netbios name
   strSecPrinc = Wscript.Arguments.Item(2)’user’s login name as in: DomainNameUserName
   strAccess   = Wscript.Arguments.Item(3)’access granted, as per the list in the KB
 
   ‘bind to the specified computer
   set objTarget = GetObject(”WinNT://” & strDomain & “/” & strComputer & “,computer”)
 
   ‘create a shell object.  Needed to call subinacl later
   set objCMD = CreateObject(”Wscript.Shell”)
 
   ‘retrieve a list of services
   objTarget.filter = Array(”Service”)
 
   For each Service in objTarget
 
   ‘call subinacl to se the permissions
   command = “subinacl /service ” & Service.name & ” /grant=” & strSecPrinc & “=” & strAccess
   objCMD.Run command, 0
 
   ‘report the services that have been changed
   Wscript.Echo “User rights changed for ” & Service.name & ” service”
   next
        

The script should be saved as a .vbs file, such as “Services.vbs,” and called like this:

   CSCRIPT Services.vbs DomainName ComputerName UserName Access
                                      

 

Vbscript to Search Windows Event Logs


‘ —— SCRIPT CONFIGURATION ——
On Error Resume Next

intEventCode = 4377 ‘ Enter the Event ID here
intEventTime = InputBox(”Enter Date in the Format MM/DD/YYYY”, “Enter Date to check Event”) ‘ No default date used
strLog = “System” ‘ Event log name; e.g. Application
intMaxNum = 0 ‘ Max events to return (0 for all)
strSource = “NTServicePack” ‘Enter the Event source here

‘The function of this section is to get the computer name(s) from a text file and connect to computer
Set oFSO = CreateObject(”Scripting.FileSystemObject”)

Set oTS = oFSO.OpenTextFile(”c:\Computer.txt”)

Do Until oTS.AtEndOfStream

varComputer = oTS.ReadLine

arrComputers = Array(varComputer)

‘ —— END CONFIGURATION ———
for each strComputer in arrComputers
WScript.Echo vbCrLf & vbCrLf
WScript.Echo “Searching ” & strComputer & “….” & vbCrLf
set objWMI = GetObject(”winmgmts:\\” & strComputer & “\root\cimv2″)
set colEvents = objWMI.ExecQuery(”Select * from Win32_NTLogEvent ” & _
” Where Logfile = ‘” & strLog & “‘” & _
” and TimeGenerated >= ‘”& intEventTime & “‘” & _
” and SourceName = ‘” &strSource & “‘” & _
” and EventCode = ” & intEventCode)

count = 0
for each objEvent in colEvents
Wscript.Echo “Date: ” & objEvent.TimeWritten
Wscript.Echo “Source: ” & objEvent.SourceName
Wscript.Echo “Category: ” & objEvent.Category
Wscript.Echo “Type: ” & objEvent.Type
Wscript.Echo “Event Code: ” & objEvent.EventCode
Wscript.Echo “User: ” & objEvent.User
Wscript.Echo “Computer: ” & objEvent.ComputerName
Wscript.Echo “Message: ” & objEvent.Message
WScript.Echo “——”
WScript.Echo
count = count + 1
if intMaxNum > 0 and count >= intMaxNum then
WScript.Echo “Reached maximum threshold…exiting”
exit for
end if
next
Next
Loop

VBScript to find old users in a domain


On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject(”ADODB.Connection”)
Set objCommand = CreateObject(”ADODB.Command”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”
Set objCommand.ActiveConnection = objConnection
Set objDSE = GetObject(”LDAP://RootD
SE”)
strDomain = objDSE.Get(”DefaultNamingContext”)
objCommand.CommandText = “SELECT adspath,Name FROM ‘LDAP://” & strDomain & “‘ ” & “WHERE objectClass=’user’ and objectcategory=’person’ and lastlogontimestamp <=’00000000000′”
objCommand.Properties(”Page Size”) = 2000
objCommand.Properties(”Timeout”) = 30
objCommand.Properties(”Searchscope”) = ADS_SCOPE_SUBTREE
objCommand.Properties(”Cache Results”) = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strUserName = objRecordSet.Fields(”Name”).Value
WScript.Echo strUserName
objRecordSet.MoveNext
Loop

How to get IP Address Information from Network Adapters Using Vbscript

The following Vbscript will gather Ip address information from network cards:

Set objWMIService = GetObject(”winmgmts:\\”& strComputer & “\root\cimv2″)
Set colAdapters = objWMIService.ExecQuery (”SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True”)
n = 1
For Each objAdapter In colAdapters
wIELine
WriteLineBoldIE “Network Adapter Info”
wIETableHeaderStart “100″
wIERowStart
wIETableHeaderItem “Adapter”
wIETableHeaderItem “IP”
wIETableHeaderItem “Subnet”
wIETableHeaderItem “Gateway”
wIETableHeaderItem “DNS”
wIETableHeaderItem “WINS”
wIERowStop
If Not IsNull(objAdapter.IPAddress) Then
For i = 0 To UBound(objAdapter.IPAddress)
wIERowStart
If i=0 Then
wIERowItem “Adapter ” & objAdapter.Index
‘ wIERowItem left(objAdapter.Description,15)
Else
wIERowItem “No Name “
End If
If objAdapter.DHCPEnabled Then
wIERowItem objAdapter.IPAddress(i) & “(DHCP)”
Else
wIERowItem objAdapter.IPAddress(i)
End If
Next
End If
If Not IsNull(objAdapter.IPSubnet) Then
For i = 0 To UBound(objAdapter.IPSubnet)
wIERowItem objAdapter.IPSubnet(i)
Next
End If

If Not IsNull(objAdapter.DefaultIPGateway) Then
For i = 0 To UBound(objAdapter.DefaultIPGateway)
wIERowItem objAdapter.DefaultIPGateway(i)
Next
Else
wIERowItem “Not configured”
End If

If Not IsNull(objAdapter.DNSServerSearchOrder) Then
DNSInfo = “”
For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
DNSInfo = DNSInfo & objAdapter.DNSServerSearchOrder(i) & “<BR>”
Next
wIERowItem DNSInfo
End If
If objAdapter.WINSPrimaryServer <> “” Then
wIERowItem objAdapter.WINSPrimaryServer & “<BR>” & objAdapter.WINSSecondaryServer
End If

n = n + 1
wIETableStop
Next

How do I Disable Windows Product Activation Notices with VBscript?

If you are interested in disabling the Windows product activation notices with a vbscript then the following steps maybe of interest to you:

  1. In the root of your system drive save the following in a text file
  2. Set objWMI = GetObject(”winmgmts:{impersonationLevel=
    impersonate}!\\” & computer & “\root\cimv2″)
    Set objWPA = objWMI.ExecQuery(”Select * from
    Win32_WindowsProductActivation”)

    For Each PA in objWPA
    PA.SetNotification(0)
    Next

  3. Replace computer with the name of the target computer. Bolded items should be on the same line in the script.
  4. Name the file deactivate.vbs
  5. open a CMD prompt and change directory to c:>
  6. Run the following command:
  7. c:> cscript deactivate.vbs

How do I script the unlocking of domain user accounts?

To use this script you will need to enter the relevant username and domain into message boxes that prompt.

UserName = InputBox(”Enter the user’s login name that you want to unlock:”)

If UserName = “” Then
WScript.Echo “No username was given or you clicked Cancel”
WScript.Quit(1)
End If

DomainName = InputBox(”Enter the domain name in which the user account exists:”)

If DomainName = “” Then
WScript.Echo “No domain was given or you clicked Cancel”
WScript.Quit(1)
End If

Set UserObj = GetObject(”WinNT://”& DomainName &”/”& UserName &”")

If UserObj.IsAccountLocked = -1 then UserObj.IsAccountLocked = 0

UserObj.SetInfo

If err.number = 0 Then

Wscript.Echo “The Account Unlock Failed. Check that the account is, ” & _
“in fact, locked-out.”

Else

Wscript.Echo “The Account Unlock was Successful”

End If

How do I Remotely Shutdown all Domain Computers -VBscript

If you every need to shutdown all you domain computers due to a expected power outage, security breach etc. then this Vbscript should help:
On Error Resume Next

Set objNet = CreateObject(”wscript.network”)
strCurrentPC = objNet.ComputerName

‘The following section will connect to any Active Directory domain

Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject(”ADODB.Connection”)
Set objCommand = CreateObject(”ADODB.Command”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”
Set objCommand.ActiveConnection = objConnection
Set objDSE = GetObject(”LDAP://RootDSE”)
strDomain = objDSE.Get(”DefaultNamingContext”)
objCommand.CommandText = “SELECT Name, Location FROM ‘LDAP://” & strDomain & “‘ ” & “WHERE objectClass=’computer’”
objCommand.Properties(”Page Size”) = 1000
objCommand.Properties(”Timeout”) = 30
objCommand.Properties(”Searchscope”) = ADS_SCOPE_SUBTREE
objCommand.Properties(”Cache Results”) = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strPCName = objRecordSet.Fields(”Name”).Value
If strPCName <> strCurrentPC then
set colOS = getobject(”winmgmts:{impersonationlevel=impersonate,(shutdown)}//” & strPCName).instancesof(”win32_operatingsystem”)
For each objOS in colOS
objOS.win32shutdown(1)
Next
End if
objRecordSet.MoveNext
Loop

======================================

This script can be easily modified to include other shutdown options which include reboots and log offs. Reference the Microsoft MSDN site: http://msdn2.microsoft.com/en-us/library/aa394058.aspx for more details on the win32shutdown class.


How do I Remotely Terminate all Windows Terminal Server Sessions with Vbscript?

‘ This vbscript finds all active Terminal Server sessions on server HOST defined
‘ below and terminates them. Does not terminate the
‘ current session if being run remotely.
‘ If running on local host, set HOST value to “.”

HOST = “.”

‘ Get disconnected sessions and log them off
sessions = DisconnectedSessions(HOST)

For each session in sessions
TerminateWinSession HOST, sessionId
Next

‘ Now get active sessions and log them off
sessions = ActiveSessions(HOST)

For each session in sessions
TerminateWinSession HOST, sessionId
Next

Sub TerminateWinSession(Host, sessionId)
Dim Sh, tmpHost
Set Sh = createobject(”WScript.Shell”)
if trim(Host)=”" Then
tmpHost = “”
Else
tmpHost = ” /SERVER:” & Host
End If
Sh.Run “%COMSPEC% /C rwinsta ” & sessionId & tmpHost, 0, False
End Sub

Function ActiveSessions(Host)
Dim tmpHost, aTmp, aTmp1(), i
if trim(Host)=”" Then
tmpHost = “”
Else
tmpHost = ” /SERVER:” & Host
End If
aTmp = Split(cmd(”qwinsta” & tmpHost & ” | find “”Active”"”), _
vbCrLf)
ReDim aTmp1(-1)
For i = 0 to UBound(aTmp)
If Left(aTmp(i),1) <> “>” Then
Redim Preserve aTmp1(UBound(aTmp1) + 1)
aTmp1(UBound(aTmp1)) = Trim(Mid(aTmp(i), 42, 6))
End If
Next
ActiveSessions = aTmp1
End Function

Function DisconnectedSessions(Host)
Dim tmpHost, aTmp, aTmp1(), i
if trim(Host)=”" Then
tmpHost = “”
Else
tmpHost = ” /SERVER:” & Host
End If
aTmp = Split(cmd(”qwinsta” & tmpHost & ” | find “”Disconnected”"”), _
vbCrLf)
ReDim aTmp1(-1)
For i = 0 to UBound(aTmp)
If Left(aTmp(i),1) <> “>” Then
Redim Preserve aTmp1(UBound(aTmp1) + 1)
aTmp1(UBound(aTmp1)) = Trim(Mid(aTmp(i), 42, 6))
End If
Next
DisconnectedSessions = aTmp1
End Function

Function Cmd(cmdline)
‘ Wrapper for getting StdOut from a console command
Dim Sh, FSO, fOut, OutF, sCmd
Set Sh = createobject(”WScript.Shell”)
Set FSO = createobject(”Scripting.FileSystemObject”)
fOut = FSO.GetTempName
sCmd = “%COMSPEC% /c ” & cmdline & ” >” & fOut
Sh.Run sCmd, 0, True
If FSO.FileExists(fOut) Then
If FSO.GetFile(fOut).Size>0 Then
Set OutF = FSO.OpenTextFile(fOut)
Cmd = OutF.Readall
OutF.Close
End If
FSO.DeleteFile(fOut)
End If
End Function

How do I disable Inactive Active Directory User Accounts with Vbscripting?

If you are a system administrator managing an Active Directory environment and you need to disable inactive user accoutns then the following vbscript may assist:

‘Queries AD for all WIndows domain accounts inactive for 12 weeks or more and disables these accounts

dsquery user -inactive 12 | dsmod user -disabled yes

‘Queries AD for all active directory disabled accounts and moves the accounts to the OU -disabled accounts. Script will not run if the # of domain accounts exceed 100
dsquery user -disabled | dsmove “ou=Disabled Accounts,dc=example,dc=net” -safety 100

Reference the Microsoft article: http://support.microsoft.com/kb/322684 for more details on dsquery and dsmod usage.


powered by FreeFind