Skip to content

Commit

Permalink
Merge pull request chocolatey#78 from vexx32/59-badrequest
Browse files Browse the repository at this point in the history
(chocolatey#59) Fix BadRequest errors for group commands
  • Loading branch information
gep13 authored Sep 21, 2022
2 parents 47efb4a + 3832125 commit 74349e9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 77 deletions.
14 changes: 2 additions & 12 deletions src/Public/Add-CCMGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @{
Expand Down
52 changes: 24 additions & 28 deletions src/Public/Add-CCMGroupMember.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,56 +62,52 @@ 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)" }) }
}

process {
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
Expand Down
36 changes: 7 additions & 29 deletions src/Public/Get-CCMGroupMember.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
20 changes: 16 additions & 4 deletions src/Public/Remove-CCMGroupMember.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/Public/Set-CCMGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 74349e9

Please sign in to comment.