PowerShell and rounding. Math is hard!

I had a student question regarding a function he wrote.  The function basically displayed disk info (disk size, free disk space and percent free), but we couldn’t get it to round the numbers.  In this first example, the numbers are not rounded:

Function Get-DiskInfo
    {        
    param ($System =".")
    $display = @{Label = "Drive" ; Expression={$_.DeviceID}},`
    @{Label = "File System"; Expression={$_.FileSystem}},`
    @{Label = "Size(GB)"; Expression={ $_.Size / 1gb }},`
    @{Label = "Free Space(GB)"; Expression={$_.FreeSpace / 1gb}},`
    @{Label = "% Free"; Expression = { ($_.FreeSpace / $_.Size) * 100}}
    Get-WmiObject -Class win32_LogicalDisk -Filter "DriveType = '3'" -ComputerName $System | Format-Table $display -auto
    }

We then tried to use the {N:02} way of rounding, but it didn’t show any results.  Example using the {N:02} on the Size(GB) column:

Function Get-DiskInfo
    {        
    param ($System =".")
    $display = @{Label = "Drive" ; Expression={$_.DeviceID}},`
    @{Label = "File System"; Expression={$_.FileSystem}},`
    @{Label = "Size(GB)"; Expression={ "{N:02}" -f $_.Size / 1gb }},`
    @{Label = "Free Space(GB)"; Expression={$_.FreeSpace / 1gb}},`
    @{Label = "% Free"; Expression = { ($_.FreeSpace / $_.Size) * 100}}
    Get-WmiObject -Class win32_LogicalDisk -Filter "DriveType = '3'" -ComputerName $System | Format-Table $display -auto
    }

After a bit of Googling I discovered the [MATH]::round function. 

Function Get-DiskInfo
    {       
    param ($System =".")
    $display = @{Label = "Drive" ; Expression={$_.DeviceID}},`
    @{Label = "File System"; Expression={$_.FileSystem}},`
    @{Label = "Size(GB)"; Expression={ [Math]::Round($_.Size / 1gb,2)}},`
    @{Label = "Free Space(GB)"; Expression={[Math]::Round($_.FreeSpace / 1gb,2)}},`
    @{Label = "% Free"; Expression = {[Math]::Round($_.FreeSpace / $_.Size * 100,2)}}
    Get-WmiObject -Class win32_LogicalDisk -Filter "DriveType = '3'" -ComputerName $System | Format-Table $display -auto
    }

Updated SCCM Computer Startup Script

In the past I have posted a VBScript that I use to install the SCCM Client and check SCCM Client health.  The updates that I have are related to fixing broken WMI.  The script is put in a GPO as a Computer Startup Script.

 

' NAME: SCCM_Client_Health_Check
' AUTHOR: Matthew Teegarden
' DATE  : 08/25/2010
' Based on the script by Chris Stauffer http://myitforum.com/cs2/blogs/cstauffer/archive/2009/12/18/sccm-sp2-health-check-startup-script-1-7.aspx
' COMMENT: Version 1.1
' 	1.0 - initial script
' 	1.1 - complete re-write.  added WMI check / Fix

Option Explicit
On Error Resume Next

Dim WshShell ' Wscript.shell
Dim fso ' FileSystemObject
Dim StrScanRun ' used for incremental count of the amount of times this script has run
Dim StrSCCMPath ' used for the location of SCCM client
Dim CCMSetupFolder ' used for the location of the CCMSetup folder
Dim SMSClientVersion ' used for storing SCCM Client Version
Dim InstallArgs ' used for SCCM Client installation switches
Dim ComSpec ' WshShell.ExpandEnvironmentStrings("%COMSPEC%")
Dim smsinstall ' SCCM Installation command
Dim strAdmAcct ' used for storing the AD Group that needs to be in the local administrators group
Dim strDomain ' stores the domain of the AD Group that needs to be in the local administrators group
Dim strGroupName ' stores the AD Group name that needs to be in the local administrators group
Dim objLocalGroup ' stores the Administrators local group
Dim strComputerName ' used for storing the computer name
Dim gstrSiteCode ' used to determine the site code of the strComptuerName
Dim Results ' used to store service state
Dim sFolder ' used to store folder path
Dim oFolder ' used to store specific folders
Dim oFile ' used for specific files
Dim WBEMFolder ' used for the wbem repository
Dim startdate ' used for SMSProvide log file access time
Dim enddate ' used for comparing the current time with last access time of the SMSProvider log file
Dim diffdate ' used to store the difference between startdate and enddate
Dim colItems ' used in For Each loops
Dim objItem ' used in For Each loops
Dim bAlreadyExists ' stores TRUE or FALSE values for strAdmAcct in the local Administrators group
Dim CurrentDirectory ' stores the location that the script is running from
Dim ScriptFullName ' stores the scripts full name.  Used with CurrentDirectory
Dim objWMI ' Used for the WMI service
Dim returncode ' used to store return codes of functions
Dim servicename ' used to pass a service name into functions
Dim Started, Stopped ' used for starting and stopping services
Dim StrSCCMInstall ' counter for how many times SCCM Client has been installed
Dim AssignedSite ' used for SCCM site assignment
Dim AutoAssignedSite ' used for auto site assignment
Dim SmsClient ' used for the SCCM WMI namespace
Dim strValueName ' used for storing registry values during the History Shift function
Dim strLogging ' used for turning logging on or off
Dim logfile ' used to write to the log file
Dim strKeyPath ' used to find the value (if exists) in the registry if the DISABLECACHEOPT and DISABLESITEOPT option was used during install
Dim strEntryName ' used to find the value (if exists) in the registry if the DISABLECACHEOPT and DISABLESITEOPT option was used during install
Dim strValue  ' ' used to find the value (if exists) in the registry if the DISABLECACHEOPT and DISABLESITEOPT option was used during install
Dim WMIisCorrupt ' used to determine if WMI is functional
Dim SomethingIsWrong ' used to determine is something is wrong!
Dim booOverWrite 

