Private/Start-UserManipulation.ps1

function Start-UserManipulation {

    Param([string]$SearchBase)

    $CurrentFunction = Get-FunctionName
    Write-Log -Message "### Start Function $CurrentFunction ###"
    $StartRunTime = (Get-Date).ToString($Script:DateFormatLog)
    #################### main code | out- host #####################
    
    $attributes = @("sAMAccountName", "Enabled", "Modified", "PasswordLastset", "userPrincipalName", "name")
    Get-ADUser -Filter * -SearchBase $Searchbase  -Properties $attributes | Sort-Object { Get-Random } | Select-Object $attributes |  Select-Object -First 15 | Format-Table | Out-Host

    $count = (Get-ADUser -filter * -SearchBase $SearchBase).count
    $NoUsers = Get-KeyValue -key "LastNumofDemoUsers"
        
    Write-Host "`nFound "-NoNewline
    Write-Host $count -NoNewline -ForegroundColor Yellow
    Write-Host " AD (Demo) Users and 15 random Users listed above!"
    Write-Host "How many Users do you like to manipulate? By the last time, it was: " -NoNewline
    Write-Host $NoUsers -ForegroundColor Yellow
        
    $result = Select-ADObject
    $result.DistinguishedName 
    #Choose-ADOrganizationalUnit -Domain "WS19-CHILD01.WS19-ROOT.CORP"

    Do {
        
        $NoUsers = Get-KeyValue -key "LastNumofDemoUsers"

        If ($UnAttended) {
            $prompt = $yes
        }
        else {
            $question = "Is the number still $NoUsers ok? - Y or N? Default "
            $prompt = Get-Answer -question $question -defaultValue $yes
        }

        if ($prompt -ne $yes) {
            $NoUsers = Read-Host "Enter a new number: "
            Set-KeyValue -key "LastNumofDemoUsers" -NewValue $NoUsers
        }
   
    } Until ($prompt -eq $yes)
    
    
    # If ($UnAttended) { Start-Sleep 2 } else { Pause }
    
      
    
    
    If (-not $SkipClearHost) { Clear-Host }

    Invoke-output -T Header -M "Try to disable all (DEMO) users "
    Write-Host      -NoNewline " Command: "
    Write-Highlight -Text "get-aduser ", "-filter * ", "-SearchBase ", """$MySearchBase""", "| Disable-ADAccount"  `
        -Color $fgcC, $fgcS, $fgcS, $fgcV, $fgcC
    Write-Host ""


    $count = Get-KeyValue -key "LastNumofDemoUsers"

    If ($UnAttended) {
        $answer = $yes      
    }
    else {
        $question = "Do you want to disable $count users - Y or N? Default "
        $answer = Get-Answer -question $question -defaultValue $Yes
    }

    If ($answer -eq $yes) {
        $MyDomain = $env:USERDNSDOMAIN
        $identifier = Disable-AS2GoUser -Domain $MyDomain -SearchBase $Script:ASSearchBase-NoU $count
        If ($UnAttended) { Start-Sleep 2 } else { Pause }
    }

    If (-not $SkipClearHost) { Clear-Host }


    Invoke-output -T Header -M "Try to reset all users password"


    
    $newRandomPW = Get-RandomPassword
    
    Write-Host      -NoNewline " Command: "
    Write-Highlight -Text "Get-aduser | Set-ADAccountPassword", " -Reset -NewPassword ", "$newRandomPW"  `
        -Color $fgcC, $fgcS, $fgcV
    Write-Host ""

    If ($UnAttended) {
        $answer = $yes  
    }
    else {
        $question = "Do you also want to reset the user's password with the random password '$newRandomPW' - Y or N? Default "
        $answer = Get-Answer -question $question -defaultValue $Yes
    }

    If ($answer -eq $yes) {
        $MyDomain = $env:USERDNSDOMAIN
        Reset-AS2GoPassword -Domain $MyDomain -NewPassword $newRandomPW -SearchBase $Script:ASSearchBase-NoR 4 -Identifier $identifier 
    }

    #CleanUp
    Get-aduser -filter * -SearchBase $Script:ASSearchBase | Enable-ADAccount

    Write-Log -Message " >> using "
    ######################## main code ############################
    $runtime = Get-RunTime -StartRunTime $StartRunTime
    Write-Log -Message " Run Time: $runtime [h] ###"
    Write-Log -Message "### End Function $CurrentFunction ###"
}