EnableDisableUser1.ps1 Script posted on the web has an error RRS feed

  • General discussion

  • Hello,

    I'm trying to use this scripted to disable a local user account on multiple systems. When I run it, I'm getting the following error - 

    work : The term 'work' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

    At c:\temp\filename.ps1:124 char:69.

    Here is a copy of the script I copied. Any help would be greatly appreciated. I have around 200 machines I need to do this to.

    param($computer="localhost", $a, $user, $password, $help, $i, $c, $f, $work, $in) 
    function funHelp() 

    NAME: EnableDisableUser.ps1 
    Enables or Disables a local user on either a local or remote machine. 
    -computer Specifies the name of the computer upon which to run the script 
    -a(ction) Action to perform < e(nable) d(isable) > 
    -user     Name of user to create 
    -help     prints help file 
    Generates an error. You must supply a user name 
    EnableDisableUser.ps1 -computer MunichServer -user myUser 
    -password Passw0rd^&! -a e 
    Enables a local user called myUser on a computer named MunichServer 
    with a password of Passw0rd^&! 
    EnableDisableUser.ps1 -user myUser -a d 
    Disables a local user called myUser on the local machine 
    EnableDisableUser.ps1 -help ? 
    Displays the help topic for the script 

    $a = Read-Host "Enter (e) to enable an account or (d) to disable an account." 
    if(!(($a -eq "e") -or ($a -eq "d"))) 
           $(Throw 'Input value must be (e) for enable or (d) for disable.') 
    $user = Read-Host "Enter the user name you want to work on." 
    if($a -eq "e") 
                    $password = Read-Host "Enter new password for user account that you are enabling." 
                        $(Throw 'a value for $password is required. 
                         Try this: EnableDisableUser.ps1 -help ?') 
    $in = Read-Host "Enter (s) if you want to run this script against a single computer or enter (m) against a list of multiple computers using a txt file" 
    if(!(($in -eq "s") -or ($in -eq "m"))) 
           $(Throw 'Input value must be (s) for single computer or (m) for multiple computers.') 

     "s" { 
            $c = Read-Host "Enter the computer name you want to work on." 
     "m" { 
            $f = Read-Host "Enter the path to the list of computers. i.e. C:\input.txt" 
            $FileExists = Test-Path $f  
            If ($FileExists -eq $True)  
                            $i = Get-Content $f 
                            foreach ($c in $i) 
                            {$c + "`n" + "=========================="; work([string]$c)} 
                            $(Write-Error 'Path to input file is not correct  
                              or is not accessable with the current user.') 

    if($help){ "Obtaining help ..." ; funhelp } 
    function work()  

    $EnableUser = 512 
    $DisableUser = 2 
    if(Test-Connection -ComputerName $c -Quiet) 
           $(Throw 'A value for $user is required. 
           Try this: EnableDisableUser.ps1 -help ?') 
    $ObjUser = [ADSI]"WinNT://$c/$user" 

     "e" { 
          $objUser.description = "Enabled Account" 
          $objUser.userflags = $EnableUser 
     "d" { 
          $objUser.description = "Disabled Account" 
          $objUser.userflags = $DisableUser 
                 "You must supply a value for the action. 
                 Try this: EnableDisableUser.ps1 -help ?" 

           $(Write-Error 'Can not contact computer.  
           It is either currently offline or not reachable through the network.') 

    • Changed type Bill_Stewart Monday, October 22, 2018 2:12 PM
    • Moved by Bill_Stewart Monday, October 22, 2018 2:13 PM This is not "fix/debug/rewrite this script I found on the Internet" forum
    Wednesday, September 12, 2018 3:07 PM

All replies

  • Unfortunately we don't have the resources to debug/fix long scripts.

    You will need to break your script down into little pieces and debug carefully.

    If you are not familiar with PowerShell or programming, now is a great time to start learning.

    If this is critical to your business, we would recommend hiring a consultant.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 12, 2018 3:10 PM