29 July 2014

POSH script to return compliancy based on collection in SCCM 2012 R2

For our Operations department, I have created a script that returns LastReboot, RebootPending and Compliancy for servers in a collection.





You execute the script with collection id as input. Output can be formatted by powershell:


PS> .\SUcompl.ps1 | Format-Table -AutoSize


ie:
d:\SCCM_scripts\SUcompl.ps1 MDT000B1 | FT -AutoSize d:\SCCM_scripts\SUcompl.ps1 MDT000B1 | sort-object -property RebootPending | FT -AutoSize d:\SCCM_scripts\SUcompl.ps1 MDT000B1 | Export-Csv report.csv -NoTypeInformation

here is the script:

<#
.SYNOPSIS
Gets the pending reboot status on computers in a sccm 2012 collection.

.DESCRIPTION
Based on a sccm 2012 collection id, pending reboot status is returned for each member in the collection.

.PARAMETER sccm collection id
The sccm 2012 Collection ID.

.EXAMPLE
PS \SUcompl.ps1 | Format-Table -AutoSize

d:\SCCM_scripts\SUcompl.ps1 MDT000B1 | FT -AutoSize
d:\SCCM_scripts\SUcompl.ps1 MDT000B1 | sort-object -property RebootPending | FT -AutoSize
d:\SCCM_scripts\SUcompl.ps1 MDT000B1 | Export-Csv report.csv -NoTypeInformation

.NOTES
Author: Bill Bjerrum
Email: billbjerrum@gmail.com
Date: 28-07-2014
Ver.: 1.00
#>
#Set required Input Parameters
Param(
[string]$CollID
)
If($CollID){}
else{
Write-Host"Required Input is missing! Collection ID."
exit
}

#Import ConfigMgr PS Module
Import-Module $env:SMS_ADMIN_UI_PATH.Replace("\bin\i386","\bin\configurationmanager.psd1"

#Get the CMSITE SiteCode
$SiteCode = Get-PSDrive -PSProvider CMSITE
Push-Location $SiteCode":"

$ComputerList = Get-CMDevice -CollectionId $CollID | Select -Property Name,ClientType

ForEach ($Computer In $ComputerList){
if ($Computer.ClientType -eq 1) {
$CBSRebootPend = $null
$PendFileRename,$Pending,$SCCM = $false,$false,$false
$WMI_OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer.Name
$LastReboot=$WMI_OS.ConvertToDateTime($WMI_OS.lastbootuptime)

# Making registry connection to the local/remote computer
$RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$Computer.Name)
# If Vista/2008 & Above query the CBS Reg Key
If ($WMI_OS.BuildNumber -ge 6001) {
$RegSubKeysCBS = $RegCon.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\").GetSubKeyNames()
$CBSRebootPend = $RegSubKeysCBS -contains "RebootPending"
}

# Query WUAU from the registry
$RegWUAU = $RegCon.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
$RegWUAURebootReq = $RegWUAU.GetSubKeyNames()
$WUAURebootReq = $RegWUAURebootReq -contains "RebootRequired"

# Query PendingFileRenameOperations from the registry
$RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
$RegValuePFRO = $RegSubKeySM.GetValue("PendingFileRenameOperations",$null)

# Closing registry connection
$RegCon.Close()

#If PendingFileRenameOperations has a value set $RegValuePFRO variable to $true
If ($RegValuePFRO) {
$PendFileRename = $true
}

$CCMClientSDK = $null
$CCMSplat = @{
NameSpace='ROOT\ccm\ClientSDK'
Class='CCM_ClientUtilities'
Name='DetermineIfRebootPending'
ComputerName=$Computer.Name
ErrorAction='SilentlyContinue' }
$CCMClientSDK = Invoke-WmiMethod @CCMSplat
If ($CCMClientSDK) {
If ($CCMClientSDK.ReturnValue -ne 0) {
Write-Warning "Error: DetermineIfRebootPending returned error code $($CCMClientSDK.ReturnValue)"
}

If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending) {
$SCCM = $true
}
}
Else {
$SCCM = $null
}

# If any of the variables are true, set $Pending variable to $true
If ($CBSRebootPend -or $WUAURebootReq -or $SCCM -or $PendFileRename) {
$Pending = $true
}

$UpdateAssigment = Get-WmiObject -Query "Select * from CCM_AssignmentCompliance" -Namespace root\ccm\SoftwareUpdates\DeploymentAgent -ComputerName $Computer.Name
If($UpdateAssigment) {
$IsCompliant = $true
$MDTCompliant = $true
$UpdateAssigment | ForEach-Object{
if($_.IsCompliant -eq $false){$MDTCompliant = $false}
}
}

# Creating Custom PSObject and Select-Object Splat
$SelectSplat = @{
Property=('Computer','OperatingSystem','LastReboot','RebootPending','Compliant')
}
New-Object -TypeName PSObject -Property @{
Computer=$WMI_OS.CSName
OperatingSystem=$WMI_OS.Caption
LastReboot=$LastReboot
RebootPending=$Pending
Compliant=$MDTCompliant
} | Select-Object @SelectSplat

#Write-Host $Computer.Name, $WMI_OS.Caption, $LastReboot, $Pending
}

}
Pop-Location


No comments:

Post a Comment