' Check the number of remaining Server 2008 Remote Desktop Services ' Per-User CALs available ' ' This program is free software; you can redistribute it and/or ' modify it under the terms of the GNU General Public License ' as published by the Free Software Foundation; either version 2 ' of the License, or (at your option) any later version. ' ' This program is distributed in the hope that it will be useful, ' but WITHOUT ANY WARRANTY; without even the implied warranty of ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ' GNU General Public License for more details. ' ' You should have received a copy of the GNU General Public License ' along with this program; if not, write to the Free Software ' Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ' ' ' Revision History ' 2013-12-13 Barry Mulholland [basra.mulholland@gmail.com] 1.0 ' * Initial script created ' ' To execute from within NSClient++ ' '[NRPE Handlers] ' CheckTSCALUsage_2008=cscript.exe //NoLogo scripts\\CheckTSCALUsage_2008.vbs $ARG1$ $ARG2$ $ARG3$ ' ' On the check_nrpe command include the -t 60 as the licensing totals calculations take a short while ' SET Args = WScript.Arguments Dim DomainNameList(100) 'List of Domains in FQDN format Dim DCPathList(100) 'List of Domain FQDN in LDAP format Dim LDAPPathList(100) 'List of LDAP Path - LDAP://DCPath 'Checks whether the command line options are passed correctly or not IF Args.Length < 3 THEN Help WSCRIPT.QUIT(2) ELSE iTotalCALs = Args(0) iWarning = Args(1) iCritical = Args(2) 'Wscript.Echo iTotalCALs & " " & iWarning & " " & iCritical END IF ON ERROR RESUME NEXT SET objRootDSE = GetObject("LDAP://RootDSE") IF IsNull(objRootDSE) = TRUE OR IsEmpty(objRootDSE) = TRUE THEN WSCRIPT.ECHO "Current domain is not reachable" WSCRIPT.QUIT(2) END IF ON ERROR RESUME NEXT strConfigurationNC = objRootDSE.Get("defaultNamingContext") IF IsNull(strConfigurationNC) = TRUE THEN WSCRIPT.ECHO "Current domain is not reachable" WSCRIPT.QUIT(2) END IF DCPathList(0) = strConfigurationNC DomainNameList(0) = Replace(Replace(DCPathList(0),"DC=",""),",",".") LDAPPathList(i) = "LDAP://" & DCPathList(i) IsDomainAccessible = 1 On Error Resume Next Const ADS_SCOPE_SUBTREE = 2 Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Open "Provider=ADsDSOObject;" Set objCommand.ActiveConnection = objConnection objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Chase referrals") = &H60 'ADS_CHASE_REFERRALS_EXTERNAL 'WSCRIPT.ECHO "Querying Domain " & DomainNameList(i) & " for W2K8 CAL Details ..." NumOfW2K8_Valid = 0 NumOfW2K8_Expired = 0 NumOfW2K8_Total = 0 ON ERROR RESUME NEXT objCommand.CommandText = "SELECT ADsPath FROM '" & LDAPPathList(i) & "' WHERE objectCategory='person' AND objectClass='user' AND ((terminalServer='*') OR (msTSManagingLS='*' AND msTSLicenseVersion='*' AND msTSExpireDate='*'))" Set objRecordSet = objCommand.Execute 'WSCRIPT.ECHO "Total no of user objects in the domain: " & objRecordset.RecordCount IF objRecordset.RecordCount = 0 THEN IsDomainAccessible = 0 END IF objRecordSet.MoveFirst IF IsDomainAccessible = 1 THEN Do Until objRecordSet.EOF IsValidPUCAL = 0 ON ERROR RESUME NEXT strPath = objRecordSet.Fields("AdsPath").Value Set objUser = GetObject(strPath) ON ERROR RESUME NEXT Value1 = objUser.Get("terminalServer") Value1Err = ERR.number ON ERROR RESUME NEXT Value2 = objUser.Get("msTSManagingLS") Value2Err = ERR.number ON ERROR RESUME NEXT Value3 = objUser.Get("msTSLicenseVersion") Value3Err = ERR.number ON ERROR RESUME NEXT Value4 = objUser.Get("msTSExpireDate") Value4Err = ERR.number IF Value1Err = 0 AND Value2Err <> 0 AND Value3Err <> 0 AND Value4Err <> 0 THEN ' This means User Account is of Win2K3 or Older Domain Controller IsValidPUCAL = 1 ELSEIF Value1Err <> 0 AND Value2Err = 0 AND Value3Err = 0 AND Value4Err = 0 THEN ' This means User Account is of w2k8 or newer Domain Controller IF Value4 < now() THEN IsValidPUCAL = 2 ELSE IsValidPUCAL = 1 END IF ELSE ' This means User does not have License IsValidPUCAL = 0 END IF IF IsValidPUCAL = 2 THEN NumOfW2K8_Expired = NumOfW2K8_Expired + 1 ELSEIF IsValidPUCAL = 1 THEN NumOfW2K8_Valid = NumOfW2K8_Valid + 1 END IF objRecordSet.MoveNext Loop Set objRecordSet = Nothing NumOfW2K8_Total = NumOfW2K8_Valid + NumOfW2K8_Expired 'WSCRIPT.ECHO "Done!" 'WSCRIPT.ECHO "For Domain: " & DomainNameList(i) & " -- Number of W2K8 CALs - Valid: " & NumOfW2K8_Valid & " Expired: " & NumOfW2K8_Expired & " Total: " & NumOfW2K8_Total 'WSCRIPT.ECHO "" iRemainingCALs = iTotalCALs - NumOfW2K8_Valid 'WSCRIPT.ECHO "iTotalCALs: " & CInt(iTotalCALs) 'WSCRIPT.ECHO "iRemainingCALs: " & CInt(iRemainingCALs) 'WSCRIPT.ECHO "iWarning: " & CInt(iWarning) 'WSCRIPT.ECHO "iCritical: " & CInt(iCritical) IF CInt(iRemainingCALs) <= CInt(iCritical) THEN WSCRIPT.ECHO "Critical! Only " & iRemainingCALs & " CALs available" WSCRIPT.QUIT(2) ELSEIF CInt(iRemainingCALS) <= CInt(iWarning) THEN WSCRIPT.ECHO "Warning! Only " & iRemainingCALs & " CALs available" WSCRIPT.QUIT(1) ELSE WSCRIPT.ECHO "OK: " & iRemainingCALs & " CALs available" WSCRIPT.QUIT(0) END IF ELSE WSCRIPT.ECHO "Domain " & DomainNameList(i) & " is not reachable. W2K8 CAL details cannot be retrieved." WSCRIPT.ECHO "" WSCRIPT.QUIT(2) END IF SUB Help() HelpMesg = "Usage : cscript CheckTSCALUsage.vbs ..." & vbNewLine & _ "" & vbNewLine & _ "where - " & vbCrLf & _ " TotalCALs - Is the number of CALs installed on the license server." & vbNewLine & _ " Warning - Warn on this number of available CALs." & vbNewLine & _ " Critical - Critical on this number of available CALs." & vbNewLine WSCRIPT.ECHO "" & HelpMesg END SUB