-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGet-UpdateErrorCodesFromSQLandCreateDeviceCollections_Sanitized.ps1
More file actions
147 lines (126 loc) · 5.57 KB
/
Get-UpdateErrorCodesFromSQLandCreateDeviceCollections_Sanitized.ps1
File metadata and controls
147 lines (126 loc) · 5.57 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#
# Press 'F5' to run this script. Running this script will load the ConfigurationManager
# module for Windows PowerShell and will connect to the site.
#
# This script was auto-generated at '2/27/2020 1:20:06 PM'.
# Uncomment the line below if running in an environment where script signing is
# required.
#Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
# Site configuration
$SiteCode = "ABC" # Site code
$ProviderMachineName = "sccm-01.contoso.local" # SMS Provider machine name
# Collection creation - change to $true for device collection generation
$CreateCollections = $false
############################## REQUIRES POWERSHELL SQL MODULE##################################
#Install-module SQLServer
# Customizations
$initParams = @{}
#$initParams.Add("Verbose", $true) # Uncomment this line to enable verbose logging
#$initParams.Add("ErrorAction", "Stop") # Uncomment this line to stop the script on any errors
# Do not change anything below this line
# Import the ConfigurationManager.psd1 module
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams
}
# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}
# Function for CMErrorMessage Translation
function Get-CMErrorMessage {
[CmdletBinding()]
param
(
[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[int64]$ErrorCode
)
[void][System.Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\SrsResources.dll")
[SrsResources.Localization]::GetErrorMessage($ErrorCode,"en-US")
}
# Function to convert Error Codes
function Convert-ErrorCode {
[CmdletBinding()]
param
(
[Parameter(Mandatory=$True,ParameterSetName='Decimal')]
[int64]$DecimalErrorCode,
[Parameter(Mandatory=$True,ParameterSetName='Hex')]
$HexErrorCode
)
if ($DecimalErrorCode)
{
$hex = '{0:x}' -f $DecimalErrorCode
$hex = "0x" + $hex
$hex
}
if ($HexErrorCode)
{
$DecErrorCode = $HexErrorCode.ToString()
$DecErrorCode
}
}
$ErrorCodes = Invoke-Sqlcmd -ServerInstance $ProviderMachineName -Database "CM_$SiteCode" -Query "Select LastErrorCode from v_Update_ComplianceStatusReported where LastErrorCode is not NULL group by LastErrorCode"
Set-Location "$($SiteCode):\" @initParams
$Errortable = @()
Foreach($ErrorCode in $ErrorCodes){
#$description = Get-CMErrorMessage -ErrorCode $Errorcode.LastErrorCode
$description = Invoke-RestMethod -Method Get -Uri "https://asdlabfunctions.azurewebsites.net/api/geterrormessage?exitcode=$($ErrorCode.LastErrorCode)"
$errorObject = New-Object psobject
Add-Member -InputObject $errorObject -MemberType NoteProperty -Name DecErrorCode -Value $Errorcode.LastErrorCode
# This nasty try/catch tries to convert the signed decimals into their unsigned counterparts... except for exit code 0
Try{
Add-Member -InputObject $errorObject -MemberType NoteProperty -Name UnsignedDecErrorcode -Value $([uint32](Convert-ErrorCode -DecimalErrorCode $ErrorCode.LastErrorCode).Replace('ffffffff',''))
}Catch{
Add-Member -InputObject $errorObject -MemberType NoteProperty -Name UnsignedDecErrorcode -Value 0
}
Add-Member -InputObject $errorObject -MemberType NoteProperty -Name Description -Value $description
$Errortable += $errorObject
IF($CreateCollections){
IF(Get-CMDeviceCollection -Name "Update Error: $($ErrorCode.LastErrorCode)"){
Write-Warning "A Collection for error code $($ErrorCode.LastErrorCode) already exists. Skipping..."
}Else{
$NewCollection = New-CMDeviceCollection -LimitingCollectionName "All Desktop and Server Clients" -Name "Update Error: $($ErrorCode.LastErrorCode)" -Comment $description -RefreshType Periodic
Add-CMDeviceCollectionQueryMembershipRule -Collection $NewCollection -RuleName "$($ErrorCode.LastErrorCode)" -QueryExpression "Select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from sms_r_system inner join SMS_UpdateComplianceStatus on SMS_UpdateComplianceStatus.machineid=sms_r_system.resourceid where SMS_UpdateComplianceStatus.LastErrorCode = $($ErrorCode.LastErrorCode)"
Write-Output "Created Collection Update Error: $($ErrorCode.LastErrorCode)."
}
}
}
$Errortable | Format-Table -AutoSize
## For Offline/No connection to SQL testing
$ErrorCodes = @()
Foreach($e in @( -2147467259,
-2147418113,
-2147417848,
-2147024894,
-2147024784,
-2147023436,
-2147023293,
-2147023278,
-2147023261,
-2147023254,
-2147010798,
-2146889721,
-2145124341,
-2145124322,
-2145124318,
-2145124303,
-2145123272,
-2145123262,
-2145107940,
-2145107924,
-2145099774,
-2016411062,
-2016410860,
-2016410032,
-2016410031,
-2016410012,
-2016410008,
-2016409966,
-2016409958,
-2016409957,
-2016409844,
0)){
$i = New-Object psobject
Add-Member -InputObject $i -MemberType NoteProperty -Name LastErrorCode -Value $e
$ErrorCodes += $i
}