forked from lazywinadmin/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove-SCCMUserDeviceAffinity.ps1
100 lines (79 loc) · 2.76 KB
/
Remove-SCCMUserDeviceAffinity.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
function Remove-SCCMUserDeviceAffinity {
<#
.SYNOPSIS
Function to remove the primary user(s) or group(s) from a device in SCCM
.DESCRIPTION
Function to remove the primary user(s) or group(s) from a device in SCCM
.PARAMETER SiteCode
Specifies the SCCM Site Code
.PARAMETER SiteServer
Specifies the SCCM Management Server
.PARAMETER DeviceName
Specifies the Resource Name on which the Primary Users need to be removed
.PARAMETER DeviceID
Specifies the Resource ID on which the Primary Users need to be removed
.PARAMETER Credential
Specifies alternative credentials to use
.EXAMPLE
$Params = @{
SiteCode = 'FXC'
SiteServer = 'SCCMServer1'
DeviceID = 'FXC00045'
Credential = (Get-Credential 'FX/SccmGuru')
}
Remove-SCCMUserDeviceAffinity @Params
.NOTES
Francois-Xavier Cat
lazywinadmin.com
@lazywinadmin
.LINK
https://github.com/lazywinadmin/PowerShell
#>
[CmdletBinding(DefaultParameterSetName = 'ResourceName')]
param
(
[Parameter(ParameterSetName = 'ResourceName')]
[Parameter(ParameterSetName = 'ResourceID')]
$SiteCode,
[Parameter(ParameterSetName = 'ResourceName',
Mandatory = $true)]
[Parameter(ParameterSetName = 'ResourceID')]
$SiteServer,
[Parameter(ParameterSetName = 'ResourceName')]
[Alias('Name', 'ResourceName')]
$DeviceName,
[Parameter(ParameterSetName = 'ResourceID')]
[Alias('ResourceID')]
$DeviceID,
[Parameter(ParameterSetName = 'ResourceName')]
[Parameter(ParameterSetName = 'ResourceID')]
[Alias('RunAs')]
[pscredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty
)
$CIMsessionSplatting = @{
ComputerName = $SiteServer
}
# Credential Specified
IF ($PSBoundParameters['Credential']) {
$CIMsessionSplatting.Credential = $Credential
}
# Create a CIM session
$CIMSession = New-CimSession @CIMsessionSplatting
# Splatting for CIM cmlets
$CIMSplatting = @{
CimSession = $CIMSession
NameSpace = "root\sms\site_$SiteCode"
ClassName = "SMS_UserMachineRelationship"
}
# Device Name Specified
IF ($PSBoundParameters['DeviceName']) {
$CIMSplatting.Filter = "ResourceName='$DeviceName' AND isActive=1 AND TYPES NOT NULL"
}
# Device ID Specified
IF ($PSBoundParameters['DeviceID']) {
$CIMSplatting.Filter = "ResourceID='$DeviceID' AND isActive=1 AND TYPES NOT NULL"
}
Get-CimInstance @CIMSplatting | Remove-CimInstance
}