Skip to content

Bulk change Device Collection Refresh Cycle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Function Get-SiteCode
{
  $wqlQuery = 'SELECT * FROM SMS_ProviderLocation'
  $a = Get-WmiObject -Query $wqlQuery -Namespace 'root\sms' -ComputerName $SMSProvider
  $a | ForEach-Object -Process {
    if($_.ProviderForLocalSite)
    {
      $script:SiteCode = $_.SiteCode
    }
  }
  return $SiteCode
}

Function Convert-NormalDateToConfigMgrDate 
{
  [CmdletBinding()]
  param (
    [parameter(Mandatory = $true, ValueFromPipeline = $true)]
    [string]$starttime
  )
  return [System.Management.ManagementDateTimeconverter]::ToDMTFDateTime($starttime)
}

Function create-ScheduleToken
{
  $SMS_ST_RecurInterval = 'SMS_ST_RecurInterval'
  $class_SMS_ST_RecurInterval = [wmiclass]''
  $class_SMS_ST_RecurInterval.psbase.Path = "\\$($SMSProvider)\ROOT\SMS\Site_$($SiteCode):$($SMS_ST_RecurInterval)"
  $script:scheduleToken = $class_SMS_ST_RecurInterval.CreateInstance()
  if($scheduleToken)
  {
    $scheduleToken.DayDuration = 0
    $scheduleToken.DaySpan = 0
    $scheduleToken.HourDuration = 0
    $scheduleToken.HourSpan = 2
    $scheduleToken.IsGMT = $false
    $scheduleToken.MinuteDuration = 0
    $scheduleToken.MinuteSpan = 0
    $scheduleToken.StartTime = (Convert-NormalDateToConfigMgrDate $starttime)
  }
}

[datetime]$starttime = [datetime]::Today
$SMSProvider = 'BRGSCCM01v'
$SiteCode = Get-SiteCode
create-ScheduleToken

#$Keyword = '%' + $Keyword.Replace(' ','%') + '%'
$Collections = Get-WmiObject -Class SMS_Collection -Namespace root\sms\site_$SiteCode -Filter "CollectionType = '2' AND Name like '%Update%'"

foreach ($Collection in $Collections)
{
  try
  {
    $Coll = Get-WmiObject -Class SMS_Collection -Namespace root\sms\site_$SiteCode -ComputerName $SMSProvider -Filter "CollectionID ='$($Collection.CollectionID)'"
    $Coll = [wmi]$Coll.__PATH
    $Coll.RefreshSchedule = $scheduleToken
    $null = $Coll.put()
    Write-Verbose -Message "Successfully edited Collection $($Coll.Name)."
  }
  catch
  {
    Write-Verbose -Message "$($Coll.Name) could not be edited."
  }
}