none
Errors with Simple Powershell AD User script not working. Status, Unlock, Set password RRS feed

  • Question

  • Getting error when making the selection. I can manually set the variable of $username and run the commands and they work but not on here. Any command I try it does this. 

    Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, 
    and then try running the command again.
    At C:\Users\edgar2\Documents\PS2EXE-GUI\options example.ps1:23 char:38
    +                 Get-ADUser -Identity $username -Properties LockedOut  ...
    +                                      ~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser


    function Show-Menu
    {
         param (
               [string]$Title = 'Menu'
         )
         cls
         Write-Host "================ $Title ================"
         $username = Read-Host "Enter Username" 
         Write-Host "1: User Account Status"
         Write-Host "2: Unlock Account"
         Write-Host "3: Reset password"
         Write-Host "Q: Press 'Q' to quit."
    }
    
    do
    {
         Show-Menu
         $input = Read-Host "Please make a selection"
         switch ($input)
         {
               '1' {
                    cls
                    Get-ADUser -Identity $username -Properties LockedOut | Select-Object LockedOut
                    'User Account is ' #missing if statement to show Status. false=unlocked true=locked.
               } '2' {
                    cls
                    Enable-ADAccount -Identity $username
                    'Account is now unlocked'
               } '3' {
                    cls
                    Set-ADAccountPassword -Identity $username -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$!Password1" -Force)
                    Set-ADUser -Identity username -ChangePasswordAtLogon $true
                    'Password has been set to $!Password1'
               } 'q' {
                    return
               }
         }
         pause
    }
    until ($input -eq 'q')


    Tuesday, February 12, 2019 9:38 PM

Answers

  • This would be a more usable and safer method:

    function Show-Menu{ param ( [string]$Title = 'Menu' ) while($true) { Clear-Host Write-Host "================ $Title ================" if($username = Read-Host "Enter Username"){ # username entered }else{ # no name entered continue } Write-Host "1: User Account Status" Write-Host "2: Unlock Account" Write-Host "3: Reset password" Write-Host "Q: Press 'Q' to quit." $response = Read-Host "Please make a selection" switch ($response) { '1' { Get-ADUser -Identity $username -Properties LockedOut | Select-Object LockedOut 'User Account is ' #missing if statement to show Status. false=unlocked true=locked. } '2' { Enable-ADAccount -Identity $username 'Account is now unlocked' } '3' { Set-ADAccountPassword -Identity $username -Reset -NewPassword (ConvertTo-SecureString -AsPlainText '$!Password1' -Force) Set-ADUser -Identity username -ChangePasswordAtLogon $true 'Password has been set to $!Password1' } 'q' { return }
    default { continue} } pause } } Show-Menu



    \_(ツ)_/


    Tuesday, February 12, 2019 10:52 PM

All replies

  • Getting error when making the selection. I can manually set the variable of $username and run the commands and they work but not on here. Any command I try it does this. 

    Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, 
    and then try running the command again.
    At C:\Users\edgar2\Documents\PS2EXE-GUI\options example.ps1:23 char:38
    +                 Get-ADUser -Identity $username -Properties LockedOut  ...
    +                                      ~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    function Show-Menu
    {
         param (
               [string]$Title = 'Menu'
         )
         cls
         Write-Host "================ $Title ================"
         $username = Read-Host "Enter Username" 
         Write-Host "1: User Account Status"
         Write-Host "2: Unlock Account"
         Write-Host "3: Reset password"
         Write-Host "Q: Press 'Q' to quit."
    }
    
    do
    {
         Show-Menu
         $input = Read-Host "Please make a selection"
         switch ($input)
         {
               '1' {
                    cls
                    Get-ADUser -Identity $username -Properties LockedOut | Select-Object LockedOut
                    'User Account is ' #missing if statement to show Status. false=unlocked true=locked.
               } '2' {
                    cls
                    Enable-ADAccount -Identity $username
                    'Account is now unlocked'
               } '3' {
                    cls
                    Set-ADAccountPassword -Identity $username -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$!Password1" -Force)
                    Set-ADUser -Identity username -ChangePasswordAtLogon $true
                    'Password has been set to $!Password1'
               } 'q' {
                    return
               }
         }
         pause
    }
    until ($input -eq 'q')

    • Moved by Dave PatrickMVP Tuesday, February 12, 2019 9:38 PM PowerShell
    • Merged by jrv Tuesday, February 12, 2019 10:34 PM DUPLICATE
    Tuesday, February 12, 2019 9:33 PM
  • What if you try $username = $username.ToString() after collection the variable value.

    This posting is provided AS IS with no warranties or guarantees , and confers no rights.

    Ahmed MALEK

    My Website Link

    My Linkedin Profile

    My MVP Profile

    Tuesday, February 12, 2019 9:54 PM
  • Getting error when making the selection. I can manually set the variable of $username and run the commands and they work but not on here. Any command I try it does this. 

    Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, 
    and then try running the command again.
    At C:\Users\edgar2\Documents\PS2EXE-GUI\options example.ps1:23 char:38


    The error is quite explicit. The value is "null". This cannot be fixed by adding "ToString" or any other way. Change the function to pass in username.

    Show-Menu{
        param (
            [string]$Title = 'Menu',
            [Parameter(Mandatory = $true)]
            [ValidateNotNullOrEmpty]
            $username
        )
        
    


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Tuesday, February 12, 2019 10:31 PM
    • Proposed as answer by jrv Tuesday, February 12, 2019 10:38 PM
    Tuesday, February 12, 2019 10:28 PM
  • Please do not post the same question in multiple forums.  It will not get you a faster or better answer.


    \_(ツ)_/

    Tuesday, February 12, 2019 10:30 PM
  • i think you have a variable scope issue.. because you run your function in your function.. maybe first ask for the username and then show the menu


    Please remember to mark the replies as answers if they helped.

    Tuesday, February 12, 2019 10:38 PM
  • i think you have a variable scope issue.. because you run your function in your function.. maybe first ask for the username and then show the menu


    Please remember to mark the replies as answers if they helped.

    Two bad guesses.  Please read the whole error.  "Scope" is a guess but there is no $username variable in any scope.  By passing the variable and making it mandatory this cannot happen even if it is just a forgotten or misspelled variable.

    The reason for these decorations on functions is to prevent this kind of failure.  A function should almost never rely on variables from an outer scope.  It is a well known source of difficult bugs and failures.  A function should be totally independent of the context it is called from.


    \_(ツ)_/

    Tuesday, February 12, 2019 10:43 PM
  • This would be a more usable and safer method:

    function Show-Menu{ param ( [string]$Title = 'Menu' ) while($true) { Clear-Host Write-Host "================ $Title ================" if($username = Read-Host "Enter Username"){ # username entered }else{ # no name entered continue } Write-Host "1: User Account Status" Write-Host "2: Unlock Account" Write-Host "3: Reset password" Write-Host "Q: Press 'Q' to quit." $response = Read-Host "Please make a selection" switch ($response) { '1' { Get-ADUser -Identity $username -Properties LockedOut | Select-Object LockedOut 'User Account is ' #missing if statement to show Status. false=unlocked true=locked. } '2' { Enable-ADAccount -Identity $username 'Account is now unlocked' } '3' { Set-ADAccountPassword -Identity $username -Reset -NewPassword (ConvertTo-SecureString -AsPlainText '$!Password1' -Force) Set-ADUser -Identity username -ChangePasswordAtLogon $true 'Password has been set to $!Password1' } 'q' { return }
    default { continue} } pause } } Show-Menu



    \_(ツ)_/


    Tuesday, February 12, 2019 10:52 PM