AD Term Script - Ask To Verify User

Welcome Forums General PowerShell Q&A AD Term Script - Ask To Verify User

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 week, 4 days ago.

  • Author
    Posts
  • #113873
    RJP

    Participant
    Points: 0
    Rank: Member

    Beginner here: Using an existing script created by a previous employee. I want to make one small change and its the 3rd line of script. Today I enter the account name to disable and it runs with it. I'd like to add a step to verify that the account name is correct before moving on.

    I've added the 3rd line but the issue is when selecting Y or N they both end with "User does not exist..." What do I need to adjust for when selecting Y, it continues on with account term?
    $DisabledUser = Read-Host "Enter Account Name to Disable"
    $user = $DisabledUser
    $DisabledUser = Read-Host "You entered $DisabledUser is this correct? Y or N
    $a = if(dsquery user -samid $user){"Exists"}else{"Does not exist"}
    if ($a -eq "Does not exist") {
    "User does not exist. Paste comments into Employee Lifecycle Form."
    "Stopped.Thank you."
    break
    }

  • #113903

    Participant
    Points: 37
    PublishedHelping Hand
    Rank: Member

    If you are new to PS, it is vital that you ramp up on it before you unnecessarily frustrate yourself, add additional confusion, or do something very destructive that you cannot recover from and could lead to business operation failure, and RPE for you. RPE = Resume Producing Event.

    Especially when messing with something as critical as ADDS.

    Please see all the no cost video training on MS Virtual Academy, TechNet Virtual labs and YouTube, as well as all the no cost eBooks on this site and on TechNet.

    As for this..

    What do I need to adjust for when selecting Y, it continues on with account term?

    If you set logic to get a response from a user, Y/N or T/F, you need to have logic to deal with each response. If Y/T, then do X, if N/F the do Y. You can use the built-in PS snippets in the PS ISE to show you how to write this type of code. Just hit CRTL+J while in the ISE to see the snippets available for you to tweak as needed.

    # Example If only snippet
    
    if ($x -gt $y)
    {
        
    }
    
    # Example If..Else snippet from the ISE
    
    if ($x -lt $y)
    {
        
    }
    else
    {
        
    }
    
    # Or the try catch snippet
    
    try
    {
        1/0
    }
    catch [DivideByZeroException]
    {
        Write-Host "Divide by zero exception"
    }
    catch [System.Net.WebException],[System.Exception]
    {
        Write-Host "Other exception"
    }
    finally
    {
        Write-Host "cleaning up ..."
    }
    

    So, wha tyou show is inline with the above, but if you are unsure, you need to use the -WhatIf switch to check that stuff is OK, or create a dummy account to work with to be sure.

    Understand that the Windows Server ADAC (Active Directory Administration Center) will write baseline PS code for you to use or tweak for later use as needed.

You must be logged in to reply to this topic.