Set WshShell = CreateObject ("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

'===========File System Constants==================================
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const OverwriteExisting = True
Const HARD_DISK = 3
'===========Registry Constants=====================================
Const HKEY_LOCAL_MACHINE = &H80000002

InstallArgs = " SMSSITECODE=AUTO SMSCACHESIZE=4096"' only needed if you have not extended AD other wise enter SCCM Client Install aguments
strAdmAcct = "DOMAIN/GROUP"	'Specify Desktop SMS Admin account domain/account
strDomain = "DOMAIN"
strGroupName = "GROUP"
strComputerName = WshShell.ExpandEnvironmentStrings("%computername%")
ComSpec = WshShell.ExpandEnvironmentStrings("%COMSPEC%")
strLogging = "enabled"

' Log file
If strLogging = "enabled" Then
	Set logfile = fso.OpenTextFile("c:\windows\temp\ConfigMgrClient.Log",ForWriting,True)
	logfile.Writeline ""
	logfile.Writeline Now
	logfile.Writeline ""
End If	

'WMI Test
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMI.ExecQuery ("Select * from Win32_Service Where Name = 'WinMgmt'")
For Each objItem In colItems
	Results = objItem.name
Next
If Results = "" Or IsNull(results) Then 
	If strLogging = "enabled" Then
		logfile.Writeline "WMI Is Corrupt"
	End If
	WMIisCorrupt = "True"
	CheckSCCMInstall
	AdvCliInst(ComSpec)
	WScript.Quit
Else
	If strLogging = "enabled" Then
		logfile.Writeline "WMI is good"
	End If
Set Results = nothing
End If

' Get Local CCM Path
StrSCCMPath = WshShell.RegRead("HKLM\SOFTWARE\Microsoft\SMS\Client\Configuration\Client Properties\Local SMS Path")
If StrSCCMPath = "" Or IsNull (StrSCCMPath) Then 
	StrSCCMPath = WshShell.RegRead("HKLM\SOFTWARE\Wow6432Node\Microsoft\SMS\Client\Configuration\Client Properties\Local SMS Path")
End If	
If strLogging = "enabled" Then
	logfile.Writeline "Get local CCM Path = " & StrSCCMPath
End If
If StrSCCMPath = "" Or IsNull (StrSCCMPath) Then
	CheckSCCMInstall
	AdvCliInst(ComSpec)
	WScript.Quit
End If

' Check for SCCM Helath Check registry key and create it if it doesn't exist 
StrScanRun = WshShell.RegRead("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\Scan_Run")
If StrScanRun = "" Or IsNull (StrScanRun) Then 
	StrScanRun = WshShell.RegRead("HKLM\SOFTWARE\Wow6432Node\SCCM\SCCM_Health_Check\Scan_Run")
End If	
If StrScanRun = "" Or IsNull (StrScanRun) Then
	WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\Scan_Run"), "0"
End If	

' Write current time to the registry
WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\Last_Run"), Now()

' Check for CCMEXEC service
Results = ServiceState("ccmexec")'Check CCM Status
If strLogging = "enabled" Then
	logfile.Writeline "SMS Agent Host = " & Results
End If
If LCase(Results) = LCase("Running")Then
	WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\CCM_Service_Check"), "Running"
	If strLogging = "enabled" Then
		logfile.Writeline "SMS Agent Host = " & Results
	End If
' CCMExec serivice is running.  Do SCCM Health checks			
	Check_Client_info  'Check Client Variable	    			
	CheckSitecode      'Check Client SiteCode
	ClientVersionCheck 'Check Client Versiont 
ElseIf LCase(Results) = LCase("Stopped")Then
	If strLogging = "enabled" Then
		logfile.Writeline "SMS Agent Host = " & Results
  End If
	set Results = Nothing
	Call EnableService("ccmexec") 
	KickService("ccmexec")
	WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\CCM_Service_Check"), "CCM Service was Stopped"
	' CCMExec serivice is running.  Do SCCM Health checks					    		
	Check_Client_info 'Check Client Variable	    			
	CheckSitecode      'Check Client SiteCode
	ClientVersionCheck 'Check Client Version
ElseIf Results = "" Or IsNull (Results) Then
	If strLogging = "enabled" Then
		logfile.Writeline "SMS Agent Host not found"
  End If
	WMIisCorrupt = "True"
	CheckSCCMInstall
	AdvCliInst(ComSpec)
	WScript.Quit
End If

CheckSCCMInstall'Check to see if CCMSetup failed and fix error
Script_increment  'Increment Registery Key For script by 1 And move present value To history 		
Check_AdminShare ' Check Admin$ Share
Check_SMSLOCALADMIN'Check Local Admin account
WS-- USEnabled ' check to ensure windows update service is enabled

Function CheckSCCMInstall  
	On Error Resume Next
	booOverWrite = vbFalse
	CurrentDirectory = replace(WScript.ScriptFullName,WScript.ScriptName,"")
	If strLogging = "enabled" Then
		logfile.Writeline  "Starting CheckSCCMInstall with directory " & CurrentDirectory
	End If
	If fso.FolderExists("C:\Windows\System32\CCMSetup\LastError") Or fso.FolderExists("C:\Windows\CCMSetup\LastError") or WMIisCorrupt = "True"  or SomethingIsWrong = "True" Then
		If strLogging = "enabled" Then
			logfile.Writeline  "LastError folder exists or Can not connect to WMI.  Deinstalling client and reinstalling"
		End If
'     Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
'     Set colItems = objWMI.ExecQuery ("Select * from Win32_Service Where Name = 'CCMSETUP'")
' 		For Each objItem In colItems
' 			objItem.StopService()
' 		Next
	  WshShell.Run("Net Stop CCMSetup /Y"), 0, True
		If strLogging = "enabled" Then
			logfile.Writeline  "Stopped CCMSEtup service if existed"
		End If
' ' 		Set colItems = objWMI.ExecQuery ("Select * from Win32_Process Where Name = 'CCMSETUP.EXE'")
' ' 		For Each objItem In colItems
' ' 			objItem.Terminate()
' ' 		Next
' 		oFile = CurrentDirectory & "PSKILL.EXE"
' 		If strLogging = "enabled" Then
' 			logfile.Writeline  "Copying " & oFile
' 		End If
' 		fso.CopyFile oFile, "C:\Windows\Temp\", booOverWrite
' 		'WScript.Sleep 10000
' 		WshShell.Run "c:\Windows\Temp\pskill ccmsetup",,True
' 		If strLogging = "enabled" Then
' 			logfile.Writeline  "Stopped CCMSEtup process if existed"
' 		End If
		oFile = CurrentDirectory & "ccmclean.exe"
		If strLogging = "enabled" Then
			logfile.Writeline  "Copying " & oFile
		End If
		fso.CopyFile oFile, "C:\Windows\Temp\", booOverWrite
		'WScript.Sleep 10000
		WshShell.Run "c:\Windows\Temp\CCMClean /all /q",,True
		If strLogging = "enabled" Then
			logfile.Writeline  "Ran CCMClean 1"
		End If
		WshShell.Run "c:\Windows\Temp\CCMClean /all /q",,True
		If strLogging = "enabled" Then
			logfile.Writeline  "Ran CCMClean 2"
		End If
		oFile = CurrentDirectory & "ccmdelcert.exe"
		If strLogging = "enabled" Then
			logfile.Writeline  "Copying " & oFile
		End If
		fso.CopyFile oFile, "C:\Windows\Temp\", booOverWrite
		'WScript.Sleep 10000
		WshShell.Run "c:\Windows\Temp\ccmdelcert.exe",,True
		If strLogging = "enabled" Then
			logfile.Writeline  "Ran CCMDelCert"
		End If
		fso.DeleteFile "C:\windows\SMSCFG.INI", True
		If strLogging = "enabled" Then
			logfile.Writeline  "Delete SMSCFG.INI"
		End If
'     Set colItems = objWMI.ExecQuery("Associators of " _
' 			& "{Win32_Service.Name='Winmgmt'} Where " _
' 			& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
' 		For Each objItem In colItems 
' 			objItem.StopService()
' 		Next
' 		WScript.Sleep 20000
' 		Set colItems = objWMI.ExecQuery _
' 			("Select * from Win32_Service where Name='Winmgmt'")
' 		For Each objItem In colItems
' 			objItem.StopService()
' 		Next
		WshShell.Run("C:\Windows\System32\regsvr32.exe c:\windows\system32\atl.dll /s"),0, True
		WshShell.Run("C:\Windows\System32\rundll32.exe wbemupgd, UpgradeRepository"), 0, True
		If strLogging = "enabled" Then
			logfile.Writeline  "Ran C:\Windows\System32\rundll32.exe wbemupgd, UpgradeRepository"
		End If
		WScript.Sleep 60000
		Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
		Set colItems = objWMI.ExecQuery ("Select * from Win32_Service Where Name = 'WinMgmt'")
		For Each objItem In colItems
			Results = objItem.name
		Next
		If Results = "" Or IsNull(results) Then 
			If strLogging = "enabled" Then
				logfile.Writeline "WMI Is STILL Corrupt"
			End If
			WshShell.Run("Net Stop Winmgmt /Y"), 0, True
			If strLogging = "enabled" Then
				logfile.Writeline  "Stopped Winmgmt"
			End If	
			fso.DeleteFolder ("C:\Windows\System32\WBEM\Repository"), True
			If strLogging = "enabled" Then
				logfile.Writeline  "Deleted Repository"
			End If
			WshShell.Run("Net Start Winmgmt"), 0, True
			If strLogging = "enabled" Then
				logfile.Writeline  "Started Winmgmt"
			End If
			sFolder = WshShell.ExpandEnvironmentStrings("%windir%\System32\WBEM")
			Set oFolder = fso.GetFolder(sFolder)
			For Each oFile In oFolder.Files
				Select Case lCase(fso.GetExtensionName(oFile))
	  			Case "mof", "mfl"
	   			WshShell.Run oFolder & "\mofcomp.exe """ & oFile & """", 0, True
	   			If strLogging = "enabled" Then
						logfile.Writeline  "Compiled " & oFile
					End If
				End Select
			Next
			WshShell.Run("C:\Windows\System32\rundll32.exe wbemupgd, UpgradeRepository"), 0, True
			If strLogging = "enabled" Then
				logfile.Writeline  "Ran C:\Windows\System32\rundll32.exe wbemupgd, UpgradeRepository"
			End If		
			WScript.Sleep 60000
		End If
		fso.DeleteFolder ("C:\Windows\System32\CCMSetup"), True
		fso.DeleteFolder ("C:\Windows\CCMSetup"), True
		fso.DeleteFolder ("C:\Windows\System32\CCM"), True
		fso.DeleteFile ("C:\Windows\Temp\ccmclean.exe"), True
		fso.DeleteFile ("C:\Windows\Temp\ccmdelcert.exe"), True
' 		fso.DeleteFile ("C:\Windows\Temp\PSKILL.exe"), True
	End If
	If strLogging = "enabled" Then
		logfile.Writeline  "Ending CheckSCCMInstall"
	End If
End Function	

Function Check_Client_info
	On Error Resume Next
	If strLogging = "enabled" Then
		logfile.Writeline  "Starting Check_Client_info"
	End If
	Set SmsClient = GetObject("winmgmts:ROOT/CCM:SMS_Client=@")
	SMSClientVersion = SmsClient.ClientVersion
	If SMSClientVersion = "" Or IsNull (SMSClientVersion) Then
		SMSClientVersion = WshShell.RegRead("HKLM\SOFTWARE\Microsoft\SMS\Mobile Client\ProductVersion")
	End If
	If strLogging = "enabled" Then
		logfile.Writeline  "Ending Check_Client_info"
	End If
End Function	

Function Script_increment
	On Error Resume Next
	If strLogging = "enabled" Then
		logfile.Writeline  "starting Script_increment"
	End If
	StrScanRun = WshShell.RegRead("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\Scan_Run")
	If StrScanRun = "" Or IsNull (StrScanRun) Then
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\Scan_Run"), "0"
	Else
		StrScanRun = StrScanRun + 1
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\Scan_Run"), StrScanRun
	End If
	If StrScanRun >= 2 Then
		History_Shift
	End If
	If strLogging = "enabled" Then
		logfile.Writeline  "Ending Script_increment"
	End If
End Function	

Function Check_AdminShare
On Error Resume Next
	If strLogging = "enabled" Then
		logfile.Writeline  "starting Check_AdminShare "
	End If	
	Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	Set colItems = objWMI.ExecQuery("Select * from Win32_Share Where Name = 'ADMIN$'")
	If colItems.Count > 0 Then
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\AdminShare_Check"), "Admin$ is present"
		If strLogging = "enabled" Then
			logfile.Writeline "Admin$ is present"
		End If
	Else
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\AdminShare_Check"), "Admin$ Is missing"
		If strLogging = "enabled" Then
			logfile.Writeline "Admin$ is missing"
		End If
		Set colItems = objWMI.execquery("select state from win32_service where name='LanmanServer'")
		For Each objItem In colItems
			objItem.StopService
		Next
		Do Until Stopped = True
			Set colItems = objWMI.execquery("select state from win32_service where name='LanmanServer'")
			For Each objItem In colItems
				If lcase(objItem.State) = lcase("Stopped") Then
					Stopped = True
				End If
			Next
		Loop
		Set colItems = objWMI.execquery("select state from win32_service where name='LanmanServer'")
		For Each objItem In colItems
			objItem.StartService
		Next
	End If	
	If strLogging = "enabled" Then
		logfile.Writeline  "ending Check_AdminShare "
	End If	
End Function

Function Check_SMSLOCALADMIN
	On Error Resume Next
	If strLogging = "enabled" Then
		logfile.Writeline  "starting  Check_SMSLOCALADMIN"
	End If	
	Set objLocalGroup = GetObject("WinNT://" & strComputerName & "/Administrators, group")
	For Each objItem In objLocalGroup.Members 
	If InStr(UCase(objItem.ADSPath),UCase(strDomain & "/" & strGroupName)) <> 0 Then
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\Account_Check"), strAdmAcct & " is in the Admins group"
		If strLogging = "enabled" Then
			logfile.Writeline strAdmAcct & " is in the Admins group"
		End If
		bAlreadyExists = True
	End If
	Next
	If bAlreadyExists <> True Then
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\Account_Check"), strAdmAcct & " was not in Admins group - adding"
		If strLogging = "enabled" Then
			logfile.Writeline  strAdmAcct & " was not in Admins group - adding"
		End If
		objLocalGroup.Add("WinNT://" & strDomain & "/" & strGroupName)
	End If  
	If strLogging = "enabled" Then
		logfile.Writeline  "starting  Check_SMSLOCALADMIN"
	End If	
End Function

Function WS-- USEnabled
	On Error Resume Next
	If strLogging = "enabled" Then
		logfile.Writeline  "starting WS-- USEnabled"
	End If
	Results = ServiceState("wuauserv")'Check Windows Update Status
	If LCase(Results) = LCase("Running")Then
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\WindowsUpdate_Service_Check"), "Running"
		If strLogging = "enabled" Then
			logfile.Writeline "Auto Updates Serivice = " & Results
		End If
	ElseIf LCase(Results) = LCase("Stopped")Then
		If strLogging = "enabled" Then
			logfile.Writeline "Auto Updates Serivice = " & Results
		End If
		Call EnableService("wuauserv") 
		KickService("wuauserv")
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\WindowsUpdate_Service_Check"), "Windows Update Service was Stopped"
	End If
	Set Results = Nothing
	If strLogging = "enabled" Then
		logfile.Writeline  "ending WS-- USEnabled"
	End If
End Function	 

Function ClientVersionCheck 
	On Error Resume Next
	If strLogging = "enabled" Then
		logfile.Writeline  "starting ClientVersionCheck "
	End If
	If SMSClientVersion = "" Then
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\CCM_Client_Check"), "Client Not Installed"
		If strLogging = "enabled" Then
			logfile.Writeline "SMSClientVersion = NO CLIENT"
		End If
		Call AdvCliInst(ComSpec)
		WScript.Quit
	Else
		If strLogging = "enabled" Then
			logfile.Writeline "SMSClientVersion = " & SMSClientVersion
		End If
		Select Case SMSClientVersion
				' RTM 4.00.5931.0001
				' SP1 4.00.6221.1000
				' SP2 4.00.6487.2000
				' R3  4.00.6487.2157
			Case "4.00.6487.2157" ' SCCM SP2 R3 client version
				PolicyEval
				ContentTransferMangerEval
				InventoryAgentEval
			Case "4.00.6487.2000" ' SCCM SP2 client version
				If strLogging = "enabled" Then
					logfile.Writeline "Needs R3 hotfix"
				End If
				CurrentDirectory = replace(WScript.ScriptFullName,WScript.ScriptName,"")
				smsinstall =ComSpec & " /c msiexec.exe /p "& CurrentDirectory & "SMSClient\i386\hotfix\KB977384\sccm2007ac-sp2-kb977384-x86-enu.msp /L*v %TEMP%\sccm2007ac-sp2-kb977384-x86-enu.msp.LOG /q REINSTALL=ALL REINSTALLMODE=mous"
			  WshShell.Run smsinstall,0,False
			  If strLogging = "enabled" Then
					logfile.Writeline "Install R3 hotfix"
				End If
				PolicyEval
				ContentTransferMangerEval
				InventoryAgentEval
		Case Else
			If strLogging = "enabled" Then
				logfile.Writeline  "ClientVersionCheck could not determine client version - installing client"
			End If
' 			WshShell.Run("Net Stop CCMSetup /Y"), 0, True
' 			If strLogging = "enabled" Then
' 				logfile.Writeline  "Stopped CCMSEtup service if existed"
' 			End If
' ' 		Set colItems = objWMI.ExecQuery ("Select * from Win32_Process Where Name = 'CCMSETUP.EXE'")
' ' 		For Each objItem In colItems
' ' 			objItem.Terminate()
' ' 		Next
			oFile = CurrentDirectory & "PSKILL.EXE"
			fso.CopyFile oFile, "C:\Windows\Temp", True
			WshShell.Run "c:\Windows\Temp\pskill ccmsetup",,True
			If strLogging = "enabled" Then
				logfile.Writeline  "Stopped CCMSEtup process if existed"
			End If
			oFile = CurrentDirectory & "ccmclean.exe"
			fso.CopyFile oFile, "C:\Windows\Temp", True
			WshShell.Run "c:\Windows\Temp\CCMClean /all /q",,True
			If strLogging = "enabled" Then
				logfile.Writeline  "Ran CCMClean 1"
			End If
			WshShell.Run "c:\Windows\Temp\CCMClean /all /q",,True
			If strLogging = "enabled" Then
				logfile.Writeline  "Ran CCMClean 2"
			End If
			oFile = CurrentDirectory & "ccmdelcert.exe"
			fso.CopyFile oFile, "C:\Windows\Temp", True
			WshShell.Run "c:\Windows\Temp\ccmdelcert.exe",,True
			If strLogging = "enabled" Then
				logfile.Writeline  "Ran CCMDelCert"
			End If
			fso.DeleteFile "C:\windows\SMSCFG.INI", True
			If strLogging = "enabled" Then
				logfile.Writeline  "Delete SMSCFG.INI"
			End If
			fso.DeleteFolder ("C:\Windows\System32\CCMSetup"), True
			fso.DeleteFolder ("C:\Windows\CCMSetup"), True
			fso.DeleteFile ("C:\Windows\Temp\ccmclean.exe"), True
			fso.DeleteFile ("C:\Windows\Temp\ccmdelcert.exe"), True
			fso.DeleteFile ("C:\Windows\Temp\PSKILL.exe"), True
      Call AdvCliInst(ComSpec)
      WScript.Quit
    End Select
	End If
	If strLogging = "enabled" Then
		logfile.Writeline  "ending ClientVersionCheck "
	End If
End Function	   

Function PolicyEval 	
	On Error Resume Next	
	If strLogging = "enabled" Then
		logfile.Writeline  "starting PolicyEval  "
	End If
	Set oFile = fso.GetFile (StrSCCMPath & "Logs\CCMExec.log")
	startdate = ofile.DateLastModified
	enddate = date()
	If isDate(startdate) Then
		diffdate = DateDiff("d", startdate, enddate)
		If strLogging = "enabled" Then
			logfile.Writeline "CCMExec.log last accessed = " & diffdate
		End If
	End If
	If diffdate > 21 Then
		If strLogging = "enabled" Then
			logfile.Writeline "CCMExec.log last accessed more than 21 days ago. Repair client"
		End If
		SomethingIsWrong = "True"
		call CheckSCCMInstall
		call AdvCliInst(ComSpec)
		wscript.quit
	End If
	If strLogging = "enabled" Then
		logfile.Writeline  "ending PolicyEval  "
	End If
End Function	

Function ContentTransferMangerEval	
	On Error Resume Next	
	If strLogging = "enabled" Then
		logfile.Writeline  "starting ContentTransferManager.log Eval  "
	End If
	Set oFile = fso.GetFile (StrSCCMPath & "Logs\ContentTransferManager.log")
	startdate = ofile.DateLastModified
		enddate = date()
	If isDate(startdate) Then
		diffdate = DateDiff("d", startdate, enddate)
		If strLogging = "enabled" Then
			logfile.Writeline "ContentTransferManager.log last accessed = " & diffdate
		End If
	End If
	If diffdate > 45 Then
		If strLogging = "enabled" Then
			logfile.Writeline "ContentTransferManager.log last accessed more than 45 days ago. Repair client"
		End If
		SomethingIsWrong = "True"
		call CheckSCCMInstall
		call AdvCliInst(ComSpec)
		wscript.quit
	End If
	If strLogging = "enabled" Then
		logfile.Writeline  "ending ContentTransferManager.log eval  "
	End If
End Function	 

Function InventoryAgentEval	
	On Error Resume Next	
	If strLogging = "enabled" Then
		logfile.Writeline  "starting InventoryAgent.log Eval  "
	End If
	Set oFile = fso.GetFile (StrSCCMPath & "Logs\InventoryAgent.log")
	startdate = ofile.DateLastModified
		enddate = date()
	If isDate(startdate) Then
		diffdate = DateDiff("d", startdate, enddate)
		If strLogging = "enabled" Then
			logfile.Writeline "InventoryAgent.log last accessed = " & diffdate
		End If
	End If
	If diffdate > 10 Then
		If strLogging = "enabled" Then
			logfile.Writeline "InventoryAgent.log last accessed more than 10 days ago. Repair client"
		End If
		SomethingIsWrong = "True"
		call CheckSCCMInstall
		call AdvCliInst(ComSpec)
		wscript.quit
	End If
	If strLogging = "enabled" Then
		logfile.Writeline  "ending InventoryAgent.log eval  "
	End If
End Function	   

Function CheckSitecode  
' 	On Error Resume Next	
' 	If strLogging = "enabled" Then
' 		logfile.Writeline  "starting CheckSitecode   "
' 	End If		
' 	'Ensure that the client is auto assigned to sites and that the client is assigned to the correct site
' 	Set SmsClient = CreateObject ("Microsoft.SMS.Client")
' 	AssignedSite = SmsClient.GetAssignedSite
' 	If ((Len(strComputerName) = 13) And (Mid(strComputerName, 7, 1) = "X")) Then
' 				gstrSiteCode = Mid(strComputerName, 3, 4)
' 	end If
' 	WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_AssignedSite"), AssignedSite
' 	If strLogging = "enabled" Then
' 		logfile.Writeline "SCCM Assigned Site = " & AssignedSite
' 	End If
' 	Set objWMI = GetObject("winmgmts:ROOT/CCM:SMS_Client=@")
' 	AutoAssignedSite = objWMI.EnableAutoAssignment
' 	If AutoAssignedSite = True Then
' 		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_AutoAssignment"), "TRUE"
' 		If strLogging = "enabled" Then
' 			logfile.Writeline "SCCM Auto Assignement = TRUE"
' 		End If
' 	Else 
' 		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_AutoAssignment"), "FALSE"
' 		If strLogging = "enabled" Then
' 			logfile.Writeline "SCCM Auto Assignement = FALSE"
' 		End If
' 		SmsClient.EnableAutoAssignment 1
' 	End If
' 	If gstrSiteCode = "MN00" Then
' 		If AssignedSite <> "P01" Then
' 			SmsClient.SetAssignedSite("P01")
' 		End If	
' 	Else
' 		If AssignedSite <> "CEN" Then
' 			SmsClient.SetAssignedSite("CEN")
' 		End If	
' 	End If 
' 	If strLogging = "enabled" Then
' 		logfile.Writeline  "ending CheckSitecode   "
' 	End If	
End Function

Function KickService(servicename)
	On Error Resume Next
	If strLogging = "enabled" Then
		logfile.Writeline  "starting KickService " & servicename
	End If	
	Results = ServiceState(servicename)
	Set objWMI = getobject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	If Not LCase(Results) = LCase("Running")Then
		Set colItems = objWMI.execquery("select state from win32_service where name='" & servicename & "'")
		For Each objItem In colItems
			' Start Service
			objItem.StartService
		Next
' 		Do Until Started = True
' 			Set colItems = objWMI.execquery("select state from win32_service where name='" & servicename & "'")
' 			For Each objItem In colItems
' 				If lcase(objItem.State) = lcase("Running") Then
' 					Started = True
' 				end If
' 			Next
' 		Loop
	End If 
	set Results = Nothing
	If strLogging = "enabled" Then
		logfile.Writeline  "ending KickService " & servicename
	End If	
End Function  

Function EnableService(servicename)
	On Error Resume Next
	If strLogging = "enabled" Then
		logfile.Writeline  "starting EnableService " & servicename
	End If
	Set objWMI = GetObject("winmgmts:root\cimv2:Win32_Service.Name='" & servicename & "'")
		Results = objWMI.ChangeStartMode("automatic")
	If strLogging = "enabled" Then
		logfile.Writeline  "ending EnableService " & servicename
	End If
End Function

Function ServiceState(servicename)
 	On Error Resume Next
 	If strLogging = "enabled" Then
		logfile.Writeline  "starting ServiceState " & servicename
	End If	
 	Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
   Set colItems = objWMI.execquery("select state from win32_service where name='" & servicename & "'")
   For Each objItem In colItems
   	Results = objItem.State
   Next
   If strLogging = "enabled" Then
		logfile.Writeline  servicename & " is " & Results
	 End If	
     ServiceState = Results
    set Results = Nothing
 	If strLogging = "enabled" Then
		logfile.Writeline  "ending ServiceState " & servicename
	End If	
End Function

Sub AdvCliInst(ComSpec)
	On Error Resume Next
	If strLogging = "enabled" Then
		logfile.Writeline  "starting AdvCliInst"
	End If	
	If ComSpec = "" Then
		WScript.Quit
	Else
		CurrentDirectory = replace(WScript.ScriptFullName,WScript.ScriptName,"")
		smsinstall =ComSpec & " /c "& CurrentDirectory & "SMSClient\CCMSetup.exe" & InstallArgs
		SCCM_Install
	  WshShell.Run smsinstall,0,True
	  If strLogging = "enabled" Then
			logfile.Writeline  "Starting CCMSetup"
		End If	
		Do While GetObject("WinMgmts:/root/cimV2").ExecQuery("SELECT * FROM Win32_Process WHERE Name='CCMSetup.exe'").Count
			WScript.Sleep 1000
		Loop
		If strLogging = "enabled" Then
			logfile.Writeline  "Ending CCMSetup"
		End If	
		If strLogging = "enabled" Then
			logfile.Writeline  "Starting R3 patch"
		End If	
		smsinstall =ComSpec & " /c msiexec.exe /p "& CurrentDirectory & "SMSClient\i386\hotfix\KB977384\sccm2007ac-sp2-kb977384-x86-enu.msp /L*v %TEMP%\sccm2007ac-sp2-kb977384-x86-enu.msp.LOG /q REINSTALL=ALL REINSTALLMODE=mous"
	  WshShell.Run smsinstall,0,True
	  If strLogging = "enabled" Then
			logfile.Writeline  "Ending R3 patch"
		End If	
	End If
	If strLogging = "enabled" Then
		logfile.Writeline  "ending AdvCliInst"
	End If	
End Sub

Function SCCM_Install
	On Error Resume Next 
	If strLogging = "enabled" Then
		logfile.Writeline  "starting SCCM_Install"
	End If
	StrSCCMInstall = WshShell.RegRead("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_Install")	
	If StrSCCMInstall = "" Or IsNull (StrSCCMInstall) Then
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_Install"), "1"
	Else
		StrSCCMInstall = WshShell.RegRead("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_Install")
		StrSCCMInstall = StrSCCMInstall + 1
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_Install"), StrSCCMInstall
	End If
	If strLogging = "enabled" Then
		logfile.Writeline  "Ending SCCM_Install"
	End If
End Function

Function History_Shift
	On Error Resume Next
	If strLogging = "enabled" Then
		logfile.Writeline  "starting History_Shift"
	End If
	' CCM service History move
	strValueName = WshShell.RegRead("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\CCM_Service_Check")
	If strValueName = "" Or IsNull (strValueName) Then 
		strValueName = "CCM Service was Stopped"
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\CCM_Service_Check"), strValueName
	Else
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\HS_CCM_Service_Check"), strValueName
	End If	
	Set strValueName = Nothing	
	' Admin$ Check History move	
	strValueName = WshShell.RegRead("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\AdminShare_Check")
	If strValueName = "" Or IsNull (strValueName) Then 
		strValueName = "Admin$ Is missing"
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\AdminShare_Check"), strValueName
	Else
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\HS_AdminShare_Check"), strValueName
	End If	
	Set strValueName = Nothing
	'Account Check History move		
	strValueName = WshShell.RegRead("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\Account_Check")
	If strValueName = "" Or IsNull (strValueName) Then 
		strValueName = strAdmAcct & " was not in Admins group"
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\Account_Check"), strValueName
	Else
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\HS_Account_Check"), strValueName
	End If	
	Set strValueName = Nothing
	'Windows Update Service History move		
	strValueName = WshShell.RegRead("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\WindowsUpdate_Service_Check")
	If strValueName = "" Or IsNull (strValueName) Then 
		strValueName = "Windows Update service was not running"
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\WindowsUpdate_Service_Check"), strValueName
	Else
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\HS_WindowsUpdate_Service_Check"), strValueName
	End If	
	Set strValueName = Nothing
	'SCCM_AssignedSite History move		
	strValueName = WshShell.RegRead("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_AssignedSite")
	If strValueName = "" Or IsNull (strValueName) Then 
		strValueName = "No Assigned Site"
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_AssignedSite"), strValueName
	Else
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\HS_SCCM_AssignedSite"), strValueName
	End If	
	Set strValueName = Nothing
	'SCCM_AutoAssignment History move		
	strValueName = WshShell.RegRead("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_AutoAssignment")
	If strValueName = "" Or IsNull (strValueName) Then 
		strValueName = "SCCM Autoassignment failure"
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\SCCM_AutoAssignment"), strValueName
	Else
		WshShell.RegWrite ("HKLM\SOFTWARE\SCCM\SCCM_Health_Check\HS_SCCM_AutoAssignment"), strValueName
	End If	
	Set strValueName = Nothing
	If strLogging = "enabled" Then
		logfile.Writeline  "ending History_Shift"
	End If
End Function