<#
# 
# This script:
#      Installs and then configures the Software Update Point role for System Center Configuration Manager Current Branch (tested on version 1511).
#      First checks if the SUP role exists, if it does it continues with configuring, if not it adds the role.
#      Once the SUP role is present it configures it. Finally it performs a Sync.
#      Note that currently some cmdlets are not yet ready to configure everything in a SUP, 
#      I've added some workarounds as a result.
#
# Niall Brady 2016/01/16 (c) windows-noob.com
#
# See the following Technet pages for details on the cmdlets
# https://technet.microsoft.com/en-us/library/dn151064%28v=sc.20%29.aspx
# https://technet.microsoft.com/en-us/library/jj821938%28v=sc.20%29.aspx
#> 
write-host "Starting script..." -ForegroundColor Yellow
# connect to ConfigMgr
#
$CMDrive='e:'
Import-Module $CMDrive'\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
$SiteCode=Get-PSDrive -PSProvider CMSite
write-host "Connecting to " -ForegroundColor White -NoNewline
write-host $SiteCode -ForegroundColor Green -NoNewLine
cd "$($SiteCode):"
write-host ", done." -ForegroundColor White
 
#
# Define some variables
# note that 'Chinese (Simplified, China)' becomes 'Chinese (Simplified, PRC)' after a sync
#
$SiteSystemServerName = 'cm01.formation.eni'
$ClientConnectionType = 'Intranet'
$WsusiisPort = '8530'
$WsusiissslPort = '8531'
$AnonymousWsusAccess
$UseProxy = $false
$UseProxyForAutoDeploymentRule = $false
$WsusSsl = $false
$RunWsusCleanupWizard = $true
$AddLanguageSummaryDetails = 'English','French'
$AddLanguageUpdateFile = 'English','French'
$AddUpdateClassification = 'Critical Updates','Security Updates','Service Packs','Update Rollups','Updates'
$RemoveLanguageUpdateFile = 'Chinese (Simplified, China)','Chinese (Simplified, PRC)','German','Japanese','Russian'
$RemoveLanguageSummaryDetails = 'Chinese (Simplified, China)','Chinese (Simplified, PRC)','German','Japanese','Russian'
$ImmediatelyExpireSupersedence = $false
$WaitMonth ='3'
$SynchronizeAction = 'SynchronizeFromMicrosoftUpdate'
$ReportingEvent  = 'DoNotCreateWsusReportingEvents'
$EnableSynchronization  = $true 
$EnableSyncFailureAlert  = $true 
$SimpleSchedule = New-CMSchedule -RecurCount 7 -RecurInterval Days
#$CustomSchedule = New-CMSchedule -Start '01/14/2015 4:00 AM' -DayOfWeek Sunday -RecurCount 1
#
# Check if the SUP Role already exists
#
write-host "Checking if SUP role exists..." -ForegroundColor Yellow

If (Get-CMSoftwareUpdatePoint -SiteCode $SiteCode -SiteSystemServerName $SiteSystemServerName)
{
write-host "The SUP role already exists on server" -ForegroundColor White -nonewline
write-host " $SiteSystemServerName " -foregroundcolor green -nonewline
write-host "will not add it again..." -foregroundcolor white
}
else
{
write-host "The SUP role does not exist on server" -ForegroundColor White -nonewline
write-host " $SiteSystemServerName " -foregroundcolor green -nonewline
write-host ". Adding it now..." -foregroundcolor white
Add-CMSoftwareUpdatePoint `
-SiteCode $SiteCode `
-SiteSystemServerName $SiteSystemServerName `
-ClientConnectionType $ClientConnectionType `
-WsusIisPort $WsusiisPort `
-WsusIisSslPort $WsusiissslPort `
-UseProxy $UseProxy `
-UseProxyForAutoDeploymentRule $UseProxyForAutoDeploymentRule `
-WsusSsl $WsusSsl | Out-null

write-host "Sleeping 30 seconds..." -ForegroundColor Yellow
# wait 30 seconds so it's gets the job done...
Start-Sleep -s 30
#
# if you want to specify the connection account unrem below, but the account must be added/created first.
# -ConnectionAccountUserName $ConnectionAccountUserName
}

