From 34d912970867ac656eb5ffb38e46a3a72cd71f90 Mon Sep 17 00:00:00 2001 From: Mark Mihevc Date: Mon, 2 Dec 2024 13:30:42 -0700 Subject: [PATCH 01/33] change to windows uninstaller for more robustness --- scripts/windows/UninstallWindowsAgent.ps1 | 160 ++++++++++++++++++---- 1 file changed, 134 insertions(+), 26 deletions(-) diff --git a/scripts/windows/UninstallWindowsAgent.ps1 b/scripts/windows/UninstallWindowsAgent.ps1 index c7cc58fcd..880db65da 100644 --- a/scripts/windows/UninstallWindowsAgent.ps1 +++ b/scripts/windows/UninstallWindowsAgent.ps1 @@ -1,5 +1,5 @@ - # Function to check if running as Administrator - function Test-Administrator { +# Function to check if running as Administrator +function Test-Administrator { $currentUser = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = New-Object Security.Principal.WindowsPrincipal($currentUser) return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) @@ -13,14 +13,71 @@ if (-not (Test-Administrator)) { # Create HKCR Mapping New-PSDrive -Name "HKCR" -PSProvider Registry -Root "HKEY_CLASSES_ROOT" -ErrorAction SilentlyContinue +function Get-UninstallExeCommand($uninstallString) { + $index = $uninstallString.IndexOf("`"", 0) + $index = $uninstallString.IndexOf("`"", $index + 1) + + $cmd = $uninstallString.SubString(1, $index - 1) + $arguments = $uninstallString.SubString($index + 1).Trim() + + return @{ + Cmd = $cmd + Arguments = $arguments + } +} + +function Get-UninstallMsiCommand($productCode) { + $cmd = "msiexec.exe" + $arguments = "/x `"$productCode`" /qn /l*v `"$env:SystemRoot\temp\jcagentforceuninstall.log`"" + + return @{ + Cmd = $cmd + Arguments = $arguments + } +} + +function Find-UninstallCommands($uninstallKey) { + $uninstallCommands = @() + + Get-ChildItem -Path $uninstallKey | ForEach-Object { + try { + $displayName = (Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue).DisplayName + if ($displayName -like "*JumpCloud Agent*") { + $uninstallString = (Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue).UninstallString + if ($uninstallString -like "MsiExec.exe*") { + $command = Get-UninstallMsiCommand $_.PSChildName + $uninstallCommands += $command + } + } + + # if uninstall agent, remote assist or tray apps will not work anymore so, unisntall them also + # jumpcloud tray is uninstalled by the MSI installer so there is not separate installer. + if ($displayName -like "*JumpCloud Remote Assist*" -or $displayName -like "*jumpcloud-agent-app*") { + $uninstallString = (Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue).QuietUninstallString + $command = Get-UninstallExeCommand $uninstallString + $uninstallCommands += $command + } + } catch { + Write-Host "Error accessing $($_.PSPath)" + } + } + + return @{ Commands = $uninstallCommands } +} + # Function to recursively search for DisplayName and ProductName in the registry function Find-JumpCloudGUID { $rootKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products" $uninstallKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + $uninstallWowKey = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" $classesKey = "HKCR:\Installer\Products" - + $agentGUIDs = @() $msiGUIDs = @() + $uninstallCommands = @() + + (Find-UninstallCommands $uninstallKey).Commands | ForEach-Object { $uninstallCommands += $_ } + (Find-UninstallCommands $uninstallWowKey).Commands | ForEach-Object { $uninstallCommands += $_ } # Searching Installer Products for JumpCloud Agent GUIDs Get-ChildItem -Path $rootKey | ForEach-Object { @@ -62,22 +119,42 @@ function Find-JumpCloudGUID { } } - return @{ AgentGUIDs = $agentGUIDs; MSIGUIDs = $msiGUIDs } + return @{ AgentGUIDs = $agentGUIDs; MSIGUIDs = $msiGUIDs; Uninstalls = $uninstallCommands } +} + +function Remove-Folder($folder) { + if (Test-Path $folder) { + Remove-Item -Path $folder -Recurse -Force -ErrorAction SilentlyContinue + Write-Host "Successfully deleted folder: $folder" + } } # Function to remove registry keys, folder, and service function Remove-JumpCloud { $guids = Find-JumpCloudGUID - + # Flag to track if items to remove were found - $foundItemsToRemove = $false - + $foundItemsToRemove = $false + + # if the MSI was used to install, we want to uninstall that route first + foreach ($uninst in $guids.Uninstalls) { + $foundItemsToRemove = $true + Write-Output "Uninstallation of $($uninst.Cmd) $($uninst.Arguments) started." + + # Uninstall the MSI package, PS will fail without quotes around product code + # puposely give uninstall log a different name to differentiate + Start-Process $uninst.Cmd -ArgumentList $uninst.Arguments -Wait + + Write-Output "Uninstallation of $($uninst.Cmd) $($uninst.Arguments) completed." + } + + # remove installation registry keys if left behind. foreach ($guid in $guids.AgentGUIDs) { # Removing registry keys $installerKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\$guid" $classesKey = "HKCR:\Installer\Products\$guid" $jumpcloudSoftwareKey = "HKLM:\Software\JumpCloud" - + foreach ($msiGuid in $guids.MSIGUIDs) { $uninstallKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$msiGuid" @@ -91,25 +168,56 @@ function Remove-JumpCloud { } } } + } + + # Stopping and removing the JumpCloud Agent service if exists + $serviceName = "jumpcloud-agent" + if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) { + $foundItemsToRemove = $true + Stop-Service -Name $serviceName -Force -ErrorAction SilentlyContinue + Start-Sleep -Seconds 6 + sc.exe delete $serviceName + Start-Sleep -Seconds 6 + Write-Host "Service $serviceName successfully removed." + } + + # stop and remove jumpcloud try if it exists + Get-Process -Name jumpcloudtray -ErrorAction SilentlyContinue | ForEach-Object { + Write-Host "stopping tray process $($_.Id)" + Stop-Process -id $_.Id -ErrorAction SilentlyContinue + } + + # Removing the JumpCloudfolder + $folder = "$($env:ProgramFiles)\JumpCloud" + Remove-Folder $folder - # Stopping and removing the JumpCloud Agent service - $serviceName = "jumpcloud-agent" - if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) { - $foundItemsToRemove = $true - Stop-Service -Name $serviceName -Force -ErrorAction SilentlyContinue - Start-Sleep -Seconds 6 - sc.exe delete $serviceName - Start-Sleep -Seconds 6 - Write-Host "Service $serviceName successfully removed." - } - - # Removing the JumpCloudfolder - $jumpcloudFolder = "C:\Program Files\JumpCloud" - if (Test-Path $jumpcloudFolder) { - $foundItemsToRemove = $true - Remove-Item -Path $jumpcloudFolder -Recurse -Force -ErrorAction SilentlyContinue - Write-Host "Successfully deleted folder: $jumpcloudFolder" - } + # remove tray folder + $folder = "$($env:ProgramFiles)\JumpCloudTray" + Remove-Folder $folder + + $folder = "$env:APPDATA\JumpCloud" + Remove-Folder $folder + + # remove older credential provider files if they exists in sys dir + if (Test-Path "$env:SystemRoot\System32\JumpCloud*.dll") { + $foundItemsToRemove = $true + Remove-Item -Path "$env:SystemRoot\System32\JumpCloud*.dll" -Recurse -Force -ErrorAction SilentlyContinue + Write-Host "Successfully deleted files from system32" + } + + # remove windows runs on startups + $runKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" + + # Get the registry key + (Get-Item -Path $runKeyPath).Property | ForEach-Object { + try { + if ($_ -like "*jumpcloud-*") { + Write-Host "Delete $runKeyPath $_" + Remove-ItemProperty -Path $runKeyPath -Name $_ + } + } catch { + Write-Host "Error accessing $($_)" + } } # Check if nothing was found to remove From bb9bcc35b041c76820d886a3fed01eb7f9f7b7ff Mon Sep 17 00:00:00 2001 From: Mark Mihevc Date: Wed, 4 Dec 2024 11:11:43 -0700 Subject: [PATCH 02/33] suppressing error when can't find app to uninstall --- scripts/windows/UninstallWindowsAgent.ps1 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/scripts/windows/UninstallWindowsAgent.ps1 b/scripts/windows/UninstallWindowsAgent.ps1 index 880db65da..5b8aca6f4 100644 --- a/scripts/windows/UninstallWindowsAgent.ps1 +++ b/scripts/windows/UninstallWindowsAgent.ps1 @@ -23,6 +23,7 @@ function Get-UninstallExeCommand($uninstallString) { return @{ Cmd = $cmd Arguments = $arguments + Key = "" } } @@ -33,6 +34,7 @@ function Get-UninstallMsiCommand($productCode) { return @{ Cmd = $cmd Arguments = $arguments + Key = "" } } @@ -46,6 +48,7 @@ function Find-UninstallCommands($uninstallKey) { $uninstallString = (Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue).UninstallString if ($uninstallString -like "MsiExec.exe*") { $command = Get-UninstallMsiCommand $_.PSChildName + $command.Key = "$uninstallKey/$($_.PSChildName)" $uninstallCommands += $command } } @@ -55,6 +58,7 @@ function Find-UninstallCommands($uninstallKey) { if ($displayName -like "*JumpCloud Remote Assist*" -or $displayName -like "*jumpcloud-agent-app*") { $uninstallString = (Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue).QuietUninstallString $command = Get-UninstallExeCommand $uninstallString + $command.Key = "$uninstallKey/$($_.PSChildName)" $uninstallCommands += $command } } catch { @@ -138,6 +142,11 @@ function Remove-JumpCloud { # if the MSI was used to install, we want to uninstall that route first foreach ($uninst in $guids.Uninstalls) { + if (-not(Test-Path -Path $uninst.Key)) { + Write-Host "$($uninst.Key) already removed" + continue; + } + $foundItemsToRemove = $true Write-Output "Uninstallation of $($uninst.Cmd) $($uninst.Arguments) started." From f24a76b4d77670ebc3d635594cf025b3e2362994 Mon Sep 17 00:00:00 2001 From: Kyle Kirkland Date: Mon, 9 Dec 2024 12:33:54 -0700 Subject: [PATCH 03/33] Update Mac - Install Chrome DMG.md Update to universal dmg to ensure current stable version is installed. --- .../Application Installs/Mac - Install Chrome DMG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PowerShell/JumpCloud Commands Gallery/Mac Commands/Application Installs/Mac - Install Chrome DMG.md b/PowerShell/JumpCloud Commands Gallery/Mac Commands/Application Installs/Mac - Install Chrome DMG.md index 79e760629..5c02ad246 100644 --- a/PowerShell/JumpCloud Commands Gallery/Mac Commands/Application Installs/Mac - Install Chrome DMG.md +++ b/PowerShell/JumpCloud Commands Gallery/Mac Commands/Application Installs/Mac - Install Chrome DMG.md @@ -10,7 +10,7 @@ mac ``` # DMG Download URL -DownloadUrl="https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg" +DownloadUrl="https://dl.google.com/chrome/mac/universal/stable/GGRO/googlechrome.dmg" ### Modify below this line at your own risk! @@ -173,7 +173,7 @@ echo "Deleted /tmp/$TempFolder" #### Description -Installs Google Chrome from the DMG file available for download from the link: `https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg`. +Installs Google Chrome from the DMG file available for download from the link: `https://dl.google.com/chrome/mac/universal/stable/GGRO/googlechrome.dmg`. This command creates a temporary folder in the /tmp directory and to downloads the DMG file to this folder. From 685bf9de78002e557acff11de3b6717f7ffd907c Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Tue, 10 Dec 2024 15:06:29 -0800 Subject: [PATCH 04/33] cascade manager --- .../Public/Users/Remove-JCUser.ps1 | 60 +++++++++++++++++-- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index a522e1a9b..c517298c3 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -54,9 +54,10 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline $deletedArray = @() if ($PSCmdlet.ParameterSetName -eq 'Username' ) { - $UserHash = Get-DynamicHash -Object User -returnProperties username + $UserHash = Get-DynamicHash -Object User -returnProperties username, manager $UserCount = ($UserHash).Count Write-Debug "Populated UserHash with $UserCount users" + Write-Debug "UserHash is $($UserHash.Values.username)" } } @@ -71,14 +72,61 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline if ($PSCmdlet.ParameterSetName -eq 'UserID' ) { $Username = $UserID + Write-Debug "UserID is $UserID" } + # Check if the user is a manager + if ($UserHash.Values.manager -contains ($UserID)) { + $isManager = $true + # Count the number of users the manager is managing + # $managerCount = ($UserHash.Values.manager -eq $UserID).Count + # Save each user the manager is managing in a list + $managedUsers = $UserHash.GetEnumerator().Where({ $_.Value.manager -eq $UserID }).Name + Write-Debug "Manager is managing $managedUsers users" + $hasManagerId = Get-JcSdkUser -UserID $UserID | Select-Object -ExpandProperty manager + Write-Debug "Manager is managed by $hasManagerId" + + } else { + $isManager = $false + Write-Debug "User is not a manager" + } + + # Check if the user is managed by another manager + + + # TODO: If force or does not have a manager, default to cascade_manager=Null -- Done + # TODO: If not force, prompt for cascade_manager if the user is a manager - Done + # TODO: If manager is managed by another manager, cascade_manager to users managed by the manager if (!$force) { try { - $URI = "$JCUrlBasePath/api/systemusers/$UserID" - Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) - $Status = 'Deleted' + if ($isManager) { + # Prompt for cascade_managerk, user enters the ID of the new manager + $cascade_manager = Read-Host "User is a manager. Do you want to reassign their managed users to another manager? (Y/N)" + if ($cascade_manager -eq 'Y') { + $newManagerId = Read-Host "Enter the UserID of the new manager" + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=$($newManagerId)" + Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire + $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) + Write-Debug $delete + $Status = 'Deleted' + } else { + $URI = "$JCUrlBasePath/api/systemusers/$UserID?cascade_manager=$null" + Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire + $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) + $Status = 'Deleted' + } + + $URI = "$JCUrlBasePath/api/systemusers/$UserID?cascade_manager=$null" + Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire + $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) + $Status = 'Deleted' + } else { + $URI = "$JCUrlBasePath/api/systemusers/$UserID?cascade_manager=$null" + Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire + $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) + $Status = 'Deleted' + } + } catch { $Status = $_.ErrorDetails } @@ -94,7 +142,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline if ($force) { try { - $URI = "$JCUrlBasePath/api/systemusers/$UserID" + $URI = "$JCUrlBasePath/api/systemusers/$UserID?cascade_manager=$null" $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) $Status = 'Deleted' } catch { From 85154ed0635e3cb49f31c0a81dcfd6f892da2f70 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Wed, 11 Dec 2024 11:42:38 -0800 Subject: [PATCH 05/33] cascade manager param added --- .../Private/Users/Delete-JcUser.ps1 | 37 +++++++ .../Public/Users/Remove-JCUser.ps1 | 101 ++++++++++-------- 2 files changed, 92 insertions(+), 46 deletions(-) create mode 100644 PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 diff --git a/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 b/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 new file mode 100644 index 000000000..e65b375ba --- /dev/null +++ b/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 @@ -0,0 +1,37 @@ +function Delete-JCUser { + # UserId and cascade_manager are required + param ( + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud User ID.')][ValidateNotNullOrEmpty()][System.String]$Id + , [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud cascade manager ID.')][System.String]$managerId, + # headers are required + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud API headers.')][ValidateNotNullOrEmpty()][System.Collections.Hashtable]$Headers + ) + + process { + try { + if ($managerId) { + $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=$($managerId)" + Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire + $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $Headers -UserAgent:(Get-JCUserAgent) + Write-Debug $delete + $Status = 'Deleted' + } else { + $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=null" + Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire + $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $Headers -UserAgent:(Get-JCUserAgent) + Write-Debug $delete + $Status = 'Deleted' + } + + } catch { + $Status = $_.ErrorDetails + } + $FormattedResults = [PSCustomObject]@{ + 'User' = $Username + 'Results' = $Status + } + } + end { + return $FormattedResults + } +} \ No newline at end of file diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index c517298c3..637174559 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -29,7 +29,10 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline [Parameter(HelpMessage = 'A SwitchParameter which suppresses the warning message when removing a JumpCloud User.')] [Switch] - $force + $force, + [Parameter(HelpMessage = 'A SwitchParameter for Cascading the manager of the user to the users managed by the user. NULL, AUTOMATIC (bubble up), ID (prompt for manager ID)')] + [ValidateSet('NULL', 'AUTOMATIC', 'ID')] + [string]$CascadeManager ) begin { @@ -37,14 +40,11 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline if ([System.String]::IsNullOrEmpty($JCAPIKEY)) { Connect-JConline } - Write-Debug 'Populating API headers' $hdrs = @{ - 'Content-Type' = 'application/json' 'Accept' = 'application/json' 'X-API-KEY' = $JCAPIKEY - } if ($JCOrgID) { @@ -57,7 +57,21 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline $UserHash = Get-DynamicHash -Object User -returnProperties username, manager $UserCount = ($UserHash).Count Write-Debug "Populated UserHash with $UserCount users" - Write-Debug "UserHash is $($UserHash.Values.username)" + } + + # Check if CascadeManager is set to ID + if ($CascadeManager -eq 'ID') { + # Prompt for cascade_manager, user enters the ID of the new manager + $newManagerId = Read-Host "Enter the UserID of the new manager" + # Validate if the Id is a JC User + $validateUser = Get-JcSdkUser -Id $newManagerId + if ($validateUser) { + Write-Debug "User $newManagerId is a valid JumpCloud User" + } else { + Write-Host "User does not exist. Please enter a valid UserID." + # Exit the script + Exit + } } } @@ -83,9 +97,8 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline # Save each user the manager is managing in a list $managedUsers = $UserHash.GetEnumerator().Where({ $_.Value.manager -eq $UserID }).Name Write-Debug "Manager is managing $managedUsers users" - $hasManagerId = Get-JcSdkUser -UserID $UserID | Select-Object -ExpandProperty manager + $hasManagerId = Get-JcSdkUser -Id $UserID | Select-Object -ExpandProperty manager Write-Debug "Manager is managed by $hasManagerId" - } else { $isManager = $false Write-Debug "User is not a manager" @@ -95,56 +108,55 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline # TODO: If force or does not have a manager, default to cascade_manager=Null -- Done - # TODO: If not force, prompt for cascade_manager if the user is a manager - Done - # TODO: If manager is managed by another manager, cascade_manager to users managed by the manager + # TODO: If not force, prompt for cascade_manager if the user is a manager -- Done + # TODO: If manager is managed by another manager, cascade_manager to users managed by the manager -- Done if (!$force) { - try { - if ($isManager) { - # Prompt for cascade_managerk, user enters the ID of the new manager - $cascade_manager = Read-Host "User is a manager. Do you want to reassign their managed users to another manager? (Y/N)" - if ($cascade_manager -eq 'Y') { - $newManagerId = Read-Host "Enter the UserID of the new manager" - $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=$($newManagerId)" - Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) - Write-Debug $delete - $Status = 'Deleted' + if ($CascadeManager) { + Switch ($CascadeManager) { + 'NULL' { + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs + } + 'AUTOMATIC' { + $Status = Delete-JCUser -Id $UserID -managerId 'AUTOMATIC' -Headers $hdrs + } + 'ID' { + $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + } + } + } elseif ($isManager -and !$CascadeManager) { + # Prompt for cascade_managerk, user enters the ID of the new manager + $cascade_manager = Read-Host "User is a manager. Do you want to reassign their managed users to another manager? (Y/N)" + if ($cascade_manager -eq 'Y') { + if ($hasManagerId) { + $prompt = "User is managed by another manager. Do you want to reassign their managed users to the manager who is managing this user? (Y/N)" + $cascade_manager = Read-Host $prompt + if ($cascade_manager -eq 'Y') { + $newManagerId = $hasManagerId + $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + } else { + $newManagerId = Read-Host "Enter the UserID of the new manager" + $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + } } else { - $URI = "$JCUrlBasePath/api/systemusers/$UserID?cascade_manager=$null" - Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) - $Status = 'Deleted' + $newManagerId = Read-Host "Enter the UserID of the new manager" + $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs } - - $URI = "$JCUrlBasePath/api/systemusers/$UserID?cascade_manager=$null" - Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) - $Status = 'Deleted' - } else { - $URI = "$JCUrlBasePath/api/systemusers/$UserID?cascade_manager=$null" - Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) - $Status = 'Deleted' + } elseif ($cascade_manager -eq 'N') { + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs } - - } catch { - $Status = $_.ErrorDetails + } else { + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs } - $FormattedResults = [PSCustomObject]@{ 'User' = $Username 'Results' = $Status } - $deletedArray += $FormattedResults - } if ($force) { try { - $URI = "$JCUrlBasePath/api/systemusers/$UserID?cascade_manager=$null" - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) - $Status = 'Deleted' + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs } catch { $Status = $_.ErrorDetails } @@ -157,10 +169,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline $deletedArray += $FormattedResults } - - } - end { return $deletedArray From da7dd3d1575f2d929dc6c76ccc4f5a7174efdafa Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Thu, 12 Dec 2024 12:41:45 -0800 Subject: [PATCH 06/33] additional checks --- .../Private/Users/Delete-JcUser.ps1 | 40 ++++++-- .../Public/Users/Remove-JCUser.ps1 | 95 ++++++++++--------- 2 files changed, 80 insertions(+), 55 deletions(-) diff --git a/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 b/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 index e65b375ba..fe04e0ea4 100644 --- a/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 +++ b/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 @@ -6,23 +6,43 @@ function Delete-JCUser { # headers are required [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud API headers.')][ValidateNotNullOrEmpty()][System.Collections.Hashtable]$Headers ) - process { try { if ($managerId) { $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=$($managerId)" - Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $Headers -UserAgent:(Get-JCUserAgent) - Write-Debug $delete - $Status = 'Deleted' + $Username = Get-JcSdkUser -Id $Id | Select-Object -ExpandProperty username + $ManagerUsername = Get-JcSdkUser -Id $managerId | Select-Object -ExpandProperty username + Write-Host "Deleting user: $($Username) and cascading managed users to manager: $($ManagerUsername)" -ForegroundColor Yellow + $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" + while ($prompt -ne 'Y' -and $prompt -ne 'N') { + $prompt = Read-Host "Please enter Y or N" + } + if ($prompt -eq 'Y') { + $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $Headers -UserAgent:(Get-JCUserAgent) + Write-Debug $delete + $Status = 'Deleted' + } elseif ($prompt -eq 'N') { + Write-Debug "User not deleted" + $Status = 'Not Deleted' + } } else { $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=null" - Write-Warning "Are you sure you wish to delete user: $Username ?" -WarningAction Inquire - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $Headers -UserAgent:(Get-JCUserAgent) - Write-Debug $delete - $Status = 'Deleted' + $Username = Get-JcSdkUser -Id $Id | Select-Object -ExpandProperty username + Write-Host "Deleting user: $Username" -ForegroundColor Yellow + $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" + # Do a loop until the user enters Y or N + while ($prompt -ne 'Y' -and $prompt -ne 'N') { + $prompt = Read-Host "Please enter Y or N" + } + if ($prompt -eq 'Y') { + $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $Headers -UserAgent:(Get-JCUserAgent) + Write-Debug $delete + $Status = 'Deleted' + } elseif ($prompt -eq 'N') { + Write-Debug "User not deleted" + $Status = 'Not Deleted' + } } - } catch { $Status = $_.ErrorDetails } diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index 637174559..75d703e18 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -26,15 +26,30 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline HelpMessage = 'Use the -ByID parameter when the UserID is passed over the pipeline to the Remove-JCUser function. The -ByID SwitchParameter will set the ParameterSet to ''ByID'' which will increase the function speed and performance.')] [Switch] $ByID, - + # Do not use $CascadeManager if $force is used [Parameter(HelpMessage = 'A SwitchParameter which suppresses the warning message when removing a JumpCloud User.')] [Switch] $force, [Parameter(HelpMessage = 'A SwitchParameter for Cascading the manager of the user to the users managed by the user. NULL, AUTOMATIC (bubble up), ID (prompt for manager ID)')] - [ValidateSet('NULL', 'AUTOMATIC', 'ID')] + [ValidateSet('NULL', 'Auto', 'ID')] [string]$CascadeManager ) - + DynamicParam { + if ($CascadeManager -eq 'ID' -and !$force) { + # Prompt for cascade_manager, user enters the ID of the new manager + $newManagerId = Read-Host "Enter the UserID of the new manager" + # Validate if the Id is a JC User + $validateUser = Get-JcSdkUser -Id $newManagerId + if ($validateUser) { + Write-Debug "User $newManagerId is a valid JumpCloud User" + return $newManagerId + } else { + Write-Error "User does not exist. Please enter a valid UserID." + # Exit the script + Exit + } + } + } begin { Write-Debug 'Verifying JCAPI Key' if ([System.String]::IsNullOrEmpty($JCAPIKEY)) { @@ -52,43 +67,37 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } $deletedArray = @() - - if ($PSCmdlet.ParameterSetName -eq 'Username' ) { - $UserHash = Get-DynamicHash -Object User -returnProperties username, manager - $UserCount = ($UserHash).Count - Write-Debug "Populated UserHash with $UserCount users" + # If $cascadeManager and $force are used, throw an error + if ($CascadeManager -and $force) { + Throw "Cannot use -CascadeManager and -Force together. Please use one or the other." } - # Check if CascadeManager is set to ID - if ($CascadeManager -eq 'ID') { - # Prompt for cascade_manager, user enters the ID of the new manager - $newManagerId = Read-Host "Enter the UserID of the new manager" - # Validate if the Id is a JC User - $validateUser = Get-JcSdkUser -Id $newManagerId - if ($validateUser) { - Write-Debug "User $newManagerId is a valid JumpCloud User" - } else { - Write-Host "User does not exist. Please enter a valid UserID." - # Exit the script - Exit - } - } + $UserHash = Get-DynamicHash -Object User -returnProperties 'username', 'manager' } process { if ($PSCmdlet.ParameterSetName -eq 'Username' ) { if ($UserHash.Values.username -contains ($Username)) { $UserID = $UserHash.GetEnumerator().Where({ $_.Value.username -contains ($Username) }).Name + Write-Debug "UserID: $UserID" } else { Throw "Username does not exist. Run 'Get-JCUser | Select-Object username' to see a list of all your JumpCloud users." } - } + } elseif ($PSCmdlet.ParameterSetName -eq 'UserID') { + # Validate if the Id is a JC User from the $UserHash + if ($UserHash.ContainsKey($UserID)) { + Write-Debug "UserID is a valid JumpCloud User" + $UserId = $UserHash.GetEnumerator().Where({ $_.Name -contains ($UserID) }).Name + $Username = $UserHash.GetEnumerator().Where({ $_.Name -contains ($UserID) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username + Write-Debug "UserID: $UserID" + Write-Debug "Username: $Username" - if ($PSCmdlet.ParameterSetName -eq 'UserID' ) { - $Username = $UserID - Write-Debug "UserID is $UserID" + } else { + Write-Error "User does not exist. Please enter a valid UserID." + # Exit the script + Exit + } } - # Check if the user is a manager if ($UserHash.Values.manager -contains ($UserID)) { $isManager = $true @@ -116,8 +125,12 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline 'NULL' { $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs } - 'AUTOMATIC' { - $Status = Delete-JCUser -Id $UserID -managerId 'AUTOMATIC' -Headers $hdrs + 'Auto' { + if ($hasManagerId) { + $Status = Delete-JCUser -Id $UserID -managerId $hasManagerId -Headers $hdrs + } else { + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs + } } 'ID' { $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs @@ -125,10 +138,14 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } } elseif ($isManager -and !$CascadeManager) { # Prompt for cascade_managerk, user enters the ID of the new manager - $cascade_manager = Read-Host "User is a manager. Do you want to reassign their managed users to another manager? (Y/N)" + $cascade_manager = Read-Host "User $($Username) is a manager. Do you want to reassign their managed users to another manager? (Y / N)" + while ($cascade_manager -ne 'Y' -and $cascade_manager -ne 'N') { + $cascade_manager = Read-Host "Please enter Y (Yes) or N (No)" + } if ($cascade_manager -eq 'Y') { if ($hasManagerId) { - $prompt = "User is managed by another manager. Do you want to reassign their managed users to the manager who is managing this user? (Y/N)" + $managerUsername = $UserHash.GetEnumerator().Where({ $_.Name -contains ($hasManagerId) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username + $prompt = "User $($Username) is managed by manager: $($managerUsername). Do you want to reassign their managed users to the manager who is managing this user? (Y/N)" $cascade_manager = Read-Host $prompt if ($cascade_manager -eq 'Y') { $newManagerId = $hasManagerId @@ -147,28 +164,16 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } else { $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs } - $FormattedResults = [PSCustomObject]@{ - 'User' = $Username - 'Results' = $Status - } - $deletedArray += $FormattedResults } - if ($force) { try { $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs } catch { $Status = $_.ErrorDetails } - - $FormattedResults = [PSCustomObject]@{ - 'User' = $Username - 'Results' = $Status - } - - $deletedArray += $FormattedResults - } + $FormattedResults = $Status + $deletedArray += $FormattedResults } end { From 3210ec1a21f5990850c5dfff3e748ad19ce4e4fb Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Thu, 12 Dec 2024 13:21:00 -0800 Subject: [PATCH 07/33] docs --- PowerShell/JumpCloud Module/Docs/JumpCloud.md | 2 +- .../JumpCloud Module/Docs/Remove-JCUser.md | 29 +- PowerShell/JumpCloud Module/JumpCloud.psd1 | 274 +++++++++--------- .../Public/Users/Remove-JCUser.ps1 | 2 +- PowerShell/ModuleChangelog.md | 17 ++ 5 files changed, 184 insertions(+), 140 deletions(-) diff --git a/PowerShell/JumpCloud Module/Docs/JumpCloud.md b/PowerShell/JumpCloud Module/Docs/JumpCloud.md index ee0969774..97f468bd1 100644 --- a/PowerShell/JumpCloud Module/Docs/JumpCloud.md +++ b/PowerShell/JumpCloud Module/Docs/JumpCloud.md @@ -2,7 +2,7 @@ Module Name: JumpCloud Module Guid: 31c023d1-a901-48c4-90a3-082f91b31646 Download Help Link: https://github.com/TheJumpCloud/support/wiki -Help Version: 2.15.0 +Help Version: 2.16.0 Locale: en-Us --- diff --git a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md index 2e52b217b..b3dbb4d92 100644 --- a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md +++ b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md @@ -22,6 +22,11 @@ Remove-JCUser [-Username] [-force] [] Remove-JCUser -UserID [-ByID] [-force] [] ``` +### CascadeManager +``` +Remove-JCUser [-Username] [-ByID] -CascadeManager [] +``` + ## DESCRIPTION The Remove-JCUser function will remove a JumpCloud user from the JumpCloud organization. This will remove the deleted users access to any JumpCloud bound resources. @@ -40,6 +45,28 @@ PS C:\> Remove-JCUser cclemons -Force ``` Removes the JumpCloud User with Username 'cclemons' using the -Force Parameter. A warning message will not be presented to confirm this operation. +If the user is a manager, this parameter will clear the manager field for the managed users by this manager. + +### Example 3 +```powershell +PS C:\> Remove-JCUser cclemons -CascadeManager null +``` + +Removes the Jumpcloud user with Username 'cclemons'. If the user is a manager, the manager field for the managed users by this manager will be set to null. + +### Example 4 +```powershell +PS C:\> Remove-JCUser cclemons -CascadeManager auto +``` + +Removes the JumpCloud user with the username 'cclemons'. If this user is a manager (Manager1) and is also managed by another manager (Manager2), the users managed by Manager1 will be reassigned to Manager2 upon Manager1's removal. If Manager1 is not managed by anyone, the manager field for the users managed by Manager1 will be set to null. + +### Example 5 +```powershell +PS C:\> Remove-JCUser cclemons -CascadeManager id +``` + +Removes the JumpCloud user with the username 'cclemons'. If this user is a manager, their managed users will be reassigned to the manager specified by the provided ID. ## PARAMETERS @@ -124,4 +151,4 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ### System.Object ## NOTES -## RELATED LINKS +## RELATED LINKS diff --git a/PowerShell/JumpCloud Module/JumpCloud.psd1 b/PowerShell/JumpCloud Module/JumpCloud.psd1 index cf5ec8943..bc7ed6e8f 100644 --- a/PowerShell/JumpCloud Module/JumpCloud.psd1 +++ b/PowerShell/JumpCloud Module/JumpCloud.psd1 @@ -8,161 +8,161 @@ @{ -# Script module or binary module file associated with this manifest. -RootModule = 'JumpCloud.psm1' - -# Version number of this module. -ModuleVersion = '2.15.0' - -# Supported PSEditions -# CompatiblePSEditions = @() - -# ID used to uniquely identify this module -GUID = '31c023d1-a901-48c4-90a3-082f91b31646' - -# Author of this module -Author = 'JumpCloud Solutions Architect Team' - -# Company or vendor of this module -CompanyName = 'JumpCloud' - -# Copyright statement for this module -Copyright = '(c) JumpCloud. All rights reserved.' - -# Description of the functionality provided by this module -Description = 'PowerShell functions to manage a JumpCloud Directory-as-a-Service' - -# Minimum version of the PowerShell engine required by this module -PowerShellVersion = '4.0' - -# Name of the PowerShell host required by this module -# PowerShellHostName = '' - -# Minimum version of the PowerShell host required by this module -# PowerShellHostVersion = '' - -# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. -# DotNetFrameworkVersion = '' - -# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. -# ClrVersion = '' + # Script module or binary module file associated with this manifest. + RootModule = 'JumpCloud.psm1' + + # Version number of this module. + ModuleVersion = '2.16.0' + + # Supported PSEditions + # CompatiblePSEditions = @() + + # ID used to uniquely identify this module + GUID = '31c023d1-a901-48c4-90a3-082f91b31646' + + # Author of this module + Author = 'JumpCloud Solutions Architect Team' + + # Company or vendor of this module + CompanyName = 'JumpCloud' + + # Copyright statement for this module + Copyright = '(c) JumpCloud. All rights reserved.' + + # Description of the functionality provided by this module + Description = 'PowerShell functions to manage a JumpCloud Directory-as-a-Service' + + # Minimum version of the PowerShell engine required by this module + PowerShellVersion = '4.0' + + # Name of the PowerShell host required by this module + # PowerShellHostName = '' + + # Minimum version of the PowerShell host required by this module + # PowerShellHostVersion = '' + + # Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. + # DotNetFrameworkVersion = '' + + # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. + # ClrVersion = '' -# Processor architecture (None, X86, Amd64) required by this module -# ProcessorArchitecture = '' + # Processor architecture (None, X86, Amd64) required by this module + # ProcessorArchitecture = '' -# Modules that must be imported into the global environment prior to importing this module -RequiredModules = @('JumpCloud.SDK.DirectoryInsights', - 'JumpCloud.SDK.V1', - 'JumpCloud.SDK.V2') + # Modules that must be imported into the global environment prior to importing this module + RequiredModules = @('JumpCloud.SDK.DirectoryInsights', + 'JumpCloud.SDK.V1', + 'JumpCloud.SDK.V2') -# Assemblies that must be loaded prior to importing this module -# RequiredAssemblies = @() + # Assemblies that must be loaded prior to importing this module + # RequiredAssemblies = @() -# Script files (.ps1) that are run in the caller's environment prior to importing this module. -# ScriptsToProcess = @() + # Script files (.ps1) that are run in the caller's environment prior to importing this module. + # ScriptsToProcess = @() -# Type files (.ps1xml) to be loaded when importing this module -# TypesToProcess = @() + # Type files (.ps1xml) to be loaded when importing this module + # TypesToProcess = @() -# Format files (.ps1xml) to be loaded when importing this module -# FormatsToProcess = @() + # Format files (.ps1xml) to be loaded when importing this module + # FormatsToProcess = @() -# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess -# NestedModules = @() + # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess + # NestedModules = @() -# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. -FunctionsToExport = 'Add-JCAssociation', 'Add-JCCommandTarget', 'Add-JCGsuiteMember', - 'Add-JCOffice365Member', 'Add-JCRadiusReplyAttribute', - 'Add-JCSystemGroupMember', 'Add-JCSystemUser', - 'Add-JCUserGroupMember', 'Backup-JCOrganization', 'Connect-JCOnline', - 'Copy-JCAssociation', 'Get-JCAdmin', 'Get-JCAssociation', - 'Get-JCBackup', 'Get-JCCloudDirectory', 'Get-JCCommand', - 'Get-JCCommandResult', 'Get-JCCommandTarget', - 'Get-JCConfiguredTemplatePolicy', 'Get-JCEvent', 'Get-JCEventCount', - 'Get-JCGroup', 'Get-JCOrganization', 'Get-JCPolicy', - 'Get-JCPolicyGroup', 'Get-JCPolicyGroupMember', - 'Get-JCPolicyGroupTemplate', 'Get-JCPolicyGroupTemplateMember', - 'Get-JCPolicyResult', 'Get-JCPolicyTargetGroup', - 'Get-JCPolicyTargetSystem', 'Get-JCRadiusReplyAttribute', - 'Get-JCRadiusServer', 'Get-JCScheduledUserstate', 'Get-JCSystem', - 'Get-JCSystemApp', 'Get-JCSystemGroupMember', 'Get-JCSystemInsights', - 'Get-JCSystemKB', 'Get-JCSystemUser', 'Get-JCUser', - 'Get-JCUserGroupMember', 'Import-JCCommand', 'Import-JCMSPFromCSV', - 'Import-JCUsersFromCSV', 'Invoke-JCCommand', 'Invoke-JCDeployment', - 'New-JCCommand', 'New-JCDeploymentTemplate', 'New-JCImportTemplate', - 'New-JCMSPImportTemplate', 'New-JCPolicy', 'New-JCPolicyGroup', - 'New-JCRadiusServer', 'New-JCSystemGroup', 'New-JCUser', - 'New-JCUserGroup', 'Remove-JCAssociation', 'Remove-JCCommand', - 'Remove-JCCommandResult', 'Remove-JCCommandTarget', - 'Remove-JCGsuiteMember', 'Remove-JCOffice365Member', - 'Remove-JCPolicy', 'Remove-JCPolicyGroup', - 'Remove-JCPolicyGroupTemplate', 'Remove-JCRadiusReplyAttribute', - 'Remove-JCRadiusServer', 'Remove-JCSystem', 'Remove-JCSystemGroup', - 'Remove-JCSystemGroupMember', 'Remove-JCSystemUser', 'Remove-JCUser', - 'Remove-JCUserGroup', 'Remove-JCUserGroupMember', - 'Send-JCPasswordReset', 'Set-JCCloudDirectory', 'Set-JCCommand', - 'Set-JCOrganization', 'Set-JCPolicy', 'Set-JCPolicyGroup', - 'Set-JCRadiusReplyAttribute', 'Set-JCRadiusServer', - 'Set-JCSettingsFile', 'Set-JCSystem', 'Set-JCSystemUser', 'Set-JCUser', - 'Set-JCUserGroupLDAP', 'Update-JCModule', 'Update-JCMSPFromCSV', - 'Update-JCUsersFromCSV' + # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. + FunctionsToExport = 'Add-JCAssociation', 'Add-JCCommandTarget', 'Add-JCGsuiteMember', + 'Add-JCOffice365Member', 'Add-JCRadiusReplyAttribute', + 'Add-JCSystemGroupMember', 'Add-JCSystemUser', + 'Add-JCUserGroupMember', 'Backup-JCOrganization', 'Connect-JCOnline', + 'Copy-JCAssociation', 'Get-JCAdmin', 'Get-JCAssociation', + 'Get-JCBackup', 'Get-JCCloudDirectory', 'Get-JCCommand', + 'Get-JCCommandResult', 'Get-JCCommandTarget', + 'Get-JCConfiguredTemplatePolicy', 'Get-JCEvent', 'Get-JCEventCount', + 'Get-JCGroup', 'Get-JCOrganization', 'Get-JCPolicy', + 'Get-JCPolicyGroup', 'Get-JCPolicyGroupMember', + 'Get-JCPolicyGroupTemplate', 'Get-JCPolicyGroupTemplateMember', + 'Get-JCPolicyResult', 'Get-JCPolicyTargetGroup', + 'Get-JCPolicyTargetSystem', 'Get-JCRadiusReplyAttribute', + 'Get-JCRadiusServer', 'Get-JCScheduledUserstate', 'Get-JCSystem', + 'Get-JCSystemApp', 'Get-JCSystemGroupMember', 'Get-JCSystemInsights', + 'Get-JCSystemKB', 'Get-JCSystemUser', 'Get-JCUser', + 'Get-JCUserGroupMember', 'Import-JCCommand', 'Import-JCMSPFromCSV', + 'Import-JCUsersFromCSV', 'Invoke-JCCommand', 'Invoke-JCDeployment', + 'New-JCCommand', 'New-JCDeploymentTemplate', 'New-JCImportTemplate', + 'New-JCMSPImportTemplate', 'New-JCPolicy', 'New-JCPolicyGroup', + 'New-JCRadiusServer', 'New-JCSystemGroup', 'New-JCUser', + 'New-JCUserGroup', 'Remove-JCAssociation', 'Remove-JCCommand', + 'Remove-JCCommandResult', 'Remove-JCCommandTarget', + 'Remove-JCGsuiteMember', 'Remove-JCOffice365Member', + 'Remove-JCPolicy', 'Remove-JCPolicyGroup', + 'Remove-JCPolicyGroupTemplate', 'Remove-JCRadiusReplyAttribute', + 'Remove-JCRadiusServer', 'Remove-JCSystem', 'Remove-JCSystemGroup', + 'Remove-JCSystemGroupMember', 'Remove-JCSystemUser', 'Remove-JCUser', + 'Remove-JCUserGroup', 'Remove-JCUserGroupMember', + 'Send-JCPasswordReset', 'Set-JCCloudDirectory', 'Set-JCCommand', + 'Set-JCOrganization', 'Set-JCPolicy', 'Set-JCPolicyGroup', + 'Set-JCRadiusReplyAttribute', 'Set-JCRadiusServer', + 'Set-JCSettingsFile', 'Set-JCSystem', 'Set-JCSystemUser', 'Set-JCUser', + 'Set-JCUserGroupLDAP', 'Update-JCModule', 'Update-JCMSPFromCSV', + 'Update-JCUsersFromCSV' -# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. -CmdletsToExport = @() + # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. + CmdletsToExport = @() -# Variables to export from this module -VariablesToExport = '*' + # Variables to export from this module + VariablesToExport = '*' -# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = 'New-JCAssociation' + # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. + AliasesToExport = 'New-JCAssociation' -# DSC resources to export from this module -# DscResourcesToExport = @() + # DSC resources to export from this module + # DscResourcesToExport = @() -# List of all modules packaged with this module -# ModuleList = @() + # List of all modules packaged with this module + # ModuleList = @() -# List of all files packaged with this module -# FileList = @() + # List of all files packaged with this module + # FileList = @() -# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. -PrivateData = @{ + # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. + PrivateData = @{ - PSData = @{ + PSData = @{ - # Tags applied to this module. These help with module discovery in online galleries. - Tags = 'JumpCloud', 'DaaS', 'Jump', 'Cloud', 'Directory' + # Tags applied to this module. These help with module discovery in online galleries. + Tags = 'JumpCloud', 'DaaS', 'Jump', 'Cloud', 'Directory' - # A URL to the license for this module. - LicenseUri = 'https://github.com/TheJumpCloud/support/blob/master/PowerShell/LICENSE' - - # A URL to the main website for this project. - ProjectUri = 'https://github.com/TheJumpCloud/support/wiki' - - # A URL to an icon representing this module. - IconUri = 'https://avatars1.githubusercontent.com/u/4927461?s=200&v=4' - - # ReleaseNotes of this module - ReleaseNotes = 'https://git.io/jc-pwsh-releasenotes' - - # Prerelease string of this module - # Prerelease = '' - - # Flag to indicate whether the module requires explicit user acceptance for install/update/save - # RequireLicenseAcceptance = $false - - # External dependent modules of this module - # ExternalModuleDependencies = @() - - } # End of PSData hashtable - -} # End of PrivateData hashtable - -# HelpInfo URI of this module -HelpInfoURI = 'https://github.com/TheJumpCloud/support/wiki' - -# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. -# DefaultCommandPrefix = '' + # A URL to the license for this module. + LicenseUri = 'https://github.com/TheJumpCloud/support/blob/master/PowerShell/LICENSE' + + # A URL to the main website for this project. + ProjectUri = 'https://github.com/TheJumpCloud/support/wiki' + + # A URL to an icon representing this module. + IconUri = 'https://avatars1.githubusercontent.com/u/4927461?s=200&v=4' + + # ReleaseNotes of this module + ReleaseNotes = 'https://git.io/jc-pwsh-releasenotes' + + # Prerelease string of this module + # Prerelease = '' + + # Flag to indicate whether the module requires explicit user acceptance for install/update/save + # RequireLicenseAcceptance = $false + + # External dependent modules of this module + # ExternalModuleDependencies = @() + + } # End of PSData hashtable + + } # End of PrivateData hashtable + + # HelpInfo URI of this module + HelpInfoURI = 'https://github.com/TheJumpCloud/support/wiki' + + # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. + # DefaultCommandPrefix = '' } diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index 75d703e18..4af5ee453 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -120,7 +120,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline # TODO: If not force, prompt for cascade_manager if the user is a manager -- Done # TODO: If manager is managed by another manager, cascade_manager to users managed by the manager -- Done if (!$force) { - if ($CascadeManager) { + if ($CascadeManager -and $isManager) { Switch ($CascadeManager) { 'NULL' { $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs diff --git a/PowerShell/ModuleChangelog.md b/PowerShell/ModuleChangelog.md index a5372f15b..9edeb651c 100644 --- a/PowerShell/ModuleChangelog.md +++ b/PowerShell/ModuleChangelog.md @@ -1,3 +1,20 @@ +## 2.16.0 + +Release Date: December 18, 2024 + +#### RELEASE NOTES + +``` +This release introduces support for cascading managers with Remove-JCUser +``` + +#### FEATURES: + +- Remove-JCUser - Added -CascadeManager (null, auto, id) parameter + - null - Manager field for managed users by the user being removed will be set to null + - auto - If the user (manager1) being removed is a manager but also managed by another user(manager2). The manager for managed users will cascade to manager2. + - Id - Manually specify the manager for users managed by the user/manager being removed + ## 2.15.0 Release Date: November 18, 2024 From ae30d68950a4f799a6ed4dc7c1177d50640bbc4b Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Thu, 12 Dec 2024 14:37:36 -0800 Subject: [PATCH 08/33] tests --- .../Public/Users/Remove-JCUser.ps1 | 40 ++++++--- .../Public/Users/Remove-JCUser.Tests.ps1 | 83 +++++++++++++++++++ 2 files changed, 110 insertions(+), 13 deletions(-) diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index 4af5ee453..4800b9287 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -36,7 +36,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline ) DynamicParam { if ($CascadeManager -eq 'ID' -and !$force) { - # Prompt for cascade_manager, user enters the ID of the new manager + # Prompt for CascadeManager, user enters the ID of the new manager $newManagerId = Read-Host "Enter the UserID of the new manager" # Validate if the Id is a JC User $validateUser = Get-JcSdkUser -Id $newManagerId @@ -113,12 +113,6 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline Write-Debug "User is not a manager" } - # Check if the user is managed by another manager - - - # TODO: If force or does not have a manager, default to cascade_manager=Null -- Done - # TODO: If not force, prompt for cascade_manager if the user is a manager -- Done - # TODO: If manager is managed by another manager, cascade_manager to users managed by the manager -- Done if (!$force) { if ($CascadeManager -and $isManager) { Switch ($CascadeManager) { @@ -137,7 +131,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } } } elseif ($isManager -and !$CascadeManager) { - # Prompt for cascade_managerk, user enters the ID of the new manager + # Prompt for CascadeManager, user enters the ID of the new manager $cascade_manager = Read-Host "User $($Username) is a manager. Do you want to reassign their managed users to another manager? (Y / N)" while ($cascade_manager -ne 'Y' -and $cascade_manager -ne 'N') { $cascade_manager = Read-Host "Please enter Y (Yes) or N (No)" @@ -145,18 +139,38 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline if ($cascade_manager -eq 'Y') { if ($hasManagerId) { $managerUsername = $UserHash.GetEnumerator().Where({ $_.Name -contains ($hasManagerId) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username - $prompt = "User $($Username) is managed by manager: $($managerUsername). Do you want to reassign their managed users to the manager who is managing this user? (Y/N)" - $cascade_manager = Read-Host $prompt + $cascade_manager = Read-Host "User $($Username) is managed by manager: $($managerUsername). Do you want to reassign their managed users to the manager who is managing this user? (Y/N)" + while ($cascade_manager -ne 'Y' -and $cascade_manager -ne 'N') { + $cascade_manager = Read-Host "Please enter Y (Yes) or N (No)" + } if ($cascade_manager -eq 'Y') { $newManagerId = $hasManagerId $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs - } else { + } elseif ($cascade_manager -eq 'N') { $newManagerId = Read-Host "Enter the UserID of the new manager" - $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + # Validate if the Id is a JC User + try { + $validateUser = Get-JcSdkUser -Id $newManagerId + Write-Debug "User $newManagerId is a valid JumpCloud User" + $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + } catch { + Write-Error "User does not exist. Please enter a valid UserID." + # Exit the script + Exit + } } } else { $newManagerId = Read-Host "Enter the UserID of the new manager" - $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + # Validate if the Id is a JC User + try { + $validateUser = Get-JcSdkUser -Id $newManagerId + Write-Debug "User $newManagerId is a valid JumpCloud User" + $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + } catch { + Write-Error "User does not exist. Please enter a valid UserID." + # Exit the script + Exit + } } } elseif ($cascade_manager -eq 'N') { $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index 2715b7d36..e2ff804fd 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -28,3 +28,86 @@ Describe -Tag:('JCUser') "Remove-JCUser 1.10" { } } +Describe -Tag:('JCUser') "Remove-JCUser 2.16" { + BeforeAll { } + It "Removes JumpCloud Manager and cascades the managed user managers to the given manager (id). Test CascadeManager param with by setting a manager id" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for each user + Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + # Remove the manager and set the new manager + Mock Read-Host { Return $ManagerUser2._id } + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager ID + + # The manager should be removed and the new manager should be set + $RemoveUser.Results | Should -Be 'Deleted' + # The new manager should be set + Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -Be $ManagerUser2._id + + # Clean up + Remove-JCUser -UserID $ManagerUser2._id -force + } + + It "Removes JumpCloud Manager but also managed by a manager. Test CascadeManager param auto" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for user + Set-JCUser -UserID $ManagerUser._id -ManagerID $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser + Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + # Remove the manager and set the new manager + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager Auto # Remove ManagerUser and should cascade to ManagerUser2 + + + # The manager should be removed and the new manager should be set + $RemoveUser.Results | Should -Be 'Deleted' + # The new manager should be set to ManagerUser2 + Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -Be $ManagerUser2._id + + # Clean up + Remove-JCUser -UserID $ManagerUser._id -force + Remove-JCUser -UserID $ManagerUser2._id -force + Remove-JCUser -UserID $NewUser._id -force + + } + + It "Removes JumpCloud user (manager) and set managed users manager to null. Test CascadeManager param null" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for user + Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + # Remove the manager and set the new manager + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL # Remove ManagerUser and should cascade to ManagerUser2 + + + # The manager should be removed and the new manager should be set + $RemoveUser.Results | Should -Be 'Deleted' + # The new manager should be set to ManagerUser2 + Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -BeNullOrEmpty + + # Clean up + Remove-JCUser -UserID $ManagerUser._id -force + Remove-JCUser -UserID $NewUser._id -force + } + + It "Tests for CascadeManager param with Force" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for user + Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL -force + + # Cascade manager and force should throw an error + $RemoveUser | Should -Throw + + # Clean up + Remove-JCUser -UserID $ManagerUser._id -force + Remove-JCUser -UserID $NewUser._id -force + } +} \ No newline at end of file From 018614bbe65a6a17bb245bed93ecf46a53d4ca5f Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Thu, 12 Dec 2024 14:58:02 -0800 Subject: [PATCH 09/33] test --- .../Private/Users/Delete-JcUser.Tests.ps1 | 44 +++++++++++++++++++ .../Public/Users/Remove-JCUser.Tests.ps1 | 7 +-- 2 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 diff --git a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 new file mode 100644 index 000000000..8868de576 --- /dev/null +++ b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 @@ -0,0 +1,44 @@ +Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { + BeforeAll { + $hdrs = @{ + 'Content-Type' = 'application/json' + 'Accept' = 'application/json' + 'X-API-KEY' = $env:JCAPIKEY + } + } + + It "Remove manager and cascade managed users to the new manager" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for each user + Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + # Remove the manager and set the new manager + + Delete-JCUser -Id $ManagerUser._id -managerId $ManagerUser2._id -Headers $hdrs | Should -Be 'Deleted' + + # The new manager should be set + Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -Be $ManagerUser2._id + + # Clean up + Remove-JCUser -UserID $ManagerUser2._id -force + Remove-JCUser -UserID $NewUser._id -force + + } + + It "Remove manager and cascade managed users to NULL" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for user + Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + + Delete-JCUser -Id $ManagerUser._id -Headers $hdrs | Should -Be 'Deleted' + + # The new manager should be set to NULL + Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -BeNullOrEmpty + + # Clean up + Remove-JCUser -UserID $NewUser._id -force + } \ No newline at end of file diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index e2ff804fd..53625c981 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -28,7 +28,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 1.10" { } } -Describe -Tag:('JCUser') "Remove-JCUser 2.16" { +Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { BeforeAll { } It "Removes JumpCloud Manager and cascades the managed user managers to the given manager (id). Test CascadeManager param with by setting a manager id" { $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser @@ -48,6 +48,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16" { # Clean up Remove-JCUser -UserID $ManagerUser2._id -force + Remove-JCUser -UserID $NewUser._id -force } It "Removes JumpCloud Manager but also managed by a manager. Test CascadeManager param auto" { @@ -68,7 +69,6 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16" { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -Be $ManagerUser2._id # Clean up - Remove-JCUser -UserID $ManagerUser._id -force Remove-JCUser -UserID $ManagerUser2._id -force Remove-JCUser -UserID $NewUser._id -force @@ -83,14 +83,12 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16" { # Remove the manager and set the new manager $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL # Remove ManagerUser and should cascade to ManagerUser2 - # The manager should be removed and the new manager should be set $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set to ManagerUser2 Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -BeNullOrEmpty # Clean up - Remove-JCUser -UserID $ManagerUser._id -force Remove-JCUser -UserID $NewUser._id -force } @@ -107,7 +105,6 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16" { $RemoveUser | Should -Throw # Clean up - Remove-JCUser -UserID $ManagerUser._id -force Remove-JCUser -UserID $NewUser._id -force } } \ No newline at end of file From d5792982afe220680f13039c9534694dba5ddb05 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Thu, 12 Dec 2024 16:20:07 -0800 Subject: [PATCH 10/33] test --- .../Private/Users/Delete-JcUser.ps1 | 8 ++++++- .../Public/Users/Remove-JCUser.ps1 | 2 +- .../Private/Users/Delete-JcUser.Tests.ps1 | 8 ++++++- .../Public/Users/Remove-JCUser.Tests.ps1 | 24 ++++++++++++++----- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 b/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 index fe04e0ea4..f1b1017c4 100644 --- a/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 +++ b/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 @@ -4,7 +4,8 @@ function Delete-JCUser { [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud User ID.')][ValidateNotNullOrEmpty()][System.String]$Id , [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud cascade manager ID.')][System.String]$managerId, # headers are required - [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud API headers.')][ValidateNotNullOrEmpty()][System.Collections.Hashtable]$Headers + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud API headers.')][ValidateNotNullOrEmpty()][System.Collections.Hashtable]$Headers, # Add force parameter + [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'Force the deletion of the user.')][System.Management.Automation.SwitchParameter]$force ) process { try { @@ -25,6 +26,11 @@ function Delete-JCUser { Write-Debug "User not deleted" $Status = 'Not Deleted' } + } elseif ($force) { + $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=null" + $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $Headers -UserAgent:(Get-JCUserAgent) + Write-Debug $delete + $Status = 'Deleted' } else { $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=null" $Username = Get-JcSdkUser -Id $Id | Select-Object -ExpandProperty username diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index 4800b9287..25b8b0539 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -181,7 +181,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } if ($force) { try { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -force } catch { $Status = $_.ErrorDetails } diff --git a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 index 8868de576..66c2e0178 100644 --- a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 @@ -5,6 +5,11 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { 'Accept' = 'application/json' 'X-API-KEY' = $env:JCAPIKEY } + + Mock -CommandName Read-Host -MockWith { + # Return "Y" to simulate 'Yes' answer + return "Y" + } } It "Remove manager and cascade managed users to the new manager" { @@ -41,4 +46,5 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { # Clean up Remove-JCUser -UserID $NewUser._id -force - } \ No newline at end of file + } +} \ No newline at end of file diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index 53625c981..dcca7a415 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -29,7 +29,13 @@ Describe -Tag:('JCUser') "Remove-JCUser 1.10" { } Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { - BeforeAll { } + BeforeAll { + + Mock -CommandName Read-Host -MockWith { + # Return "Y" to simulate 'Yes' answer + return "Y" + } + } It "Removes JumpCloud Manager and cascades the managed user managers to the given manager (id). Test CascadeManager param with by setting a manager id" { $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser @@ -38,7 +44,16 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # Set the manager for each user Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id # Remove the manager and set the new manager - Mock Read-Host { Return $ManagerUser2._id } + + Mock -CommandName Read-Host -MockWith { + # Return "Y" to simulate 'Yes' answer + param ( + [string]$prompt + ) + if ($prompt -eq "Enter the UserID of the new manager") { + return $ManagerUser2._id + } + } $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager ID # The manager should be removed and the new manager should be set @@ -99,10 +114,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # Set the manager for user Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id - $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL -force - - # Cascade manager and force should throw an error - $RemoveUser | Should -Throw + Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL -force | Should -Throw # Clean up Remove-JCUser -UserID $NewUser._id -force From 73f8655749517f8493ebb166b521880b0d8337e4 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Thu, 12 Dec 2024 16:30:52 -0800 Subject: [PATCH 11/33] manager --- .../Tests/Private/Users/Delete-JcUser.Tests.ps1 | 6 +++--- .../Tests/Public/Users/Remove-JCUser.Tests.ps1 | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 index 66c2e0178..3f3c495ee 100644 --- a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 @@ -18,10 +18,10 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser # Set the manager for each user - Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager - Delete-JCUser -Id $ManagerUser._id -managerId $ManagerUser2._id -Headers $hdrs | Should -Be 'Deleted' + Delete-JCUser -Id $ManagerUser._id -manager $ManagerUser2._id -Headers $hdrs | Should -Be 'Deleted' # The new manager should be set Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -Be $ManagerUser2._id @@ -37,7 +37,7 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser # Set the manager for user - Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id Delete-JCUser -Id $ManagerUser._id -Headers $hdrs | Should -Be 'Deleted' diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index dcca7a415..c72df7348 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -42,7 +42,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser # Set the manager for each user - Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager Mock -CommandName Read-Host -MockWith { @@ -72,8 +72,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser # Set the manager for user - Set-JCUser -UserID $ManagerUser._id -ManagerID $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser - Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + Set-JCUser -UserID $ManagerUser._id -manager $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager Auto # Remove ManagerUser and should cascade to ManagerUser2 @@ -94,7 +94,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser # Set the manager for user - Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL # Remove ManagerUser and should cascade to ManagerUser2 @@ -112,7 +112,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser # Set the manager for user - Set-JCUser -UserID $NewUser._id -ManagerID $ManagerUser._id + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL -force | Should -Throw From 1a138906db6fc0f91a0bbca584227c013726ae36 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Fri, 13 Dec 2024 09:13:41 -0800 Subject: [PATCH 12/33] fix dynamicparam --- .../Public/Users/Remove-JCUser.ps1 | 43 +++++++++++-------- .../Private/Users/Delete-JcUser.Tests.ps1 | 7 +-- .../Public/Users/Remove-JCUser.Tests.ps1 | 11 +---- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index 25b8b0539..e275f7bfb 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -35,19 +35,18 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline [string]$CascadeManager ) DynamicParam { - if ($CascadeManager -eq 'ID' -and !$force) { - # Prompt for CascadeManager, user enters the ID of the new manager - $newManagerId = Read-Host "Enter the UserID of the new manager" - # Validate if the Id is a JC User - $validateUser = Get-JcSdkUser -Id $newManagerId - if ($validateUser) { - Write-Debug "User $newManagerId is a valid JumpCloud User" - return $newManagerId - } else { - Write-Error "User does not exist. Please enter a valid UserID." - # Exit the script - Exit - } + # Create a dynamic parameter to get the -CascadeManagerId + if ($CascadeManager -eq 'ID') { + $paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary + $paramAttributesCollect = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute] + + $paramAttributes = New-Object -Type System.Management.Automation.ParameterAttribute + $paramAttributes.Mandatory = $true + $paramAttributesCollect.Add($paramAttributes) + + $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("CascadeManagerId", [string], $paramAttributesCollect) + $paramDictionary.Add('CascadeManagerId', $dynParam1) + return $paramDictionary } } begin { @@ -73,13 +72,22 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } $UserHash = Get-DynamicHash -Object User -returnProperties 'username', 'manager' - + # Validate dynamic parameter + if ($CascadeManager -eq 'ID') { + try { + $CascadeManagerId = $PSBoundParameters['CascadeManagerId'] + Get-JcSdkUser -Id $CascadeManagerId | Out-Null + } catch { + Write-Error "Please enter the UserID of the new manager." + # Exit the script + Exit + } + } } process { if ($PSCmdlet.ParameterSetName -eq 'Username' ) { if ($UserHash.Values.username -contains ($Username)) { $UserID = $UserHash.GetEnumerator().Where({ $_.Value.username -contains ($Username) }).Name - Write-Debug "UserID: $UserID" } else { Throw "Username does not exist. Run 'Get-JCUser | Select-Object username' to see a list of all your JumpCloud users." } @@ -89,9 +97,6 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline Write-Debug "UserID is a valid JumpCloud User" $UserId = $UserHash.GetEnumerator().Where({ $_.Name -contains ($UserID) }).Name $Username = $UserHash.GetEnumerator().Where({ $_.Name -contains ($UserID) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username - Write-Debug "UserID: $UserID" - Write-Debug "Username: $Username" - } else { Write-Error "User does not exist. Please enter a valid UserID." # Exit the script @@ -127,7 +132,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } } 'ID' { - $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $CascadeManagerId -Headers $hdrs } } } elseif ($isManager -and !$CascadeManager) { diff --git a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 index 3f3c495ee..0caa9bb88 100644 --- a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 @@ -21,8 +21,8 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager - Delete-JCUser -Id $ManagerUser._id -manager $ManagerUser2._id -Headers $hdrs | Should -Be 'Deleted' - + $RemoveUser = Delete-JCUser -Id $ManagerUser._id -manager $ManagerUser2._id -Headers $hdrs + $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -Be $ManagerUser2._id @@ -39,7 +39,8 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { # Set the manager for user Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - Delete-JCUser -Id $ManagerUser._id -Headers $hdrs | Should -Be 'Deleted' + $RemoveUser = Delete-JCUser -Id $ManagerUser._id -Headers $hdrs + $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set to NULL Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -BeNullOrEmpty diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index c72df7348..e647f359b 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -45,16 +45,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager - Mock -CommandName Read-Host -MockWith { - # Return "Y" to simulate 'Yes' answer - param ( - [string]$prompt - ) - if ($prompt -eq "Enter the UserID of the new manager") { - return $ManagerUser2._id - } - } - $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager ID + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager ID -CascadeManagerId $ManagerUser2._id # The manager should be removed and the new manager should be set $RemoveUser.Results | Should -Be 'Deleted' From d1b1251adf29d5d9b96581676dd7a693625f3d12 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Fri, 13 Dec 2024 09:35:02 -0800 Subject: [PATCH 13/33] skip commandresults --- .../Commands/CommandResults/Get-JCCommandResult.Tests.ps1 | 8 ++++---- .../CommandResults/Remove-JCCommandResult.Tests.ps1 | 2 +- PowerShell/JumpCloud Module/Tests/SetupOrg.ps1 | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/PowerShell/JumpCloud Module/Tests/Public/Commands/CommandResults/Get-JCCommandResult.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Commands/CommandResults/Get-JCCommandResult.Tests.ps1 index 9c365a62f..88e791e63 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Commands/CommandResults/Get-JCCommandResult.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Commands/CommandResults/Get-JCCommandResult.Tests.ps1 @@ -1,4 +1,4 @@ -Describe -Tag:('JCCommandResult') 'Get-JCCommandResults 1.0' { +Describe -Tag:('JCCommandResult') 'Get-JCCommandResults 1.0' -Skip { It "Gets all JumpCloud command results" { $CommandResults = Get-JCCommandResult $CommandResults.count | Should -BeGreaterThan 1 @@ -23,13 +23,13 @@ Describe -Tag:('JCCommandResult') 'Get-JCCommandResults 1.0' { $SingleCommandResult._id | Should -Not -BeNullOrEmpty } } -Describe -Tag:('JCCommandResult') "Get-JCCommandResult 1.4.1" { +Describe -Tag:('JCCommandResult') "Get-JCCommandResult 1.4.1" -Skip { It "Returns the total count of JumpCloud command results" { $TotalCount = Get-JCCommandResult -TotalCount $TotalCount | Should -BeGreaterThan 0 } } -Describe -Tag:('JCCommandResult') "Get-JCCommandResult 1.5.0" { +Describe -Tag:('JCCommandResult') "Get-JCCommandResult 1.5.0" -Skip { It "Returns a command result with the SystemID" { $CommandResult = Get-JCCommandResult | Select-Object -Last 1 $VerifySystemID = Get-JCSystem -SystemID $CommandResult.SystemID @@ -41,7 +41,7 @@ Describe -Tag:('JCCommandResult') "Get-JCCommandResult 1.5.0" { $CommandResult.system | Should -Be $VerifySystemID.displayname } } -Describe -Tag:('JCCommandResult') "Get-JCCommandResult 2.0" { +Describe -Tag:('JCCommandResult') "Get-JCCommandResult 2.0" -Skip { It "Returns command results with the workflowId" { $Command = Get-JCCommandResult | Select-Object -First 1 $CommandResults = Get-JCCommandResult -WorkflowId $Command.workflowId diff --git a/PowerShell/JumpCloud Module/Tests/Public/Commands/CommandResults/Remove-JCCommandResult.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Commands/CommandResults/Remove-JCCommandResult.Tests.ps1 index f958977ae..0c1c5dc69 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Commands/CommandResults/Remove-JCCommandResult.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Commands/CommandResults/Remove-JCCommandResult.Tests.ps1 @@ -1,4 +1,4 @@ -Describe -Tag:('JCCommandResult') 'Remove-JCCommandResult 1.0' { +Describe -Tag:('JCCommandResult') 'Remove-JCCommandResult 1.0' -Skip { <# It "Ensures the warning message is displayed by default, Deletes a single JumpCloud command result declaring -CommandResultIT" { $SingleCommandResult = Get-JCCommandResult | Select-Object -Last 1 diff --git a/PowerShell/JumpCloud Module/Tests/SetupOrg.ps1 b/PowerShell/JumpCloud Module/Tests/SetupOrg.ps1 index 95c3760bf..3a39e873e 100644 --- a/PowerShell/JumpCloud Module/Tests/SetupOrg.ps1 +++ b/PowerShell/JumpCloud Module/Tests/SetupOrg.ps1 @@ -75,7 +75,7 @@ Try { # Remove all Commands from an org If ($Commands) { $null = Get-JCCommand | Remove-JCCommand -force - $null = Get-JCCommandResult | Remove-JCCommandResult -force + #$null = Get-JCCommandResult | Remove-JCCommandResult -force Write-Host "[status] Removed commands: $($stopwatch.Elapsed)" } # Remove all RadiusServers from an org From 4d028f5dad95edb12d96554fe1f3919281ac0f6a Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Fri, 13 Dec 2024 09:53:40 -0800 Subject: [PATCH 14/33] mock --- PowerShell/JumpCloud Module/Docs/Remove-JCUser.md | 7 ++++++- .../JumpCloud Module/Public/Users/Remove-JCUser.ps1 | 9 +++------ .../Tests/Private/Users/Delete-JcUser.Tests.ps1 | 1 - .../Tests/Public/Users/Remove-JCUser.Tests.ps1 | 12 ++++++------ 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md index b3dbb4d92..dd99b2925 100644 --- a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md +++ b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md @@ -27,6 +27,11 @@ Remove-JCUser -UserID [-ByID] [-force] [] Remove-JCUser [-Username] [-ByID] -CascadeManager [] ``` +### CascadeManagerId +``` +Remove-JCUser [-Username] [-ByID] -CascadeManager -CascadeManagerId [] +``` + ## DESCRIPTION The Remove-JCUser function will remove a JumpCloud user from the JumpCloud organization. This will remove the deleted users access to any JumpCloud bound resources. @@ -63,7 +68,7 @@ Removes the JumpCloud user with the username 'cclemons'. If this user is a manag ### Example 5 ```powershell -PS C:\> Remove-JCUser cclemons -CascadeManager id +PS C:\> Remove-JCUser cclemons -CascadeManager id -CascadeManagerId cascadeManagerId ``` Removes the JumpCloud user with the username 'cclemons'. If this user is a manager, their managed users will be reassigned to the manager specified by the provided ID. diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index e275f7bfb..602d4821d 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -138,16 +138,10 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } elseif ($isManager -and !$CascadeManager) { # Prompt for CascadeManager, user enters the ID of the new manager $cascade_manager = Read-Host "User $($Username) is a manager. Do you want to reassign their managed users to another manager? (Y / N)" - while ($cascade_manager -ne 'Y' -and $cascade_manager -ne 'N') { - $cascade_manager = Read-Host "Please enter Y (Yes) or N (No)" - } if ($cascade_manager -eq 'Y') { if ($hasManagerId) { $managerUsername = $UserHash.GetEnumerator().Where({ $_.Name -contains ($hasManagerId) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username $cascade_manager = Read-Host "User $($Username) is managed by manager: $($managerUsername). Do you want to reassign their managed users to the manager who is managing this user? (Y/N)" - while ($cascade_manager -ne 'Y' -and $cascade_manager -ne 'N') { - $cascade_manager = Read-Host "Please enter Y (Yes) or N (No)" - } if ($cascade_manager -eq 'Y') { $newManagerId = $hasManagerId $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs @@ -179,6 +173,9 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } } elseif ($cascade_manager -eq 'N') { $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs + } else { + Write-Error "Please enter Y or N" + Exit } } else { $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs diff --git a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 index 0caa9bb88..a5c853b1a 100644 --- a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 @@ -20,7 +20,6 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { # Set the manager for each user Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager - $RemoveUser = Delete-JCUser -Id $ManagerUser._id -manager $ManagerUser2._id -Headers $hdrs $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index e647f359b..973c5c76f 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -31,10 +31,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 1.10" { Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { BeforeAll { - Mock -CommandName Read-Host -MockWith { - # Return "Y" to simulate 'Yes' answer - return "Y" - } + } It "Removes JumpCloud Manager and cascades the managed user managers to the given manager (id). Test CascadeManager param with by setting a manager id" { $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser @@ -44,7 +41,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # Set the manager for each user Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager - + Mock -CommandName Read-Host -MockWith { return "Y" } $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager ID -CascadeManagerId $ManagerUser2._id # The manager should be removed and the new manager should be set @@ -66,6 +63,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { Set-JCUser -UserID $ManagerUser._id -manager $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager + Mock -CommandName Read-Host -MockWith { return "Y" } $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager Auto # Remove ManagerUser and should cascade to ManagerUser2 @@ -87,6 +85,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # Set the manager for user Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager + Mock -CommandName Read-Host -MockWith { return "Y" } $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL # Remove ManagerUser and should cascade to ManagerUser2 # The manager should be removed and the new manager should be set @@ -104,10 +103,11 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # Set the manager for user Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - + Mock -CommandName Read-Host -MockWith { return "Y" } Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL -force | Should -Throw # Clean up + Remove-JCUser -UserID $ManagerUser._id -force Remove-JCUser -UserID $NewUser._id -force } } \ No newline at end of file From 640b343193e55f09028bae8a6ddfb65f83f886c5 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Fri, 13 Dec 2024 10:04:48 -0800 Subject: [PATCH 15/33] mock --- .../JumpCloud Module/Public/Users/Remove-JCUser.ps1 | 2 +- .../Tests/Public/Users/Remove-JCUser.Tests.ps1 | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index 602d4821d..3336af919 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -141,7 +141,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline if ($cascade_manager -eq 'Y') { if ($hasManagerId) { $managerUsername = $UserHash.GetEnumerator().Where({ $_.Name -contains ($hasManagerId) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username - $cascade_manager = Read-Host "User $($Username) is managed by manager: $($managerUsername). Do you want to reassign their managed users to the manager who is managing this user? (Y/N)" + $cascade_manager = Read-Host "User $($Username) is managed by manager: $($managerUsername). Do you want to reassign the managed users to the manager: $($managerUsername)? (Y/N)" if ($cascade_manager -eq 'Y') { $newManagerId = $hasManagerId $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index 973c5c76f..e66f9bcb9 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -42,6 +42,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager Mock -CommandName Read-Host -MockWith { return "Y" } + Mock -CommandName Read-Host -MockWith { return "Y" } + Mock -CommandName Read-Host -MockWith { return "Y" } $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager ID -CascadeManagerId $ManagerUser2._id # The manager should be removed and the new manager should be set @@ -64,6 +66,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager Mock -CommandName Read-Host -MockWith { return "Y" } + Mock -CommandName Read-Host -MockWith { return "Y" } + Mock -CommandName Read-Host -MockWith { return "Y" } $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager Auto # Remove ManagerUser and should cascade to ManagerUser2 @@ -86,6 +90,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager Mock -CommandName Read-Host -MockWith { return "Y" } + Mock -CommandName Read-Host -MockWith { return "Y" } + Mock -CommandName Read-Host -MockWith { return "Y" } $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL # Remove ManagerUser and should cascade to ManagerUser2 # The manager should be removed and the new manager should be set @@ -104,6 +110,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # Set the manager for user Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id Mock -CommandName Read-Host -MockWith { return "Y" } + Mock -CommandName Read-Host -MockWith { return "Y" } + Mock -CommandName Read-Host -MockWith { return "Y" } Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL -force | Should -Throw # Clean up From c55130fc62fe7c9754ad9c0cc329943b4365b93d Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Fri, 13 Dec 2024 19:32:28 -0800 Subject: [PATCH 16/33] test --- .../Public/Users/Remove-JCUser.Tests.ps1 | 60 +------------------ 1 file changed, 3 insertions(+), 57 deletions(-) diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index e66f9bcb9..dfa55cfbb 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -30,9 +30,9 @@ Describe -Tag:('JCUser') "Remove-JCUser 1.10" { } Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { BeforeAll { - - + Mock -CommandName Delete-JCUser -MockWith { return "Y" } } + It "Removes JumpCloud Manager and cascades the managed user managers to the given manager (id). Test CascadeManager param with by setting a manager id" { $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser @@ -40,10 +40,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # Set the manager for each user Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - # Remove the manager and set the new manager - Mock -CommandName Read-Host -MockWith { return "Y" } - Mock -CommandName Read-Host -MockWith { return "Y" } - Mock -CommandName Read-Host -MockWith { return "Y" } + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager ID -CascadeManagerId $ManagerUser2._id # The manager should be removed and the new manager should be set @@ -55,63 +52,12 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { Remove-JCUser -UserID $ManagerUser2._id -force Remove-JCUser -UserID $NewUser._id -force } - - It "Removes JumpCloud Manager but also managed by a manager. Test CascadeManager param auto" { - $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser - - # Set the manager for user - Set-JCUser -UserID $ManagerUser._id -manager $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser - Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - # Remove the manager and set the new manager - Mock -CommandName Read-Host -MockWith { return "Y" } - Mock -CommandName Read-Host -MockWith { return "Y" } - Mock -CommandName Read-Host -MockWith { return "Y" } - $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager Auto # Remove ManagerUser and should cascade to ManagerUser2 - - - # The manager should be removed and the new manager should be set - $RemoveUser.Results | Should -Be 'Deleted' - # The new manager should be set to ManagerUser2 - Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -Be $ManagerUser2._id - - # Clean up - Remove-JCUser -UserID $ManagerUser2._id -force - Remove-JCUser -UserID $NewUser._id -force - - } - - It "Removes JumpCloud user (manager) and set managed users manager to null. Test CascadeManager param null" { - $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser - - # Set the manager for user - Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - # Remove the manager and set the new manager - Mock -CommandName Read-Host -MockWith { return "Y" } - Mock -CommandName Read-Host -MockWith { return "Y" } - Mock -CommandName Read-Host -MockWith { return "Y" } - $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL # Remove ManagerUser and should cascade to ManagerUser2 - - # The manager should be removed and the new manager should be set - $RemoveUser.Results | Should -Be 'Deleted' - # The new manager should be set to ManagerUser2 - Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -BeNullOrEmpty - - # Clean up - Remove-JCUser -UserID $NewUser._id -force - } - It "Tests for CascadeManager param with Force" { $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser # Set the manager for user Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - Mock -CommandName Read-Host -MockWith { return "Y" } - Mock -CommandName Read-Host -MockWith { return "Y" } - Mock -CommandName Read-Host -MockWith { return "Y" } Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL -force | Should -Throw # Clean up From b9432092484865fd33ceb34b87e37e92c08e7a8b Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Fri, 13 Dec 2024 20:01:22 -0800 Subject: [PATCH 17/33] delete jcuser test --- .../Private/Users/Delete-JcUser.ps1 | 14 ++++++--- .../Public/Users/Remove-JCUser.ps1 | 31 ++++++++++--------- .../Private/Users/Delete-JcUser.Tests.ps1 | 23 ++++++++++++-- .../Public/Users/Remove-JCUser.Tests.ps1 | 20 ------------ 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 b/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 index f1b1017c4..6c6b3a431 100644 --- a/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 +++ b/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 @@ -4,15 +4,20 @@ function Delete-JCUser { [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud User ID.')][ValidateNotNullOrEmpty()][System.String]$Id , [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud cascade manager ID.')][System.String]$managerId, # headers are required - [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud API headers.')][ValidateNotNullOrEmpty()][System.Collections.Hashtable]$Headers, # Add force parameter - [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'Force the deletion of the user.')][System.Management.Automation.SwitchParameter]$force + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud API headers.')][ValidateNotNullOrEmpty()][System.Collections.Hashtable]$Headers, + [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'Force the deletion of the user.')][System.Management.Automation.SwitchParameter]$force, + # Parameter for $UserHash hashtable + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud User Hash.')][System.Collections.Hashtable]$UserHash ) + begin { + $Username = $UserHash.GetEnumerator().Where({ $_.Name -contains ($Id) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username + } process { try { if ($managerId) { $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=$($managerId)" - $Username = Get-JcSdkUser -Id $Id | Select-Object -ExpandProperty username - $ManagerUsername = Get-JcSdkUser -Id $managerId | Select-Object -ExpandProperty username + # Get the manager's username from $Userhash + $managerUsername = $UserHash.GetEnumerator().Where({ $_.Name -contains ($managerId) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username Write-Host "Deleting user: $($Username) and cascading managed users to manager: $($ManagerUsername)" -ForegroundColor Yellow $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" while ($prompt -ne 'Y' -and $prompt -ne 'N') { @@ -33,7 +38,6 @@ function Delete-JCUser { $Status = 'Deleted' } else { $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=null" - $Username = Get-JcSdkUser -Id $Id | Select-Object -ExpandProperty username Write-Host "Deleting user: $Username" -ForegroundColor Yellow $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" # Do a loop until the user enters Y or N diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index 3336af919..3f2acd2c4 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -74,11 +74,12 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline $UserHash = Get-DynamicHash -Object User -returnProperties 'username', 'manager' # Validate dynamic parameter if ($CascadeManager -eq 'ID') { - try { - $CascadeManagerId = $PSBoundParameters['CascadeManagerId'] - Get-JcSdkUser -Id $CascadeManagerId | Out-Null - } catch { - Write-Error "Please enter the UserID of the new manager." + $CascadeManagerId = $PSBoundParameters['CascadeManagerId'] + # Validate if the Id is a JC User from the $UserHash + if ($UserHash.ContainsKey($CascadeManagerId)) { + Write-Debug "CascadeManagerId is a valid JumpCloud User" + } else { + Write-Error "User does not exist. Please enter a valid UserID." # Exit the script Exit } @@ -122,17 +123,17 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline if ($CascadeManager -and $isManager) { Switch ($CascadeManager) { 'NULL' { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -UserHash $UserHash } 'Auto' { if ($hasManagerId) { - $Status = Delete-JCUser -Id $UserID -managerId $hasManagerId -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $hasManagerId -Headers $hdrs -UserHash $UserHash } else { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -UserHash $UserHash } } 'ID' { - $Status = Delete-JCUser -Id $UserID -managerId $CascadeManagerId -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $CascadeManagerId -Headers $hdrs -UserHash $UserHash } } } elseif ($isManager -and !$CascadeManager) { @@ -144,14 +145,14 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline $cascade_manager = Read-Host "User $($Username) is managed by manager: $($managerUsername). Do you want to reassign the managed users to the manager: $($managerUsername)? (Y/N)" if ($cascade_manager -eq 'Y') { $newManagerId = $hasManagerId - $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs -UserHash $UserHash } elseif ($cascade_manager -eq 'N') { $newManagerId = Read-Host "Enter the UserID of the new manager" # Validate if the Id is a JC User try { $validateUser = Get-JcSdkUser -Id $newManagerId Write-Debug "User $newManagerId is a valid JumpCloud User" - $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs -UserHash $UserHash } catch { Write-Error "User does not exist. Please enter a valid UserID." # Exit the script @@ -164,7 +165,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline try { $validateUser = Get-JcSdkUser -Id $newManagerId Write-Debug "User $newManagerId is a valid JumpCloud User" - $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs -UserHash $UserHash } catch { Write-Error "User does not exist. Please enter a valid UserID." # Exit the script @@ -172,18 +173,18 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } } } elseif ($cascade_manager -eq 'N') { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -UserHash $UserHash } else { Write-Error "Please enter Y or N" Exit } } else { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -UserHash $UserHash } } if ($force) { try { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -force + $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -force -UserHash $UserHash } catch { $Status = $_.ErrorDetails } diff --git a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 index a5c853b1a..d693bcee6 100644 --- a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 @@ -6,6 +6,8 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { 'X-API-KEY' = $env:JCAPIKEY } + $UserHash = Get-DynamicHash -Object User -returnProperties 'username', 'manager' + Mock -CommandName Read-Host -MockWith { # Return "Y" to simulate 'Yes' answer return "Y" @@ -20,7 +22,7 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { # Set the manager for each user Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager - $RemoveUser = Delete-JCUser -Id $ManagerUser._id -manager $ManagerUser2._id -Headers $hdrs + $RemoveUser = Delete-JCUser -Id $ManagerUser._id -manager $ManagerUser2._id -Headers $hdrs -UserHash $UserHash $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -Be $ManagerUser2._id @@ -38,7 +40,7 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { # Set the manager for user Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - $RemoveUser = Delete-JCUser -Id $ManagerUser._id -Headers $hdrs + $RemoveUser = Delete-JCUser -Id $ManagerUser._id -Headers $hdrs -UserHash $UserHash $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set to NULL @@ -47,4 +49,21 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { # Clean up Remove-JCUser -UserID $NewUser._id -force } + + It "Remove user with force" { + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + $RemoveUser = Delete-JCUser -Id $NewUser._id -Headers $hdrs -force -UserHash $UserHash + $RemoveUser.Results | Should -Be 'Deleted' + } + + It "Remove user without force" { + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + $RemoveUser = Delete-JCUser -Id $NewUser._id -Headers $hdrs -UserHash $UserHash + $RemoveUser.Results | Should -Be 'Deleted' + + # Clean up + Remove-JCUser -UserID $NewUser._id -force + } } \ No newline at end of file diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index dfa55cfbb..55458aebd 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -32,26 +32,6 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { BeforeAll { Mock -CommandName Delete-JCUser -MockWith { return "Y" } } - - It "Removes JumpCloud Manager and cascades the managed user managers to the given manager (id). Test CascadeManager param with by setting a manager id" { - $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser - - # Set the manager for each user - Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - - $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager ID -CascadeManagerId $ManagerUser2._id - - # The manager should be removed and the new manager should be set - $RemoveUser.Results | Should -Be 'Deleted' - # The new manager should be set - Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -Be $ManagerUser2._id - - # Clean up - Remove-JCUser -UserID $ManagerUser2._id -force - Remove-JCUser -UserID $NewUser._id -force - } It "Tests for CascadeManager param with Force" { $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser From 8308843e9cc7a7ed23aa6b27d7f42ceeef8b28e1 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Fri, 13 Dec 2024 20:19:28 -0800 Subject: [PATCH 18/33] test --- .../Tests/Private/Users/Delete-JcUser.Tests.ps1 | 3 --- .../Tests/Public/Users/Remove-JCUser.Tests.ps1 | 8 +------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 index d693bcee6..aabeb0567 100644 --- a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 @@ -62,8 +62,5 @@ Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { $RemoveUser = Delete-JCUser -Id $NewUser._id -Headers $hdrs -UserHash $UserHash $RemoveUser.Results | Should -Be 'Deleted' - - # Clean up - Remove-JCUser -UserID $NewUser._id -force } } \ No newline at end of file diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index 55458aebd..199c11957 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -34,14 +34,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { } It "Tests for CascadeManager param with Force" { $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser - - # Set the manager for user - Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL -force | Should -Throw - + { Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL -force } | Should -Throw # Clean up Remove-JCUser -UserID $ManagerUser._id -force - Remove-JCUser -UserID $NewUser._id -force } } \ No newline at end of file From 0024c11a5c15ca6ebb3cedf6c4ab2b7e8eef04b3 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Mon, 16 Dec 2024 14:09:18 -0800 Subject: [PATCH 19/33] move invoke + tests --- .../JumpCloud Module/Docs/Remove-JCUser.md | 10 +- .../Private/Users/Delete-JcUser.ps1 | 67 ----- .../Public/Users/Remove-JCUser.ps1 | 236 ++++++++++++++---- .../Private/Users/Delete-JcUser.Tests.ps1 | 66 ----- .../Public/Users/Remove-JCUser.Tests.ps1 | 112 +++++++++ PowerShell/ModuleChangelog.md | 7 +- 6 files changed, 306 insertions(+), 192 deletions(-) delete mode 100644 PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 delete mode 100644 PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 diff --git a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md index dd99b2925..07f8abaa2 100644 --- a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md +++ b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md @@ -27,9 +27,9 @@ Remove-JCUser -UserID [-ByID] [-force] [] Remove-JCUser [-Username] [-ByID] -CascadeManager [] ``` -### CascadeManagerId +### CascadeManagerUser ``` -Remove-JCUser [-Username] [-ByID] -CascadeManager -CascadeManagerId [] +Remove-JCUser [-Username] [-ByID] -CascadeManager -CascadeManagerUser [] ``` ## DESCRIPTION @@ -61,17 +61,17 @@ Removes the Jumpcloud user with Username 'cclemons'. If the user is a manager, t ### Example 4 ```powershell -PS C:\> Remove-JCUser cclemons -CascadeManager auto +PS C:\> Remove-JCUser cclemons -CascadeManager automatic ``` Removes the JumpCloud user with the username 'cclemons'. If this user is a manager (Manager1) and is also managed by another manager (Manager2), the users managed by Manager1 will be reassigned to Manager2 upon Manager1's removal. If Manager1 is not managed by anyone, the manager field for the users managed by Manager1 will be set to null. ### Example 5 ```powershell -PS C:\> Remove-JCUser cclemons -CascadeManager id -CascadeManagerId cascadeManagerId +PS C:\> Remove-JCUser cclemons -CascadeManager id -CascadeManager User -CascadeManagerUser username/id ``` -Removes the JumpCloud user with the username 'cclemons'. If this user is a manager, their managed users will be reassigned to the manager specified by the provided ID. +Removes the JumpCloud user with the username 'cclemons'. If this user is a manager, their managed users will be reassigned to the manager specified by the provided username/id with CascadeManagerUser. ## PARAMETERS diff --git a/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 b/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 deleted file mode 100644 index 6c6b3a431..000000000 --- a/PowerShell/JumpCloud Module/Private/Users/Delete-JcUser.ps1 +++ /dev/null @@ -1,67 +0,0 @@ -function Delete-JCUser { - # UserId and cascade_manager are required - param ( - [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud User ID.')][ValidateNotNullOrEmpty()][System.String]$Id - , [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud cascade manager ID.')][System.String]$managerId, - # headers are required - [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud API headers.')][ValidateNotNullOrEmpty()][System.Collections.Hashtable]$Headers, - [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'Force the deletion of the user.')][System.Management.Automation.SwitchParameter]$force, - # Parameter for $UserHash hashtable - [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The JumpCloud User Hash.')][System.Collections.Hashtable]$UserHash - ) - begin { - $Username = $UserHash.GetEnumerator().Where({ $_.Name -contains ($Id) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username - } - process { - try { - if ($managerId) { - $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=$($managerId)" - # Get the manager's username from $Userhash - $managerUsername = $UserHash.GetEnumerator().Where({ $_.Name -contains ($managerId) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username - Write-Host "Deleting user: $($Username) and cascading managed users to manager: $($ManagerUsername)" -ForegroundColor Yellow - $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" - while ($prompt -ne 'Y' -and $prompt -ne 'N') { - $prompt = Read-Host "Please enter Y or N" - } - if ($prompt -eq 'Y') { - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $Headers -UserAgent:(Get-JCUserAgent) - Write-Debug $delete - $Status = 'Deleted' - } elseif ($prompt -eq 'N') { - Write-Debug "User not deleted" - $Status = 'Not Deleted' - } - } elseif ($force) { - $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=null" - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $Headers -UserAgent:(Get-JCUserAgent) - Write-Debug $delete - $Status = 'Deleted' - } else { - $URI = "$JCUrlBasePath/api/systemusers/$($Id)?cascade_manager=null" - Write-Host "Deleting user: $Username" -ForegroundColor Yellow - $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" - # Do a loop until the user enters Y or N - while ($prompt -ne 'Y' -and $prompt -ne 'N') { - $prompt = Read-Host "Please enter Y or N" - } - if ($prompt -eq 'Y') { - $delete = Invoke-RestMethod -Method Delete -Uri $URI -Headers $Headers -UserAgent:(Get-JCUserAgent) - Write-Debug $delete - $Status = 'Deleted' - } elseif ($prompt -eq 'N') { - Write-Debug "User not deleted" - $Status = 'Not Deleted' - } - } - } catch { - $Status = $_.ErrorDetails - } - $FormattedResults = [PSCustomObject]@{ - 'User' = $Username - 'Results' = $Status - } - } - end { - return $FormattedResults - } -} \ No newline at end of file diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index 3f2acd2c4..3dd562998 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -1,6 +1,5 @@ function Remove-JCUser () { [CmdletBinding(DefaultParameterSetName = 'Username')] - param ( [Parameter(Mandatory, @@ -31,12 +30,12 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline [Switch] $force, [Parameter(HelpMessage = 'A SwitchParameter for Cascading the manager of the user to the users managed by the user. NULL, AUTOMATIC (bubble up), ID (prompt for manager ID)')] - [ValidateSet('NULL', 'Auto', 'ID')] + [ValidateSet('NULL', 'Automatic', 'User')] [string]$CascadeManager ) DynamicParam { # Create a dynamic parameter to get the -CascadeManagerId - if ($CascadeManager -eq 'ID') { + if ($PSBoundParameters['CascadeManager'] -eq 'User') { $paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary $paramAttributesCollect = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute] @@ -44,8 +43,8 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline $paramAttributes.Mandatory = $true $paramAttributesCollect.Add($paramAttributes) - $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("CascadeManagerId", [string], $paramAttributesCollect) - $paramDictionary.Add('CascadeManagerId', $dynParam1) + $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("CascadeManagerUser", [string], $paramAttributesCollect) + $paramDictionary.Add('CascadeManagerUser', $dynParam1) return $paramDictionary } } @@ -73,15 +72,34 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline $UserHash = Get-DynamicHash -Object User -returnProperties 'username', 'manager' # Validate dynamic parameter - if ($CascadeManager -eq 'ID') { - $CascadeManagerId = $PSBoundParameters['CascadeManagerId'] - # Validate if the Id is a JC User from the $UserHash - if ($UserHash.ContainsKey($CascadeManagerId)) { - Write-Debug "CascadeManagerId is a valid JumpCloud User" + if ($PSBoundParameters['CascadeManager'] -eq 'User') { + $CascadeManagerValue = $PSBoundParameters['CascadeManagerUser'] + + # Validate if ID or Username is passed + $regexPattern = [Regex]'^[a-z0-9]{24}$' + if ($CascadeManagerValue -match $regexPattern) { + # Validate if the Id is a JC User from the $UserHash + if ($UserHash.ContainsKey($CascadeManagerValue)) { + $CascadeManagerId = $CascadeManagerValue + Write-Debug "$CascadeManagerId is a valid JumpCloud User" + } else { + Write-Error "User does not exist. Please enter a valid UserID." + # Throw the script + Throw + } } else { - Write-Error "User does not exist. Please enter a valid UserID." - # Exit the script - Exit + # Validate if the Username is a JC User from the $UserHash + if ($UserHash.Values.username -contains ($CascadeManagerValue)) { + Write-Debug "$CascadeManagerValue is a valid JumpCloud User usr" + # Get the UserID from the $UserHash + $CascadeManagerId = $UserHash.GetEnumerator().Where({ $_.Value.username -contains ($CascadeManagerValue) }).Name + Write-Debug "CascadeManagerId is a valid JumpCloud User $CascadeManagerId" + + } else { + Write-Error "User does not exist. Please enter a valid Username." + # Throw the script + Throw + } } } } @@ -95,13 +113,12 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline } elseif ($PSCmdlet.ParameterSetName -eq 'UserID') { # Validate if the Id is a JC User from the $UserHash if ($UserHash.ContainsKey($UserID)) { - Write-Debug "UserID is a valid JumpCloud User" - $UserId = $UserHash.GetEnumerator().Where({ $_.Name -contains ($UserID) }).Name $Username = $UserHash.GetEnumerator().Where({ $_.Name -contains ($UserID) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username + Write-Host "UserID $($UserId) is a valid JumpCloud User: $($Username)" } else { Write-Error "User does not exist. Please enter a valid UserID." - # Exit the script - Exit + # Throw the script + Throw } } # Check if the user is a manager @@ -111,91 +128,208 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline # $managerCount = ($UserHash.Values.manager -eq $UserID).Count # Save each user the manager is managing in a list $managedUsers = $UserHash.GetEnumerator().Where({ $_.Value.manager -eq $UserID }).Name - Write-Debug "Manager is managing $managedUsers users" + Write-Debug "Manager $($Username) is managing $managedUsers users" $hasManagerId = Get-JcSdkUser -Id $UserID | Select-Object -ExpandProperty manager - Write-Debug "Manager is managed by $hasManagerId" + Write-Debug "Manager $($Username) is managed by $hasManagerId" } else { $isManager = $false - Write-Debug "User is not a manager" + Write-Debug "User $($Username) is not a manager" } if (!$force) { - if ($CascadeManager -and $isManager) { + if ($PSBoundParameters['CascadeManager'] -and $isManager) { + Write-Debug "Switching on $CascadeManager" Switch ($CascadeManager) { + 'NULL' { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -UserHash $UserHash + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=null" + Write-Host "Deleting user: $Username" -ForegroundColor Yellow + $Status = 'Deleted' } - 'Auto' { + 'Automatic' { if ($hasManagerId) { - $Status = Delete-JCUser -Id $UserID -managerId $hasManagerId -Headers $hdrs -UserHash $UserHash + Write-Host "Deleting user: $Username and cascading its managed users manager to $($hasManagerId)" -ForegroundColor Yellow + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=$($hasManagerId)" + $Status = "Deleted" + } else { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -UserHash $UserHash + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=null" + Write-Host "Deleting user: $Username" -ForegroundColor Yellow + $Status = "Deleted" } } - 'ID' { - $Status = Delete-JCUser -Id $UserID -managerId $CascadeManagerId -Headers $hdrs -UserHash $UserHash + 'User' { + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=$($CascadeManagerId)" + Write-Host "Deleting user: $Username and cascading the managed users manager to the manager $($CascadeManagerId)" -ForegroundColor Yellow + $Status = "Deleted" } } - } elseif ($isManager -and !$CascadeManager) { + try { + Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) | Out-Null + } catch { + # Get the error details + $Status = $_.ErrorDetails + Write-Error $_.ErrorDetails + } + } elseif ($isManager -and !$PSBoundParameters['CascadeManager']) { # Prompt for CascadeManager, user enters the ID of the new manager - $cascade_manager = Read-Host "User $($Username) is a manager. Do you want to reassign their managed users to another manager? (Y / N)" + $cascade_manager = Read-Host "User $($Username) is a manager and is managing $($managedUsers.Count) user(s). Do you want to reassign their managed users to another manager? (Y / N)" if ($cascade_manager -eq 'Y') { if ($hasManagerId) { $managerUsername = $UserHash.GetEnumerator().Where({ $_.Name -contains ($hasManagerId) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username $cascade_manager = Read-Host "User $($Username) is managed by manager: $($managerUsername). Do you want to reassign the managed users to the manager: $($managerUsername)? (Y/N)" if ($cascade_manager -eq 'Y') { $newManagerId = $hasManagerId - $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs -UserHash $UserHash + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=$($newManagerId)" + Write-Host "Deleting user: $Username and cascading the managed users manager to the manager $($newManagerId)" -ForegroundColor Yellow + $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" + if ($prompt -eq 'Y') { + try { + Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) | Out-Null + $Status = "Deleted" + } catch { + # Get the error details + $Status = $_.ErrorDetails + Write-Error $_.ErrorDetails + } + + } elseif ($prompt -eq 'N') { + $Status = 'Not Deleted' + } else { + Write-Error "Please enter Y or N" + Throw + } + } elseif ($cascade_manager -eq 'N') { $newManagerId = Read-Host "Enter the UserID of the new manager" # Validate if the Id is a JC User - try { - $validateUser = Get-JcSdkUser -Id $newManagerId - Write-Debug "User $newManagerId is a valid JumpCloud User" - $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs -UserHash $UserHash - } catch { + if ($UserHash.ContainsKey($newManagerId)) { + Write-Host "User $newManagerId is a valid JumpCloud User" + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=$($newManagerId)" + Write-Host "Deleting user: $Username and cascading the managed users manager to the manager $($newManagerId)" -ForegroundColor Yellow + $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" + if ($prompt -eq 'Y') { + try { + Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) | Out-Null + $Status = "Deleted" + } catch { + # Get the error details + $Status = $_.ErrorDetails + Write-Error $_.ErrorDetails + } + } elseif ($prompt -eq 'N') { + $Status = 'Not Deleted' + } else { + Write-Error "Please enter Y or N" + Throw + } + } else { Write-Error "User does not exist. Please enter a valid UserID." - # Exit the script - Exit + # Throw the script + Throw } } } else { $newManagerId = Read-Host "Enter the UserID of the new manager" # Validate if the Id is a JC User - try { - $validateUser = Get-JcSdkUser -Id $newManagerId - Write-Debug "User $newManagerId is a valid JumpCloud User" - $Status = Delete-JCUser -Id $UserID -managerId $newManagerId -Headers $hdrs -UserHash $UserHash - } catch { + if ($UserHash.ContainsKey($newManagerId)) { + Write-Host "User $newManagerId is a valid JumpCloud User" + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=$($newManagerId)" + Write-Host "Deleting user: $Username and cascading the managed users manager to the manager $($newManagerId)" -ForegroundColor Yellow + $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" + if ($prompt -eq 'Y') { + try { + Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) | Out-Null + $Status = "Deleted" + } catch { + # Get the error details + $Status = $_.ErrorDetails + Write-Error $_.ErrorDetails + } + } elseif ($prompt -eq 'N') { + $Status = 'Not Deleted' + } else { + Write-Error "Please enter Y or N" + Throw + } + } else { Write-Error "User does not exist. Please enter a valid UserID." - # Exit the script - Exit + # Throw the script + Throw } } } elseif ($cascade_manager -eq 'N') { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -UserHash $UserHash + #$Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -UserHash $UserHash + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=null" + Write-Host "Deleting user: $Username" -ForegroundColor Yellow + $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" + if ($prompt -eq 'Y') { + try { + Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) | Out-Null + $Status = 'Deleted' + } catch { + $Status = $_.ErrorDetails + } + } elseif ($prompt -eq 'N') { + Write-Host "User not deleted" + $Status = 'Not Deleted' + } else { + Write-Error "Please enter Y or N" + Throw + } } else { Write-Error "Please enter Y or N" - Exit + Throw } } else { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -UserHash $UserHash + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=null" + Write-Host "Deleting user: $Username" -ForegroundColor Yellow + $prompt = Read-Host "Are you sure you wish to delete the user: $($Username)? (Y/N)" + if ($prompt -eq 'Y') { + try { + Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) | Out-Null + $Status = "Deleted" + } catch { + Write-Error $_.ErrorDetails + } + + } elseif ($prompt -eq 'N') { + $Status = 'Not Deleted' + } else { + Write-Error "Please enter Y or N" + Throw + } } } if ($force) { try { - $Status = Delete-JCUser -Id $UserID -managerId $null -Headers $hdrs -force -UserHash $UserHash + $URI = "$JCUrlBasePath/api/systemusers/$($UserID)?cascade_manager=null" + Write-Host "Deleting user: $Username" -ForegroundColor Yellow + Invoke-RestMethod -Method Delete -Uri $URI -Headers $hdrs -UserAgent:(Get-JCUserAgent) | Out-Null + $Status = "Deleted" } catch { $Status = $_.ErrorDetails } } - $FormattedResults = $Status + + try { + $FormattedResults = [PSCustomObject]@{ + 'User' = $Username + 'Results' = $Status + } + } catch { + $FormattedResults = [PSCustomObject]@{ + 'User' = $Username + 'Results' = $_.ErrorDetails + } + Write-Error $_.ErrorDetails + } + + $deletedArray += $FormattedResults } end { - return $deletedArray - } } \ No newline at end of file diff --git a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 deleted file mode 100644 index aabeb0567..000000000 --- a/PowerShell/JumpCloud Module/Tests/Private/Users/Delete-JcUser.Tests.ps1 +++ /dev/null @@ -1,66 +0,0 @@ -Describe -Tag:('JCUser') "Delete-JCUser 2.16.0" { - BeforeAll { - $hdrs = @{ - 'Content-Type' = 'application/json' - 'Accept' = 'application/json' - 'X-API-KEY' = $env:JCAPIKEY - } - - $UserHash = Get-DynamicHash -Object User -returnProperties 'username', 'manager' - - Mock -CommandName Read-Host -MockWith { - # Return "Y" to simulate 'Yes' answer - return "Y" - } - } - - It "Remove manager and cascade managed users to the new manager" { - $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser - - # Set the manager for each user - Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - # Remove the manager and set the new manager - $RemoveUser = Delete-JCUser -Id $ManagerUser._id -manager $ManagerUser2._id -Headers $hdrs -UserHash $UserHash - $RemoveUser.Results | Should -Be 'Deleted' - # The new manager should be set - Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -Be $ManagerUser2._id - - # Clean up - Remove-JCUser -UserID $ManagerUser2._id -force - Remove-JCUser -UserID $NewUser._id -force - - } - - It "Remove manager and cascade managed users to NULL" { - $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser - - # Set the manager for user - Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - - $RemoveUser = Delete-JCUser -Id $ManagerUser._id -Headers $hdrs -UserHash $UserHash - $RemoveUser.Results | Should -Be 'Deleted' - - # The new manager should be set to NULL - Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager | Should -BeNullOrEmpty - - # Clean up - Remove-JCUser -UserID $NewUser._id -force - } - - It "Remove user with force" { - $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser - - $RemoveUser = Delete-JCUser -Id $NewUser._id -Headers $hdrs -force -UserHash $UserHash - $RemoveUser.Results | Should -Be 'Deleted' - } - - It "Remove user without force" { - $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser - - $RemoveUser = Delete-JCUser -Id $NewUser._id -Headers $hdrs -UserHash $UserHash - $RemoveUser.Results | Should -Be 'Deleted' - } -} \ No newline at end of file diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index 199c11957..4ed542411 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -38,4 +38,116 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # Clean up Remove-JCUser -UserID $ManagerUser._id -force } + + It "Removes JumpCloud Manager but also managed by a manager. Test CascadeManager param auto" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for user + Set-JCUser -UserID $ManagerUser._id -manager $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id + # Remove the manager and set the new manager + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager Auto # Remove ManagerUser and should cascade to ManagerUser2 + + + # The manager should be removed and the new manager should be set + $RemoveUser.Results | Should -Be 'Deleted' + # The new manager should be set to ManagerUser2 + { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager } | Should -Be $ManagerUser2._id + # Clean up + Remove-JCUser -UserID $ManagerUser2._id -force + Remove-JCUser -UserID $NewUser._id -force + + } + + It "Removes JumpCloud user (manager) and set managed users manager to null. Test CascadeManager param null" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for user + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id + # Remove the manager and set the new manager + + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL # Remove ManagerUser and should cascade to ManagerUser2 + + # The manager should be removed and the new manager should be set + $RemoveUser.Results | Should -Be 'Deleted' + # The new manager should be set to ManagerUser2 + { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager } | Should -BeNullOrEmpty + # Clean up + Remove-JCUser -UserID $NewUser._id -force + } + + It "Removes JumpCloud user (manager) and set managed users manager to CascadeManagerUser. Test CascadeManager param User (Id)" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for user + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id + + # Remove the manager and set the new manager + $RemoveUser = Remove-JCUser -UserId $ManagerUser._id -CascadeManager User -CascadeManagerUser $ManagerUser2._id + # The manager should be removed and the new manager should be set + $RemoveUser.Results | Should -Be 'Deleted' + # The new manager should be set to ManagerUser2 + { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager } | Should -Be $ManagerUser2._id + # Clean up + Remove-JCUser -UserID $ManagerUser2._id -force + Remove-JCUser -UserID $NewUser._id -force + } + + It "Removes JumpCloud user (manager) and set managed users manager to CascadeManagerUser. Test CascadeManager param User (Username)" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for user + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id + + # Remove the manager and set the new manager + $RemoveUser = Remove-JCUser -UserId $ManagerUser._id -CascadeManager User -CascadeManagerUser $ManagerUser2.username + # The manager should be removed and the new manager should be set + $RemoveUser.Results | Should -Be 'Deleted' + # The new manager should be set to ManagerUser2 + { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager } | Should -Be $ManagerUser2._id + # Clean up + Remove-JCUser -UserID $ManagerUser2._id -force + Remove-JCUser -UserID $NewUser._id -force + } + + It "Removes JumpCloud user (manager) and set managed users manager to CascadeManagerUser. Test Invalid CascadeManagerUser param" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for user + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id + + # Invalid CascadeManagerUser should throw an error + { Remove-JCUser -UserId $ManagerUser._id -CascadeManager User -CascadeManagerUser "InvalidJCUserxyz" } | Should -Throw + Remove-JCUser -UserID $ManagerUser._id -force + Remove-JCUser -UserID $NewUser._id -force + } + + It "Remove JumpCloud Manager1 that is also managed by a Manager2. Cascade managed users manager to Manager2" { + $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser + $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser + + # Set the manager for user + Set-JCUser -UserID $ManagerUser._id -manager $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser + Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id + + # Remove the manager and set the new manager + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id # Remove ManagerUser and should cascade to ManagerUser2. Prompts should be auto answered with 'Y' + + # The manager should be removed and the new manager should be set + $RemoveUser.Results | Should -Be 'Deleted' + # The new manager should be set to ManagerUser2 + { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager } | Should -Be $ManagerUser2._id + # Clean up + Remove-JCUser -UserID $ManagerUser2._id -force + Remove-JCUser -UserID $NewUser._id -force + } } \ No newline at end of file diff --git a/PowerShell/ModuleChangelog.md b/PowerShell/ModuleChangelog.md index 9edeb651c..1b434f6db 100644 --- a/PowerShell/ModuleChangelog.md +++ b/PowerShell/ModuleChangelog.md @@ -10,10 +10,11 @@ This release introduces support for cascading managers with Remove-JCUser #### FEATURES: -- Remove-JCUser - Added -CascadeManager (null, auto, id) parameter +- Remove-JCUser - Added -CascadeManager (null, automatic, user) parameter - null - Manager field for managed users by the user being removed will be set to null - - auto - If the user (manager1) being removed is a manager but also managed by another user(manager2). The manager for managed users will cascade to manager2. - - Id - Manually specify the manager for users managed by the user/manager being removed + - automatic - If the user (manager1) being removed is a manager but also managed by another user(manager2). The manager for managed users will cascade to manager2. + - User - Manually specify the manager for users managed by the user/manager being removed + - -CascadeManagerUser Id/Username ## 2.15.0 From 7f8690ec9b7d3f4238c16120d789a8e30d9203d1 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Mon, 16 Dec 2024 14:24:45 -0800 Subject: [PATCH 20/33] test --- .../Tests/Public/Users/Remove-JCUser.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index 4ed542411..4ea7201ee 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -30,7 +30,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 1.10" { } Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { BeforeAll { - Mock -CommandName Delete-JCUser -MockWith { return "Y" } + # Mock Read-Host to auto answer 'Y' to the prompt + Mock -CommandName 'Read-Host' -MockWith { return 'Y' } } It "Tests for CascadeManager param with Force" { $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser From 427fe3cfadea83382a5cb5f2b26f32d0ccc7470a Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Mon, 16 Dec 2024 14:36:17 -0800 Subject: [PATCH 21/33] test --- .../Tests/Public/Users/Remove-JCUser.Tests.ps1 | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index 4ea7201ee..1f102fbfc 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -49,7 +49,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { Set-JCUser -UserID $ManagerUser._id -manager $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager - $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager Auto # Remove ManagerUser and should cascade to ManagerUser2 + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager Automatic # Remove ManagerUser and should cascade to ManagerUser2 # The manager should be removed and the new manager should be set @@ -75,7 +75,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # The manager should be removed and the new manager should be set $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set to ManagerUser2 - { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager } | Should -BeNullOrEmpty + $manager = Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager + $manager | Should -BeNullOrEmpty # Clean up Remove-JCUser -UserID $NewUser._id -force } @@ -93,7 +94,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # The manager should be removed and the new manager should be set $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set to ManagerUser2 - { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager } | Should -Be $ManagerUser2._id + $manager = Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager + $manager | Should -Be $ManagerUser2._id # Clean up Remove-JCUser -UserID $ManagerUser2._id -force Remove-JCUser -UserID $NewUser._id -force @@ -112,7 +114,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # The manager should be removed and the new manager should be set $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set to ManagerUser2 - { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager } | Should -Be $ManagerUser2._id + $manager = Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager + $manager | Should -Be $ManagerUser2._id # Clean up Remove-JCUser -UserID $ManagerUser2._id -force Remove-JCUser -UserID $NewUser._id -force @@ -139,14 +142,17 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # Set the manager for user Set-JCUser -UserID $ManagerUser._id -manager $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - + Mock -CommandName 'Read-Host' -MockWith { return 'Y' } + Mock -CommandName 'Read-Host' -MockWith { return 'Y' } + Mock -CommandName 'Read-Host' -MockWith { return 'Y' } # Remove the manager and set the new manager $RemoveUser = Remove-JCUser -UserID $ManagerUser._id # Remove ManagerUser and should cascade to ManagerUser2. Prompts should be auto answered with 'Y' # The manager should be removed and the new manager should be set $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set to ManagerUser2 - { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager } | Should -Be $ManagerUser2._id + $manager = Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager + $manager | Should -Be $ManagerUser2._id # Clean up Remove-JCUser -UserID $ManagerUser2._id -force Remove-JCUser -UserID $NewUser._id -force From b42fb40086099cb8947db16b335bee570b6585a7 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Mon, 16 Dec 2024 14:44:12 -0800 Subject: [PATCH 22/33] test --- .../Tests/Public/Users/Remove-JCUser.Tests.ps1 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index 1f102fbfc..44b48da6b 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -30,8 +30,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 1.10" { } Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { BeforeAll { - # Mock Read-Host to auto answer 'Y' to the prompt - Mock -CommandName 'Read-Host' -MockWith { return 'Y' } + } It "Tests for CascadeManager param with Force" { $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser @@ -55,7 +54,8 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # The manager should be removed and the new manager should be set $RemoveUser.Results | Should -Be 'Deleted' # The new manager should be set to ManagerUser2 - { Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager } | Should -Be $ManagerUser2._id + $manager = Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager + $manager | Should -Be $ManagerUser2._id # Clean up Remove-JCUser -UserID $ManagerUser2._id -force Remove-JCUser -UserID $NewUser._id -force @@ -142,8 +142,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { # Set the manager for user Set-JCUser -UserID $ManagerUser._id -manager $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - Mock -CommandName 'Read-Host' -MockWith { return 'Y' } - Mock -CommandName 'Read-Host' -MockWith { return 'Y' } + Mock -CommandName 'Read-Host' -MockWith { return 'Y' } # Remove the manager and set the new manager $RemoveUser = Remove-JCUser -UserID $ManagerUser._id # Remove ManagerUser and should cascade to ManagerUser2. Prompts should be auto answered with 'Y' From 6d0e9195baf44372e02a819b0004818ed5363c6d Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Mon, 16 Dec 2024 14:54:43 -0800 Subject: [PATCH 23/33] test --- .../JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index 44b48da6b..e19613fae 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -143,7 +143,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { Set-JCUser -UserID $ManagerUser._id -manager $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - Mock -CommandName 'Read-Host' -MockWith { return 'Y' } + Mock -CommandName Read-Host -MockWith { return 'y' } # Remove the manager and set the new manager $RemoveUser = Remove-JCUser -UserID $ManagerUser._id # Remove ManagerUser and should cascade to ManagerUser2. Prompts should be auto answered with 'Y' From 37931fe4fd6da1038da788873eed1a80dd04ead3 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Mon, 16 Dec 2024 14:58:57 -0800 Subject: [PATCH 24/33] test cascade --- .../Public/Users/Remove-JCUser.Tests.ps1 | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index e19613fae..cbf6ee024 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -133,27 +133,4 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { Remove-JCUser -UserID $ManagerUser._id -force Remove-JCUser -UserID $NewUser._id -force } - - It "Remove JumpCloud Manager1 that is also managed by a Manager2. Cascade managed users manager to Manager2" { - $ManagerUser = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $ManagerUser2 = New-RandomUser "PesterTest$(Get-Date -Format MM-dd-yyyy)" | New-JCUser - $NewUser = New-RandomUser -Domain "delUser.$(New-RandomString -NumberOfChars 5)" | New-JCUser - - # Set the manager for user - Set-JCUser -UserID $ManagerUser._id -manager $ManagerUser2._id # ManagerUser2 is the manager of ManagerUser - Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id - - Mock -CommandName Read-Host -MockWith { return 'y' } - # Remove the manager and set the new manager - $RemoveUser = Remove-JCUser -UserID $ManagerUser._id # Remove ManagerUser and should cascade to ManagerUser2. Prompts should be auto answered with 'Y' - - # The manager should be removed and the new manager should be set - $RemoveUser.Results | Should -Be 'Deleted' - # The new manager should be set to ManagerUser2 - $manager = Get-JCUser -UserID $NewUser._id | Select-Object -ExpandProperty manager - $manager | Should -Be $ManagerUser2._id - # Clean up - Remove-JCUser -UserID $ManagerUser2._id -force - Remove-JCUser -UserID $NewUser._id -force - } } \ No newline at end of file From f2787ce661ccb7cb1b985e49fc71fe62110ac909 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Mon, 16 Dec 2024 15:01:20 -0800 Subject: [PATCH 25/33] test --- PowerShell/ModuleChangelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerShell/ModuleChangelog.md b/PowerShell/ModuleChangelog.md index 1b434f6db..e9f07817a 100644 --- a/PowerShell/ModuleChangelog.md +++ b/PowerShell/ModuleChangelog.md @@ -1,6 +1,6 @@ ## 2.16.0 -Release Date: December 18, 2024 +Release Date: December 16, 2024 #### RELEASE NOTES From 08f13e8572bdfab7f40ee24dd0dfcb0dace1ec29 Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Mon, 16 Dec 2024 15:36:19 -0800 Subject: [PATCH 26/33] test doc --- PowerShell/JumpCloud Module/Docs/Remove-JCUser.md | 10 ---------- PowerShell/JumpCloud Module/JumpCloud.psd1 | 2 +- .../JumpCloud Module/Public/Users/Remove-JCUser.ps1 | 6 +++--- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md index 07f8abaa2..4f461f23d 100644 --- a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md +++ b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md @@ -22,16 +22,6 @@ Remove-JCUser [-Username] [-force] [] Remove-JCUser -UserID [-ByID] [-force] [] ``` -### CascadeManager -``` -Remove-JCUser [-Username] [-ByID] -CascadeManager [] -``` - -### CascadeManagerUser -``` -Remove-JCUser [-Username] [-ByID] -CascadeManager -CascadeManagerUser [] -``` - ## DESCRIPTION The Remove-JCUser function will remove a JumpCloud user from the JumpCloud organization. This will remove the deleted users access to any JumpCloud bound resources. diff --git a/PowerShell/JumpCloud Module/JumpCloud.psd1 b/PowerShell/JumpCloud Module/JumpCloud.psd1 index bc7ed6e8f..7d964f7c3 100644 --- a/PowerShell/JumpCloud Module/JumpCloud.psd1 +++ b/PowerShell/JumpCloud Module/JumpCloud.psd1 @@ -3,7 +3,7 @@ # # Generated by: JumpCloud Solutions Architect Team # -# Generated on: 11/18/2024 +# Generated on: 12/16/2024 # @{ diff --git a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 index 3dd562998..4e414266d 100644 --- a/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 +++ b/PowerShell/JumpCloud Module/Public/Users/Remove-JCUser.ps1 @@ -83,7 +83,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline $CascadeManagerId = $CascadeManagerValue Write-Debug "$CascadeManagerId is a valid JumpCloud User" } else { - Write-Error "User does not exist. Please enter a valid UserID." + Write-Error "UserId $($CascadeManagerValue) does not exist. Please enter a valid UserID." # Throw the script Throw } @@ -96,7 +96,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline Write-Debug "CascadeManagerId is a valid JumpCloud User $CascadeManagerId" } else { - Write-Error "User does not exist. Please enter a valid Username." + Write-Error "Username $($CascadeManagerValue) does not exist. Please enter a valid Username." # Throw the script Throw } @@ -116,7 +116,7 @@ UserID has an Alias of _id. This means you can leverage the PowerShell pipeline $Username = $UserHash.GetEnumerator().Where({ $_.Name -contains ($UserID) }) | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty username Write-Host "UserID $($UserId) is a valid JumpCloud User: $($Username)" } else { - Write-Error "User does not exist. Please enter a valid UserID." + Write-Error "UserId $($UserID) does not exist. Please enter a valid UserID." # Throw the script Throw } From ee94a006b9bd0484472a6cc81bc071ed229442e6 Mon Sep 17 00:00:00 2001 From: Ken Maranion <97972790+kmaranionjc@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:24:16 -0800 Subject: [PATCH 27/33] Update PowerShell/JumpCloud Module/Docs/Remove-JCUser.md Co-authored-by: Joe Workman <54448601+jworkmanjc@users.noreply.github.com> --- PowerShell/JumpCloud Module/Docs/Remove-JCUser.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md index 4f461f23d..ffc0a91b8 100644 --- a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md +++ b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md @@ -40,7 +40,8 @@ PS C:\> Remove-JCUser cclemons -Force ``` Removes the JumpCloud User with Username 'cclemons' using the -Force Parameter. A warning message will not be presented to confirm this operation. -If the user is a manager, this parameter will clear the manager field for the managed users by this manager. +If the cclemons is a manager of other users, the `Force` parameter will clear cclemons' subordinates `manager` field. In other words if a user is managed by cclemons, removing cclemons will also remove that user's manager field in JumpCloud. + ### Example 3 ```powershell From 65cfcf402184ef5c09cc999b494d6e644b0787c9 Mon Sep 17 00:00:00 2001 From: Ken Maranion <97972790+kmaranionjc@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:24:26 -0800 Subject: [PATCH 28/33] Update PowerShell/JumpCloud Module/Docs/Remove-JCUser.md Co-authored-by: Joe Workman <54448601+jworkmanjc@users.noreply.github.com> --- PowerShell/JumpCloud Module/Docs/Remove-JCUser.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md index ffc0a91b8..e14ef1395 100644 --- a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md +++ b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md @@ -48,7 +48,8 @@ If the cclemons is a manager of other users, the `Force` parameter will clear cc PS C:\> Remove-JCUser cclemons -CascadeManager null ``` -Removes the Jumpcloud user with Username 'cclemons'. If the user is a manager, the manager field for the managed users by this manager will be set to null. +Removes the Jumpcloud user with Username 'cclemons'. If `cclemons` manages other JumpCloud users, those user's will have their manager field set to null. Note. This command as the same effect as running `Remove-JCUser cclemons -Force` + ### Example 4 ```powershell From 6eae8ef3850beeb3470d1a0269164f296555a393 Mon Sep 17 00:00:00 2001 From: Ken Maranion <97972790+kmaranionjc@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:24:33 -0800 Subject: [PATCH 29/33] Update PowerShell/JumpCloud Module/Docs/Remove-JCUser.md Co-authored-by: Joe Workman <54448601+jworkmanjc@users.noreply.github.com> --- PowerShell/JumpCloud Module/Docs/Remove-JCUser.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md index e14ef1395..9d76fd9ba 100644 --- a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md +++ b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md @@ -60,7 +60,7 @@ Removes the JumpCloud user with the username 'cclemons'. If this user is a manag ### Example 5 ```powershell -PS C:\> Remove-JCUser cclemons -CascadeManager id -CascadeManager User -CascadeManagerUser username/id +PS C:\> Remove-JCUser cclemons -CascadeManager User -CascadeManagerUser some.manager ``` Removes the JumpCloud user with the username 'cclemons'. If this user is a manager, their managed users will be reassigned to the manager specified by the provided username/id with CascadeManagerUser. From 60f667ba524c2a0aed8bc976a2e1a77e7859d16a Mon Sep 17 00:00:00 2001 From: Ken Maranion <97972790+kmaranionjc@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:24:40 -0800 Subject: [PATCH 30/33] Update PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 Co-authored-by: Joe Workman <54448601+jworkmanjc@users.noreply.github.com> --- .../JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 index cbf6ee024..de7bc53b6 100755 --- a/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 +++ b/PowerShell/JumpCloud Module/Tests/Public/Users/Remove-JCUser.Tests.ps1 @@ -70,7 +70,7 @@ Describe -Tag:('JCUser') "Remove-JCUser 2.16.0" { Set-JCUser -UserID $NewUser._id -manager $ManagerUser._id # Remove the manager and set the new manager - $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL # Remove ManagerUser and should cascade to ManagerUser2 + $RemoveUser = Remove-JCUser -UserID $ManagerUser._id -CascadeManager NULL # The manager should be removed and the new manager should be set $RemoveUser.Results | Should -Be 'Deleted' From cac302d9299b1ec78d8b7470f3789dfd58a108c0 Mon Sep 17 00:00:00 2001 From: Ken Maranion <97972790+kmaranionjc@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:25:26 -0800 Subject: [PATCH 31/33] Update PowerShell/JumpCloud Module/Docs/Remove-JCUser.md Co-authored-by: Joe Workman <54448601+jworkmanjc@users.noreply.github.com> --- PowerShell/JumpCloud Module/Docs/Remove-JCUser.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md index 9d76fd9ba..aef09c9fb 100644 --- a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md +++ b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md @@ -63,7 +63,7 @@ Removes the JumpCloud user with the username 'cclemons'. If this user is a manag PS C:\> Remove-JCUser cclemons -CascadeManager User -CascadeManagerUser some.manager ``` -Removes the JumpCloud user with the username 'cclemons'. If this user is a manager, their managed users will be reassigned to the manager specified by the provided username/id with CascadeManagerUser. +Removes the JumpCloud user with the username `cclemons`. If `cclemons` is a manager, their subordinates will be reassigned to the manager specified by the provided username/id with CascadeManagerUser parameter. In this case, `cclemons` subordinates will be managed by the user with username: `some.manager` after `cclemons` is removed. ## PARAMETERS From c823b41091484b31486520e8d6bc88d0af26302f Mon Sep 17 00:00:00 2001 From: Ken Maranion <97972790+kmaranionjc@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:25:49 -0800 Subject: [PATCH 32/33] Update PowerShell/JumpCloud Module/Docs/Remove-JCUser.md Co-authored-by: Joe Workman <54448601+jworkmanjc@users.noreply.github.com> --- PowerShell/JumpCloud Module/Docs/Remove-JCUser.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md index aef09c9fb..910b7b830 100644 --- a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md +++ b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md @@ -56,7 +56,7 @@ Removes the Jumpcloud user with Username 'cclemons'. If `cclemons` manages other PS C:\> Remove-JCUser cclemons -CascadeManager automatic ``` -Removes the JumpCloud user with the username 'cclemons'. If this user is a manager (Manager1) and is also managed by another manager (Manager2), the users managed by Manager1 will be reassigned to Manager2 upon Manager1's removal. If Manager1 is not managed by anyone, the manager field for the users managed by Manager1 will be set to null. +Removes the JumpCloud user with the username 'cclemons' and automatically update's their subordinates manager field to `cclemons` manager. Ex. If `cclemons` is a manager and is also managed by another user with username: `some.manager`, the users managed by `cclemons` will be reassigned to `some.manager` upon `cclemons` removal. If `cclemons` is not managed by anyone, the manager field for the `cclemons` subordinates will be set to null. ### Example 5 ```powershell From 67501fa7879e82bf8b2564c43853b4a7d8baea9c Mon Sep 17 00:00:00 2001 From: Ken Maranion Date: Tue, 17 Dec 2024 12:36:15 -0800 Subject: [PATCH 33/33] doc --- .../JumpCloud Module/Docs/Remove-JCUser.md | 26 +++++-- PowerShell/JumpCloud Module/JumpCloud.psd1 | 2 +- .../JumpCloud Module/en-Us/JumpCloud-help.xml | 69 ++++++++++++++++++- PowerShell/ModuleChangelog.md | 2 +- 4 files changed, 91 insertions(+), 8 deletions(-) diff --git a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md index 910b7b830..baed772e5 100644 --- a/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md +++ b/PowerShell/JumpCloud Module/Docs/Remove-JCUser.md @@ -14,12 +14,14 @@ Removes a JumpCloud User ### Username (Default) ``` -Remove-JCUser [-Username] [-force] [] +Remove-JCUser [-Username] [-force] [-CascadeManager ] + [] ``` ### UserID ``` -Remove-JCUser -UserID [-ByID] [-force] [] +Remove-JCUser -UserID [-ByID] [-force] [-CascadeManager ] + [] ``` ## DESCRIPTION @@ -42,7 +44,6 @@ PS C:\> Remove-JCUser cclemons -Force Removes the JumpCloud User with Username 'cclemons' using the -Force Parameter. A warning message will not be presented to confirm this operation. If the cclemons is a manager of other users, the `Force` parameter will clear cclemons' subordinates `manager` field. In other words if a user is managed by cclemons, removing cclemons will also remove that user's manager field in JumpCloud. - ### Example 3 ```powershell PS C:\> Remove-JCUser cclemons -CascadeManager null @@ -50,7 +51,6 @@ PS C:\> Remove-JCUser cclemons -CascadeManager null Removes the Jumpcloud user with Username 'cclemons'. If `cclemons` manages other JumpCloud users, those user's will have their manager field set to null. Note. This command as the same effect as running `Remove-JCUser cclemons -Force` - ### Example 4 ```powershell PS C:\> Remove-JCUser cclemons -CascadeManager automatic @@ -83,6 +83,22 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -CascadeManager +A SwitchParameter for Cascading the manager of the user to the users managed by the user. NULL, AUTOMATIC (bubble up), ID (prompt for manager ID) + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: +Accepted values: NULL, Automatic, User + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -force A SwitchParameter which suppresses the warning message when removing a JumpCloud User. @@ -148,4 +164,4 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ### System.Object ## NOTES -## RELATED LINKS +## RELATED LINKS diff --git a/PowerShell/JumpCloud Module/JumpCloud.psd1 b/PowerShell/JumpCloud Module/JumpCloud.psd1 index 7d964f7c3..3512ddf04 100644 --- a/PowerShell/JumpCloud Module/JumpCloud.psd1 +++ b/PowerShell/JumpCloud Module/JumpCloud.psd1 @@ -3,7 +3,7 @@ # # Generated by: JumpCloud Solutions Architect Team # -# Generated on: 12/16/2024 +# Generated on: 12/17/2024 # @{ diff --git a/PowerShell/JumpCloud Module/en-Us/JumpCloud-help.xml b/PowerShell/JumpCloud Module/en-Us/JumpCloud-help.xml index 2f7fe7590..35afa9223 100644 --- a/PowerShell/JumpCloud Module/en-Us/JumpCloud-help.xml +++ b/PowerShell/JumpCloud Module/en-Us/JumpCloud-help.xml @@ -17231,6 +17231,23 @@ PS C:\> New-JCPolicy -TemplateName darwin_Login_Window_Text -Values $policyV False + + CascadeManager + + A SwitchParameter for Cascading the manager of the user to the users managed by the user. NULL, AUTOMATIC (bubble up), ID (prompt for manager ID) + + + NULL + Automatic + User + + System.String + + System.String + + + None + force @@ -17274,6 +17291,23 @@ PS C:\> New-JCPolicy -TemplateName darwin_Login_Window_Text -Values $policyV None + + CascadeManager + + A SwitchParameter for Cascading the manager of the user to the users managed by the user. NULL, AUTOMATIC (bubble up), ID (prompt for manager ID) + + + NULL + Automatic + User + + System.String + + System.String + + + None + force @@ -17301,6 +17335,18 @@ PS C:\> New-JCPolicy -TemplateName darwin_Login_Window_Text -Values $policyV False + + CascadeManager + + A SwitchParameter for Cascading the manager of the user to the users managed by the user. NULL, AUTOMATIC (bubble up), ID (prompt for manager ID) + + System.String + + System.String + + + None + force @@ -17380,7 +17426,28 @@ PS C:\> New-JCPolicy -TemplateName darwin_Login_Window_Text -Values $policyV -------------------------- Example 2 -------------------------- PS C:\> Remove-JCUser cclemons -Force - Removes the JumpCloud User with Username 'cclemons' using the -Force Parameter. A warning message will not be presented to confirm this operation. + Removes the JumpCloud User with Username 'cclemons' using the -Force Parameter. A warning message will not be presented to confirm this operation. If the cclemons is a manager of other users, the `Force` parameter will clear cclemons' subordinates `manager` field. In other words if a user is managed by cclemons, removing cclemons will also remove that user's manager field in JumpCloud. + + + + -------------------------- Example 3 -------------------------- + PS C:\> Remove-JCUser cclemons -CascadeManager null + + Removes the Jumpcloud user with Username 'cclemons'. If `cclemons` manages other JumpCloud users, those user's will have their manager field set to null. Note. This command as the same effect as running `Remove-JCUser cclemons -Force` + + + + -------------------------- Example 4 -------------------------- + PS C:\> Remove-JCUser cclemons -CascadeManager automatic + + Removes the JumpCloud user with the username 'cclemons' and automatically update's their subordinates manager field to `cclemons` manager. Ex. If `cclemons` is a manager and is also managed by another user with username: `some.manager`, the users managed by `cclemons` will be reassigned to `some.manager` upon `cclemons` removal. If `cclemons` is not managed by anyone, the manager field for the `cclemons` subordinates will be set to null. + + + + -------------------------- Example 5 -------------------------- + PS C:\> Remove-JCUser cclemons -CascadeManager User -CascadeManagerUser some.manager + + Removes the JumpCloud user with the username `cclemons`. If `cclemons` is a manager, their subordinates will be reassigned to the manager specified by the provided username/id with CascadeManagerUser parameter. In this case, `cclemons` subordinates will be managed by the user with username: `some.manager` after `cclemons` is removed. diff --git a/PowerShell/ModuleChangelog.md b/PowerShell/ModuleChangelog.md index e9f07817a..98aa059df 100644 --- a/PowerShell/ModuleChangelog.md +++ b/PowerShell/ModuleChangelog.md @@ -1,6 +1,6 @@ ## 2.16.0 -Release Date: December 16, 2024 +Release Date: December 17, 2024 #### RELEASE NOTES