diff --git a/src/Public/Add-CCMGroup.ps1 b/src/Public/Add-CCMGroup.ps1 index b81ff9e..93f1f5c 100644 --- a/src/Public/Add-CCMGroup.ps1 +++ b/src/Public/Add-CCMGroup.ps1 @@ -82,18 +82,8 @@ function Add-CCMGroup { $body = @{ Name = $Name Description = $Description - Groups = if (-not $processedGroups) { - @() - } - else { - @(, $processedGroups) - } - Computers = if (-not $processedComputers) { - @() - } - else { - @(, $processedComputers) - } + Groups = @($processedGroups) + Computers = @($processedComputers) } | ConvertTo-Json $irmParams = @{ diff --git a/src/Public/Add-CCMGroupMember.ps1 b/src/Public/Add-CCMGroupMember.ps1 index 24bfdff..ee8a1e5 100644 --- a/src/Public/Add-CCMGroupMember.ps1 +++ b/src/Public/Add-CCMGroupMember.ps1 @@ -62,8 +62,7 @@ function Add-CCMGroupMember { $ComputerCollection = [System.Collections.Generic.List[psobject]]::new() $GroupCollection = [System.Collections.Generic.List[psobject]]::new() - $id = Get-CCMGroup -Group $name | Select-Object -ExpandProperty Id - $current = Get-CCMGroup -Id $id | Select-Object * + $current = Get-CCMGroupMember -Group $name | Select-Object * $current.computers | ForEach-Object { $ComputerCollection.Add([pscustomobject]@{computerId = "$($_.computerId)" }) } } @@ -71,47 +70,44 @@ function Add-CCMGroupMember { switch ($PSCmdlet.ParameterSetName) { { $Computer } { foreach ($c in $Computer) { - if ($c -in $current.computers.computerName) { - Write-Warning "Skipping $c, already exists" + $computerId = $computers | Where-Object { $_.Name -eq $c } | Select-Object -ExpandProperty Id + if (-not $computerId) { + Write-Warning "A computer with the name $c could not be found, skipping adding it to the group" + continue + } + + if ($computerId -in $current.computers.computerId) { + Write-Warning "Skipping $c, already exists in this group" } else { - $Cresult = $computers | Where-Object { $_.Name -eq "$c" } | Select-Object Id - $ComputerCollection.Add([pscustomobject]@{ computerId = "$($Cresult.Id)" }) + $ComputerCollection.Add([pscustomobject]@{ computerId = $computerId }) } } - - $processedComputers = $ComputerCollection } 'Group' { foreach ($g in $Group) { - if ($g -in $current.groups.subGroupName) { - Write-Warning "Skipping $g, already exists" + $gId = $groups | Where-Object { $_.Name -eq $g } | Select-Object -ExpandProperty Id + if (-not $gId) { + Write-Warning "A group with the name $g could not be found, skipping adding it to the group" + continue + } + + if ($gId -in $current.groups.subGroupId) { + Write-Warning "Skipping $g, already exists in this group" } else { - $Gresult = $groups | Where-Object { $_.Name -eq $g } | Select-Object Id - $GroupCollection.Add([pscustomobject]@{ subGroupId = "$($Gresult.Id)" }) + $GroupCollection.Add([pscustomobject]@{ subGroupId = $gId }) } } - $processedGroups = $GroupCollection } } $body = @{ - Name = $Name - Id = ($groups | Where-Object { $_.name -eq "$Name" } | Select-Object -ExpandProperty Id) - Description = ($groups | Where-Object { $_.name -eq "$Name" } | Select-Object -ExpandProperty Description) - Groups = if (-not $processedGroups) { - @() - } - else { - @(, $processedGroups) - } - Computers = if (-not $processedComputers) { - @() - } - else { - @(, $processedComputers) - } + Name = $current.Name + Id = $current.Id + Description = $current.Description + Groups = @($GroupCollection) + Computers = @($ComputerCollection) } | ConvertTo-Json -Depth 3 Write-Verbose $body diff --git a/src/Public/Get-CCMGroupMember.ps1 b/src/Public/Get-CCMGroupMember.ps1 index e15c521..481314d 100644 --- a/src/Public/Get-CCMGroupMember.ps1 +++ b/src/Public/Get-CCMGroupMember.ps1 @@ -41,37 +41,15 @@ function Get-CCMGroupMember { process { $Id = (Get-CCMGroup -Group $Group).Id - $irmParams = @{ - Uri = "$($protocol)://$hostname/api/services/app/Groups/GetGroupForEdit?id=$Id" - Method = "GET" - ContentType = "application/json" - WebSession = $Session - } - - try { - $record = Invoke-RestMethod @irmParams -ErrorAction Stop - } - catch { - throw $_.Exception.Message - } - - $cCollection = [System.Collections.Generic.List[psobject]]::new() - $gCollection = [System.Collections.Generic.List[psobject]]::new() - - $record.result.computers | ForEach-Object { - $cCollection.Add($_) - } - - $record.result.groups | ForEach-Object { - $gCollection.Add($_) - } + $result = Get-CCMGroup -Id $Id [pscustomobject]@{ - Name = $record.result.Name - Description = $record.result.Description - Groups = @($gCollection) - Computers = @($cCollection) - CanDeploy = $record.result.isEligibleForDeployments + Id = $result.Id + Name = $result.Name + Description = $result.Description + Groups = @($result.Groups | Where-Object { $_ }) + Computers = @($result.Computers | Where-Object { $_ }) + CanDeploy = $result.isEligibleForDeployments } } } diff --git a/src/Public/Remove-CCMGroupMember.ps1 b/src/Public/Remove-CCMGroupMember.ps1 index 304befa..750b540 100644 --- a/src/Public/Remove-CCMGroupMember.ps1 +++ b/src/Public/Remove-CCMGroupMember.ps1 @@ -57,15 +57,27 @@ function Remove-CCMGroupMember { process { $currentMembers = Get-CCMGroupMember -Group $Group - $G = Get-CCMGroup -Group $Group - $currentMembers | Add-Member -MemberType NoteProperty -Name Id -Value $G.Id + $computers = Get-CCMComputer + $groups = Get-CCMGroup foreach ($c in $ComputerMember) { - $currentMembers.Computers = @($($currentMembers.Computers | Where-Object { $_.ComputerName -ne $c })) + $cId = $computers | Where-Object { $_.Name -eq $c } | Select-Object -ExpandProperty Id + if (-not $cId) { + Write-Warning "No computer with the name $c was found, cannot remove it from the group" + continue + } + + $currentMembers.Computers = @($currentMembers.Computers | Where-Object { $_.computerId -ne $cId }) } foreach ($g in $GroupMember) { - $currentMembers.Groups = @($($currentMembers.Groups | Where-Object { $_.subGroupName -ne $g })) + $gId = $groups | Where-Object { $_.Name -eq $g } | Select-Object -ExpandProperty Id + if (-not $gId) { + Write-Warning "No group with the name $g was found, cannot remove it from the group" + continue + } + + $currentMembers.Groups = @($currentMembers.Groups | Where-Object { $_.subGroupId -ne $g }) } if (-not $currentMembers.Groups) { diff --git a/src/Public/Set-CCMGroup.ps1 b/src/Public/Set-CCMGroup.ps1 index aa1f256..077a580 100644 --- a/src/Public/Set-CCMGroup.ps1 +++ b/src/Public/Set-CCMGroup.ps1 @@ -57,7 +57,7 @@ function Set-CCMGroup { throw "Not authenticated! Please run Connect-CCMServer first!" } - $existing = Get-CCMGroup -Group $Group + $existing = Get-CCMGroupMember -Group $Group } process { @@ -80,11 +80,11 @@ function Set-CCMGroup { Method = "POST" ContentType = "application/json" Body = @{ - Id = $($existing.id) + Id = $existing.id Name = $Name Description = $Description - Groups = @() - Computers = @() + Groups = $existing.Groups + Computers = $existing.Computers } | ConvertTo-Json WebSession = $Session }