Sunday, May 8, 2016

Script to Checks all packages assigned to a DP and redistributes any with errors

' ****************************************************************************
'
' Copyright (c) 2013, Jonathan Bennett / AutoIt Consulting Ltd
' All rights reserved.
' http://www.autoitconsulting.com
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
' ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
' WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL AUTOIT CONSULTING LTD BE LIABLE FOR ANY
' DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
' (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
' LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
' ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
' (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
' SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'
' ****************************************************************************
'
' Purpose:   Checks all packages assigned to a DP and redistributes any with errors.
'
' Usage:     cscript.exe CM2012_DP_Redist.vbs
'
' Version:   1.0.0
'
' History:
' 1.0.0 20-Nov-2013 - Jonathan Bennett
'       First version.
'
' ****************************************************************************

' ****************************************************************************
' Global constant and variable declarations
' ****************************************************************************

Option Explicit

' CHANGEABLE VARIABLES

' The name of the CAS/Primary site server
Public Const CASServerName = "CASSERVERNAME"

' Which DP to refresh packages for - leave this blank to check ALL DPs
Public Const DPServerName = "DPSERVERNAME"

' END OF CHANGABLE VARIABLES


Public Const wbemFlagReturnImmediately = 16
Public Const wbemFlagForwardOnly = 32

Dim oSWbemServices

' ****************************************************************************
' End declarations
' ****************************************************************************

' ****************************************************************************
' Main routine
' ****************************************************************************

' Connect to CM 2012 (CAS)
If ConnectServer(CASServerName) = False Then
WScript.Echo "Unable to connect to server."
WScript.Quit 1
End If


' Find all packages with a bad status
Dim queryString
Dim dpStatuses, dpStatus, serverName, packageID, packageDPs, packageDP, nalPath

If DPServerName = "" Then
queryString = "SELECT Name,PackageID,MessageState FROM SMS_DistributionDPStatus WHERE MessageState > 2"
Else
queryString = "SELECT Name,PackageID,MessageState FROM SMS_DistributionDPStatus WHERE Name LIKE '%" & DPServerName & "%' AND MessageState > 2"
End If

Set dpStatuses = oSWbemServices.ExecQuery(queryString,, wbemFlagForwardOnly+wbemFlagReturnImmediately)
For Each dpStatus in dpStatuses
serverName = UCase(dpStatus.Name)
packageID = dpStatus.PackageID

queryString = "SELECT * FROM SMS_DistributionPoint WHERE PackageID = '" & packageID & "'"
Set packageDPs = oSWbemServices.ExecQuery(queryString,, wbemFlagForwardOnly+wbemFlagReturnImmediately)
For Each packageDP in packageDPs
nalPath = UCase(packageDP.ServerNalPath)

If InStr(1, nalPath, serverName) > 0 Then
WScript.Echo "Redistributing " & packageID & " on " & serverName & "..."
packageDP.RefreshNow = True
On Error Resume Next
packageDP.Put_
On Error Goto 0
End If

Next
Next


WScript.Quit 0


' ****************************************************************************
' Functions
' ****************************************************************************

Function ConnectServer(ByVal serverName)
If serverName = "" Then serverName = "."

Dim oSWbemLocator : Set oSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

On Error Resume Next
Set oSWbemServices = oSWbemLocator.ConnectServer(serverName, "root\sms")
If Err.Number <> 0 Then
ConnectServer = False
Exit Function
End If
On Error Goto 0

Dim ProviderLoc : Set ProviderLoc = oSWbemServices.ExecQuery("Select * FROM SMS_ProviderLocation WHERE ProviderForLocalSite='True'")

Dim Location
For Each Location In ProviderLoc
Set oSWbemServices = oSWbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
Exit For
Next
ConnectServer = True
End Function

No comments:

Post a Comment