-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ListOfComputersCollections.ps1
128 lines (88 loc) · 4.59 KB
/
Get-ListOfComputersCollections.ps1
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import-module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
cd PYT:
#set domain and SCCM site details
[string] $Namespace = "root\SMS\site_PYT"
$SiteServer = "POSHSITE01.POSHYT.corp"
$Domain = 'POSHYT'
# Input output files names and paths
$InputFile = "c:\temp\Posh_inputs\SCCM_Collection_Discovery_Hostnames.csv"
$OutputFile = "c:\temp\Posh_outputs\SCCM_Collection_Discovery_$(get-date -f yyyy-MM-dd-HH-mm).csv"
$ErrorLog = "c:\temp\posh_outputs\SCCM_Collection_Discovery_$(get-date -f yyyy-MM-dd-HH-mm)_Errorlog.log"
#initialise error counter
$ErrorCount=0
#initialise Progress counter
$i=0
#get number of machines to check
$ComputersCount = Import-Csv $InputFile -Header Hostname
$ComputersCount = $ComputersCount.count
Import-Csv $InputFile -Header Hostname | ForEach-Object {
# copy input into variables
$hostname = $_.Hostname
#clear other variables
$ResIDQuery = $Computer = $null
$i++ # main loop progress counter
$percentageComplete = (($i / $ComputersCount) * 100)
$percentageComplete = [math]::Round($percentageComplete,1)
Write-Host -ForegroundColor Cyan "Querying Computer - $($hostname): $i of $($computerscount) - Percent complete $($percentageComplete) %"
Write-host -ForegroundColor yellow "Testing if $hostname is in AD and online"
#Find machine in AD
Try{
$Computer = (Get-ADComputer $hostname -server $Domain -ErrorAction Stop).name
Write-host -ForegroundColor Green "Found $($hostname) in AD"
}catch{
#increase error counter if something not right
$ErrorCount += 1
#print error message
$ErrorMessage = $_.Exception.Message
"Can't find $($hostname) in AD because $($ErrorMessage)" | Out-File $ErrorLog -Force -Append
Write-host -ForegroundColor RED "Can't find $($hostname) in AD because $($ErrorMessage)"
}
#check machine is in SCCM
try{
$ResIDQuery = Get-WmiObject -ComputerName $SiteServer -Namespace $Namespace -Class "SMS_R_SYSTEM" -Filter "Name='$Computer'" -ErrorAction STOP
Write-host -ForegroundColor Green "Found $($hostname) in SCCM"
}catch{
#increase error counter if something not right
$ErrorCount += 1
#print error message
$ErrorMessage = $_.Exception.Message
"Can't find $($hostname) in SCCM because $($ErrorMessage)" | Out-File $ErrorLog -Force -Append
Write-host -ForegroundColor RED "Can't find $($hostname) in SCCM because $($ErrorMessage)"
}
#If Machine in SCCM, extract collections
if($null -ne $ResIDQuery){
try{
$Collections = (Get-WmiObject -ComputerName $SiteServer -Class sms_fullcollectionmembership -Namespace $Namespace -Filter "ResourceID = '$($ResIDQuery.ResourceId)'" -ErrorAction Stop)
Write-host -ForegroundColor Green "Extracted $($hostname)'s collections"
$devicecollections = @()
ForEach ($Collection in $collections){
$colID = $Collection.CollectionID
$collectioninfo = Get-WmiObject -ComputerName $SiteServer -Namespace $Namespace -Class "SMS_Collection" -Filter "CollectionID='$colID'"
$object = New-Object -TypeName PSObject
$object | Add-Member -MemberType NoteProperty -Name "Computer" -Value $hostname
$object | Add-Member -MemberType NoteProperty -Name "Name" -Value $collectioninfo.Name
$object | Add-Member -MemberType NoteProperty -Name "Commnent" -Value $collectioninfo.Comment
$devicecollections += $object
}#end of foreach collection in collections
$devicecollections | Export-Csv $OutputFile -Append -NoTypeInformation
}catch{
#increase error counter if something not right
$ErrorCount += 1
#print error message
$ErrorMessage = $_.Exception.Message
"Can't Extract $($hostname)'s collections because $($ErrorMessage)" | Out-File $ErrorLog -Force -Append
Write-host -ForegroundColor RED "Can't Extract $($hostname)'s collections because $($ErrorMessage)"
}
}#end of if Machine is in SCCM
}#End of ForEach-object loop
If ($ErrorCount -ge1) {
Write-host "-----------------"
Write-Host -ForegroundColor Red "The script execution completed, but with errors. See $($ErrorLog)"
Write-host "-----------------"
Pause
}Else{
Write-host "-----------------"
Write-Host -ForegroundColor Green "Script execution completed without error."
Write-host "-----------------"
Pause
}