Reguła Przeniesienie serwera usługi AD między lokacjami
AD_Server_Moved_Site (Rule)
Knowledge Base article:
Podsumowanie
Reguła przeniesienia serwera usługi AD między lokacjami. Ta reguła sprawdza, czy kontroler domeny został przeniesiony między lokacjami od czasu ostatniego uruchomienia tego skryptu. Możesz wyłączyć tę regułę, jeśli nie potrzebujesz wiedzieć, kiedy kontroler domeny zmienia swoją lokację.
<Rule ID="AD_Server_Moved_Site" Enabled="onStandardMonitoring" Target="AD2008Core!Microsoft.Windows.Server.2008.AD.DomainControllerRole" ConfirmDelivery="false" Remotable="false" Priority="Normal" DiscardLevel="100">
<Category>Maintenance</Category>
<DataSources>
<DataSource ID="Scheduler" TypeID="System!System.SimpleScheduler">
<IntervalSeconds>86400</IntervalSeconds>
<SyncTime>00:01</SyncTime>
</DataSource>
</DataSources>
<WriteActions>
<WriteAction ID="WA" TypeID="Windows!Microsoft.Windows.ScriptWriteAction">
<ScriptName>AD_Server_Moved_Site.vbs</ScriptName>
<Arguments>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/NetworkName$ $Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/NetbiosComputerName$ false $Target/ManagementGroup/Id$</Arguments>
<ScriptBody><Script>
'*************************************************************************
' Script Name - AD Server Changed Site
'
' Purpose - Checks to see if the server has changed sites.
'
' Assumptions - Script is run by a timed event
'
' Parameters - LogSuccessEvent - Whether to log an event when the script
' completes successfully
'
' (c) Copyright 2001, Microsoft Corporation, All Rights Reserved
' Proprietary and confidential to Microsoft Corporation
'*************************************************************************
Sub Main()
On Error Resume Next
Dim IsTargetAgentless, bLogSuccessEvent, dtStart, TargetFQDNComputer, TargetNetbiosComputer
IsTargetAgentless = False
Err.Clear
IsTargetAgentless=false
If Not(IsTargetAgentless) Then
dtStart = Now
TargetFQDNComputer = oParams(0)
TargetNetbiosComputer = oParams(1)
bLogSuccessEvent = CBool(oParams(2))
Dim strStoredSiteGUID
strStoredSiteGUID = GetData("Site GUID")
Dim strComputer
strComputer = TargetFQDNComputer
Dim oRootDSE
Set oRootDSE = GetObject("LDAP://" & strComputer & "/RootDSE")
If 0 = Err.Number Then
Dim strConfigDN
strConfigDN = oRootDSE.Get("configurationNamingContext")
Dim oADOConn
Set oADOConn = CreateObject("ADODB.Connection")
oADOConn.Provider = "ADSDsOObject"
oADOConn.Open "ADs Provider"
Dim rsServers
Set rsServers = oADOConn.Execute("<LDAP://" & strComputer & "/CN=Sites," & strConfigDN & ">;(cn=" & TargetNetbiosComputer & ");adspath;subtree")
If 0 = Err.Number Then
' Bind to the computer then get it's parent (should be serversContainer) then
' bind to the parent of that object which will be the site.
Dim oComputer
Do Until rsServers.EOF Or IsObject(oComputer)
Set oComputer = GetObject(rsServers.Fields("adspath"))
rsServers.MoveNext
Loop
Set rsServers = nothing
Dim oSite
If IsObject(oComputer) Then
Dim oServersContainer
Set oServersContainer = GetObject(oComputer.Parent)
If IsObject(oServersContainer) Then
Set oSite = GetObject(oServersContainer.Parent)
End If
Set oServersContainer = nothing
Else
CreateEvent EVENTID_COULD_NOT_FIND_OBJECT, EVENT_TYPE_WARNING, "The script '" & SCRIPT_NAME & "' could not find " & _
strComputer & " in Active Directory."
bLogSuccess = False
End If
Set oComputer = nothing
Dim strSiteGUID
If IsObject(oSite) Then
' Get the objectGUID from the site.
strSiteGUID = oSite.GUID
Else
CreateEvent EVENTID_COULD_NOT_FIND_OBJECT, EVENT_TYPE_WARNING, "The script '" & SCRIPT_NAME & "' could not find the "& _
"site containing " & strComputer & " in Active Directory."
bLogSuccess = False
End If
' If the stored value is not empty then compare with the siteGUID. If they are different
' then create an informational event and store the new guid.
If Len(strSiteGUID) > 0 Then
If Len(strStoredSiteGUID) > 0 Then
If Trim(LCase(strSiteGUID)) <> Trim(LCase(strStoredSiteGUID)) Then
' Find the name of the original site
Dim rsSites
Set rsSites = oADOConn.Execute("<LDAP://" & strComputer & "/CN=Sites, " & strConfigDN & ">;(objectCategory=Site);cn,adspath;subtree")
Dim strOriginalSite
strOriginalSite = "Original Site has been deleted"
Dim oTempSite
Do Until rsSites.EOF
Set oTempSite = GetObject(rsSites.Fields("adspath"))
If 0 = Err.Number Then
If oTempSite.GUID = strStoredSiteGUID Then
strOriginalSite = rsSites.Fields("cn")
End If
End If
rsSites.MoveNext
Loop
Set rsSites = nothing
CreateEvent EVENTID_SERVER_CHANGED_SITES, EVENT_TYPE_INFORMATION, "The server (" & strComputer & ") has changed sites." & vbCrLf & _
"Original Site : " & strOriginalSite & vbCrLf & _
"Current Site : " & oSite.Get("cn")
bLogSuccessEvent = False
End If
End If
SetData "Site GUID", strSiteGUID
End If
Set oSite = nothing
Else ' oADOConn.Execute(...)
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script '" & SCRIPT_NAME & "', failed to execute the following query:" & vbCrLf & _
"<LDAP://" & strComputer & "/CN=Sites," & strConfigDN & ">;(cn=" & TargetNetbiosComputer & ");adspath;subtree" & _
vbCrLf & GetErrorString(Err)
bLogSuccess = False
End If
Else ' GetObject("LDAP://" & strComputer & "/RootDSE")
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script '" & SCRIPT_NAME & "' failed to bind to the RootDSE of " & _
strComputer & "." & vbCrLf & GetErrorString(Err)
bLogSuccess = False
End If
If bLogSuccessEvent THen
CreateEvent EVENTID_SUCCESS, EVENT_TYPE_INFORMATION, "The script '" & SCRIPT_NAME & "' completed " & _
"successfully in " & DateDiff("s", Now, dtStart) & " seconds."
End If
Else
CreateEvent EVENT_ID_AGENTLESS, EVENT_TYPE_ERROR, "The AD Management Pack does not support the agentless management mode." & vbCrLf & _
"The script '" & SCRIPT_NAME & "' will not execute." & vbCrLf & _
"To prevent this alert being generated again, either change the monitoring " & _
"mode of the computer '" & TargetFQDNComputer & "' to agent-managed " & _
"or disable the rule that generated this alert."
End If
End Sub
'******************************************************************************
' Name: CreateEvent
'
' Purpose: To create a MOM event
'
' Arguments: lngEventID, the event ID
' lngEventType, the event type (see values at top of file)
' strMessage, the message text for the event
'
' Returns: None
'
Sub CreateEvent(lngEventID, lngEventType, strMessage)
On Error Resume Next
oAPI.LogScriptEvent "AD Server Changed Site", lngEventID, lngEventType, strMessage
End Sub
'******************************************************************************
Function GetData(strKey)
'
' Purpose: Retrieves data out of a varset. Uses the key to determine what
' data to retrieve.
'
' Arguments: strKey, the key of the data to retrieve
'
' Returns: String, the data to return or an empty string
'
On Error Resume Next
If IsNull(oReg) Then
Set oReg = CreateObject("WScript.Shell")
End If
Dim regData
regData = oReg.RegRead(REG_Key & "\" & strKey )
If IsNull(regData) or IsEmpty(regData) or regData = "" Then
GetData = ""
Else
GetData = regData
End If
Err.Clear
End Function
'******************************************************************************
Sub SetData(strKey, strData)
'
' Purpose: To store data in a varset. If the key exists then the data
' associated with that key is replaced, otherwise the key/data
' combination is added to the varset.
'
' Arguments: strKey, the key of the line to replace
' strData, the data to associate with the key
'
' Returns: Nothing
'
If IsNull(oReg) Then
Set oReg = CreateObject("WScript.Shell")
End If
Call oReg.RegWrite(REG_Key & "\" & strKey , strData )
Err.Clear
End Sub
'******************************************************************************
' Name: IsServiceRunning
'
' Purpose: To determine whether a given service is running
'
' Arguments: strServiceName, the name of the service that is to be checked
'
' Returns: Boolean, True if the service is in the running state.
'
Function IsServiceRunning(strServiceName)
On Error Resume Next
Dim objServiceSet
Dim objService
Dim strQuery
IsServiceRunning = False
'Get the status of Service
strQuery = "select * from Win32_Service where Name='" & strServiceName & "' and State='Running'"
Set objServiceSet = GetObject("winmgmts:").ExecQuery(strQuery)
If Err = 0 Then
For Each objService In objServiceSet
IsServiceRunning = True
Next
Else
' Must cancel the error handling before raising an exception
On Error Goto 0
Err.Raise Err.number, Err.Source, "While executing '" & strQuery & _
"', the following error occurred:" & vbCrLf & GetErrorString(Err)
End If
End Function
'******************************************************************************
' Name: GetErrorString
'
' Purpose: Attempts to find the description for an error if no description
' is passed in.
'
' Parameters: oError - the error object
'
' Return: String, the description for the error. (Includes the error code.)
'
Function GetErrorString(oError)
Dim lErrNumber, strErrDescription
lErrNumber = oError.Number
strErrDescription = oError.Description
On Error Resume Next
If 0 >= Len(strErrDescription) Then
' If we don't have an error description, then check to see if the error
' is a 0x8007xxxx error. If it is, then look it up.
Const ErrorMask = &HFFFF0000
Const HiWord8007 = &H80070000
Const LoWordMask = 65535 ' This is equivalent to 0x0000FFFF
If (lErrNumber And ErrorMask) = HiWord8007 Then
Dim oShell
Set oShell = CreateObject("WScript.Shell")
If IsObject(oShell) Then
Dim oExec
Set oExec = oShell.Exec("net helpmsg " & (lErrNumber And LoWordMask))
Dim strMessage, i
Do
strMessage = oExec.stdout.ReadLine()
i = i + 1
Loop While (Len(strMessage) = 0) And (i < 5)
strErrDescription = strMessage
End If
End If
End If
GetErrorString = "The error returned was '" & strErrDescription & "' (0x" & Hex(lErrNumber) & ")"
End Function