#
# Configure the SUP Role further
#
write-host "Configuring the SUP role..." -ForegroundColor Yellow
Set-CMSoftwareUpdatePointComponent `
-SiteCode $SiteCode `
-SynchronizeAction $SynchronizeAction `
-ReportingEvent $ReportingEvent `
-AddLanguageSummaryDetails  $AddLanguageSummaryDetails `
-AddLanguageUpdateFile $AddLanguageUpdateFile `
-AddUpdateClassification $AddUpdateClassification `
-RemoveLanguageUpdateFile $RemoveLanguageUpdateFile `
-RemoveLanguageSummaryDetail $RemoveLanguageSummaryDetails `
-ImmediatelyExpireSupersedence $ImmediatelyExpireSupersedence `
-WaitMonth $WaitMonth `
-Schedule $SimpleSchedule `
-EnableSynchronization $true `
-EnableSyncFailureAlert $EnableSyncFailureAlert 

write-host "SUP role initial configuration done." -ForegroundColor White

# workaround for Adding the Upgrades classification	

write-host "Checking if the Upgrades software update classification is enabled" -ForegroundColor Yellow
$IsUpgradesSubscribed=Get-WmiObject -Namespace Root\SMS\Site_$SiteCode SMS_UpdateCategoryInstance -Filter "IsSubscribed = 'True' AND CategoryTypeName = 'UpdateClassification' AND LocalizedCategoryInstanceName='Upgrades'"
If ($IsUpgradesSubscribed)
{
write-host "The " -ForegroundColor White -NoNewline
write-host "Upgrades " -ForegroundColor Green -NoNewline
write-host "software update classication is already enabled, ignoring..." -ForegroundColor white
}

else
{
write-host "The " -ForegroundColor White -NoNewline
write-host "Upgrades " -ForegroundColor Green -NoNewline
write-host "software update classication is not enabled, enabling it now..." -ForegroundColor white
$wmiobject=Get-WmiObject -Namespace Root\SMS\Site_$SiteCode SMS_UpdateCategoryInstance -Filter "CategoryTypeName = 'UpdateClassification' AND LocalizedCategoryInstanceName='Upgrades'"
$wmiobject.IsSubscribed = $true
$wmiobject.Put() | Out-null
}

write-host "Setting the Run WSUS cleanup Wizard value now..." -ForegroundColor Yellow

# workaround for Adding the Run WSUS Cleanup wizard, thanks Adam Melzer for helping me out here!
$sup = Get-CMSoftwareUpdatePointComponent
$ep = $sup.EmbeddedProperties
Write-Host "Current Value: " $($ep["Call WSUS Cleanup"].Value)
If ($RunWsusCleanupWizard)
{
$ep["Call WSUS Cleanup"].Value = 1 # enable

}
else
{
$ep["Call WSUS Cleanup"].Value = 0 # disable
}
$sup.EmbeddedProperties = $ep
$sup.Put() 
Write-Host "New value: " $($ep["Call WSUS Cleanup"].Value)

write-host "Sleeping 30 seconds..." -ForegroundColor Yellow
# wait 30 seconds so it's gets the job done...
Start-Sleep -s 30

Function PerformSUPSync()
{
Invoke-WmiMethod -Namespace Root\SMS\site_$SiteCode `
-Class SMS_SoftwareUpdate `
-Name SyncNow `
-ArgumentList $true |Out-Null
}

#
# This function is thanks to Joachim Bryttmar, thanks Joachim !
# 
# http://www.infogeek.se/guider/guide-hur-man-hanterar-software-updates-component-i-sccm-2012-r2-med-powershell-och-wmi/
#


Function Set-SUPProductSubscription($ProductNames,$SubscriptionStatus)
{
Foreach ($Product in $ProductNames)
    {
    $wmiobject = Get-WmiObject -Namespace Root\SMS\site_$SiteCode -Class SMS_UpdateCategoryInstance -Filter "LocalizedCategoryInstanceName=""$Product"""
    $wmiobject.IsSubscribed = $SubscriptionStatus
    $wmiobject.Put() | Out-null
    Write-Host "Subscription for Product" $wmiobject.LocalizedCategoryInstanceName "is now set to:" $wmiobject.IsSubscribed
    }
}

write-host "Removing Products..." -ForegroundColor Yellow
Set-SUPProductSubscription "Office 2002/XP","Office 2003","Office 2007","Office 2010","Windows 7","Windows 8","Windows Server 2012","Windows Defender","Windows Internet Explorer 7 Dynamic Installer","Windows Internet Explorer 8 Dynamic Installer","Windows Server 2003, Datacenter Edition","Windows Server 2003","Windows Server 2008 R2","Windows Server 2008 Server Manager Dynamic Installer","Windows Server 2008","Windows Vista Dynamic Installer","Windows Vista","Windows XP 64-Bit Edition Version 2003","Windows XP x64 Edition","Windows XP" $False

write-host "Starting a SUP sync for categories and products..." -ForegroundColor Yellow

# Perform a Sync !
PerformSUPSync
write-host "Sleeping 12 minutes..." -ForegroundColor Yellow
# wait 12 minutes for sync so it's gets the job done...
Start-Sleep -s 720

# now add the Windows 10 Products to the SUP...
write-host "Adding Windows 10 Products to SUP..." -ForegroundColor Yellow
Set-SUPProductSubscription "Windows 10","Windows 10 and later drivers","Windows 10 and later upgrade & servicing drivers","Windows 10 Feature On Demand","Windows 10 GDR-DU","Windows 10 Language Interface Packs","Windows 10 Language Packs","Windows 10 LTSB" $True
#
write-host "Sleeping 2 minutes..." -ForegroundColor Yellow
# wait 2 minutes for sync so it's gets the job done...
Start-Sleep -s 120
write-host "Starting a SUP sync for Windows 10 updates, this WILL take some time to complete..." -ForegroundColor Yellow

# Perform a Sync !
PerformSUPSync
write-host "Sleeping 30 minutes..." -ForegroundColor Yellow
# wait 30 minutes for sync so it's gets the job done...
Start-Sleep -s 1800

# all done
write-host "The script is now exiting !." -ForegroundColor white