Skip to content

Instantly share code, notes, and snippets.

@HighLibrarian
Last active August 6, 2020 12:24
Show Gist options
  • Save HighLibrarian/af4056e8bd9a74e1672fc67f8b700b67 to your computer and use it in GitHub Desktop.
Save HighLibrarian/af4056e8bd9a74e1672fc67f8b700b67 to your computer and use it in GitHub Desktop.
# Quick way to bulk deploy a baseline to alot of collections
# make sure to edit:
## BaselineName
## Collections
## Schedule
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1"
}
#Get SiteCode
$SiteCode = Get-PSDrive -PSProvider CMSITE
$ProviderMachineName = (Get-PSDrive -PSProvider CMSITE).Root
Set-location $SiteCode":"
$logfile = "$env:USERPROFILE\desktop\BaselineDeployment.log"
Function write-cmlog
{
#Define and validate parameters
[CmdletBinding()]
Param
(
#Path to the log file
[parameter(Mandatory=$True)]
[String]$LogFile,
#The information to log
[parameter(Mandatory=$True)]
[String]$Message,
#The source of the error
[parameter(Mandatory=$True)]
[String]$Component,
#The severity (1 - Information, 2- Warning, 3 - Error)
[parameter(Mandatory=$True)]
[ValidateRange(1,3)]
[Single]$Severity
)
# output to host
switch ($Severity)
{
1 {$ForegroundColor = "white" }
2 {$ForegroundColor = "DarkYellow"}
3 {$ForegroundColor = "red"}
Default {$ForegroundColor = "white"}
}
#Obtain UTC offset
$DateTime = New-Object -ComObject WbemScripting.SWbemDateTime
$DateTime.SetVarDate($(Get-Date))
$UtcValue = $DateTime.Value
$UtcOffset = $UtcValue.Substring(21, $UtcValue.Length - 21)
#Create the line to be logged
$LogLine = "<![LOG[$message]LOG]!>" +`
"<time=`"$(Get-Date -Format HH:mm:ss.fff)$($UtcOffset)`" " +`
"date=`"$(Get-Date -Format M-d-yyyy)`" " +`
"component=`"$Component`" " +`
"context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " +`
"type=`"$Severity`" " +`
"thread=`"$($pid)`" " +`
"file=`"`">"
#Write the line to the passed log file
Out-File -InputObject $LogLine -Append -NoClobber -Encoding Default -FilePath $LogFile -WhatIf:$False
write-host "$message" -ForegroundColor $ForegroundColor
}
$Baseline = Get-CMBaseline -Name "NAME OF YOUR BASELINE"
$TargetCollections = Get-CMDeviceCollection -Name "NAME OF TARGET COLLECTIONS*"
$ScheduleInterval = "Day"
$ScheduleCount = "1"
$BaselineSchedule = New-CMSchedule -RecurInterval $ScheduleInterval -RecurCount $ScheduleCount
foreach ($collection in $TargetCollections)
{
try
{
write-cmlog -LogFile $logfile -Message "Creating deployment for $($baseline.LocalizedDisplayName) on Collection name: $($collection.name)" -Component "deployment" -Severity 1
New-CMBaselineDeployment -Name $Baseline.LocalizedDisplayName -EnableEnforcement $true -Schedule $BaselineSchedule -Collection $collection -OverrideServiceWindow $true
write-cmlog -LogFile $logfile -Message "Successfully Created deployment for $($baseline.LocalizedDisplayName) on Collection name: $($collection.name)" -Component "deployment" -Severity 1
}
catch
{
write-cmlog -LogFile $logfile -Message "Something went wrong creating deployment for $($baseline.LocalizedDisplayName) on Collection name: $($collection.name)" -Component "deployment" -Severity 3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment