HiveBrain v1.2.0
Get Started
← Back to all entries
patternshellMinor

Terminating an Active Directory user

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
terminatingdirectoryactiveuser

Problem

I'm writing an AD Termination script for work. I'm new to PowerShell and was wondering if multiple commands within PowerShell switch statement is okay to do:

# Switch statement to start here
$switchTitle = "Terminate User"
$confirmMessage = "Are you sure that " + $fullName + " is the user that you want to terminate?"

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",
"Starts the termination process for the selected user"

$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",
"Cancels the termination process and, eventually, will prompt for another user selection"

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

$result = $host.ui.PromptForChoice($switchTitle, $confirmMessage, $options, 0)

switch ($result)
   {
      0 {"You have selected Yes. The Termination Script will now start."
           Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\Path\To\UserFolder" ` 
           -Destination "Microsoft.PowerShell.Core\FileSystem::\\path\To\TermFolder" -Recurse -Force
        # TODO: Put delete command here for old profile
        # TODO: Put Disable AD Object command here
        }
      1 {"You have selected No. Please choose another user."}
   }


I'm also concerned with PowerShell styling/conventions so if I'm doing anything wrong, please let me know.

Solution

Prompting for confirmation

The way you're doing the prompt is overly complicated. Consider this:

[CmdletBinding(SupportsShouldProcess=$true)]
param()

$switchTitle = "Terminate User"
$confirmMessage = "Are you sure that " + $fullName + " is the user that you want to terminate?"

if ($PSCmdlet.ShouldContinue($confirmMessage, $switchTitle)) {
    # She said yes!
} else {
    # nope
}


I sometimes wrap this up into a small function that allows for quicker usage if it's always for a single purpose within a script and I need it a lot:

function Get-Confirmation {
[CmdletBinding(SupportsShouldProcess=$true)]
param(
    [Parameter(Mandatory=$true)]
    [String]
    $User
)

    $confirmMessage = 'Are you sure that {0} is the user that you want to terminate?' -f $User

    $PSCmdlet.ShouldContinue($confirmMessage, 'Terminate User?')
}


Then just use it like so:

# code that populates $fullName
if (Get-Confirmation -User $fullName) {
    # terminate
} else {
    # don't terminate
}


Referencing files

Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\Path\To\UserFolder" ` 
-Destination "Microsoft.PowerShell.Core\FileSystem::\\path\To\TermFolder" -Recurse -Force


You don't have to use the full provider name to reference a file, even for a UNC path.

It would be fine to just do this:

Copy-Item -Path "\\Path\To\UserFolder" ` 
-Destination "\\path\To\TermFolder" -Recurse -Force

Code Snippets

[CmdletBinding(SupportsShouldProcess=$true)]
param()

$switchTitle = "Terminate User"
$confirmMessage = "Are you sure that " + $fullName + " is the user that you want to terminate?"

if ($PSCmdlet.ShouldContinue($confirmMessage, $switchTitle)) {
    # She said yes!
} else {
    # nope
}
function Get-Confirmation {
[CmdletBinding(SupportsShouldProcess=$true)]
param(
    [Parameter(Mandatory=$true)]
    [String]
    $User
)

    $confirmMessage = 'Are you sure that {0} is the user that you want to terminate?' -f $User

    $PSCmdlet.ShouldContinue($confirmMessage, 'Terminate User?')
}
# code that populates $fullName
if (Get-Confirmation -User $fullName) {
    # terminate
} else {
    # don't terminate
}
Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\Path\To\UserFolder" ` 
-Destination "Microsoft.PowerShell.Core\FileSystem::\\path\To\TermFolder" -Recurse -Force
Copy-Item -Path "\\Path\To\UserFolder" ` 
-Destination "\\path\To\TermFolder" -Recurse -Force

Context

StackExchange Code Review Q#104448, answer score: 4

Revisions (0)

No revisions yet.