PoshCode Logo PowerShell Code Repository

IP Scan/Local User admin by pezhore 23 months ago
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1684"></script>download | new post

Where I work, we don’t use AD for roughly 30-60 servers. There are multiple identical local windows accounts on various servers, and when a person leaves the company, those accounts need to be deleted by hand. This group of scripts performs the following tasks:
1) Ping scans a range of IPs for responding hosts.
2) Takes those hosts and attempts to find the specified user
3) Optionally with the -delete flag, deletes the user

There’s three parts to this script. The trigger which is run from the console in the same directory as the finduser.ps1 and set-localaccount.ps1 files. Note that set-localaccount.ps1 is from powershell.nu, with some minor changes that allow the computername to passed as a parameter.

  1. # The trigger
  2. $obj = New-Object system.Net.NetworkInformation.Ping
  3. 100..200 | % { $ip = "10.1.1.$_"
  4. $ping = $obj.send($ip,100)
  5. write-host "." -noNewLine
  6. if ($ping.status -eq "Success"){
  7.    C:\Powershell\Finduser.ps1 $ping.address.ipaddresstostring
  8. }}
  9.  
  10. #--------------------------------
  11.  
  12. # Finduser.ps1
  13. param (
  14.         [string]$strcomputer,
  15.         [switch]$delete)
  16.  
  17.   $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
  18.  
  19.   $Users = $computer.psbase.children |where{$_.psbase.schemaclassname -eq "User"}
  20.   foreach ($member in $Users.psbase.syncroot){
  21.     if ($member.name -eq "username"){
  22.       write-host Found user! $computer.name
  23.       if ($delete){
  24.         write-host Deleting...
  25.         .\set-localaccount.ps1 -UserName username -remove -computerName $computer.name
  26.       }
  27.     }
  28.   }
  29.  
  30. #--------------------------------
  31.  
  32. # set-localaccount.ps1
  33. ##################################################################################
  34. #
  35. #  Script name: Set-LocalAccount.ps1
  36. #  Author:      niklas.goude@zipper.se
  37. #  Homepage:    www.powershell.nu
  38. #
  39. ##################################################################################
  40.  
  41. param([string]$UserName, [string]$Password, [switch]$Add, [switch]$Remove, [switch]$ResetPassword, [switch]$help, [string]$computername)
  42.  
  43. function GetHelp() {
  44. $HelpText = @"
  45. DESCRIPTION:
  46.  
  47. NAME: Set-LocalAccount.ps1
  48. Adds or Removes a Local Account
  49.  
  50. PARAMETERS:
  51.  
  52. -UserName        Name of the User to Add or Remove (Required)
  53. -Password        Sets Users Password (optional)
  54. -Add             Adds Local User (Optional)
  55. -Remove          Removes Local User (Optional)
  56. -ResetPassword   Resets Local User Password (Optional)
  57. -help            Prints the HelpFile (Optional)
  58.  
  59. SYNTAX:
  60.  
  61. .\Set-LocalAccount.ps1 -UserName nika -Password Password1 -Add
  62. Adds Local User nika and sets Password to Password1
  63.  
  64. .\Set-LocalAccount.ps1 -UserName nika -Remove
  65. Removes Local User nika
  66.  
  67. .\Set-LocalAccount.ps1 -UserName nika -Password Password1 -ResetPassword
  68. Sets Local User nika's Password to Password1
  69.  
  70. .\Set-LocalAdmin.ps1 -help
  71. Displays the helptext
  72. "@
  73. $HelpText
  74. }
  75.  
  76. function AddRemove-LocalAccount ([string]$UserName, [string]$Password, [switch]$Add, [switch]$Remove, [switch]$ResetPassword, [string]$computerName) {
  77.     if($Add) {
  78.         [string]$ConnectionString = "WinNT://$computerName"
  79.         $ADSI = [adsi]$ConnectionString
  80.         $User = $ADSI.Create("user",$UserName)
  81.         $User.SetPassword($Password)
  82.         $User.SetInfo()
  83.     }
  84.  
  85.     if($Remove) {
  86.         [string]$ConnectionString = "WinNT://$computerName"
  87.         $ADSI = [adsi]$ConnectionString
  88.         $ADSI.Delete("user",$UserName)
  89.     }
  90.  
  91.     if($ResetPassword) {
  92.         [string]$ConnectionString = "WinNT://" + $ComputerName + "/" + $UserName + ",user"
  93.         $Account = [adsi]$ConnectionString
  94.         $Account.psbase.invoke("SetPassword", $Password)
  95.     }
  96. }
  97.  
  98. if($help) { GetHelp; Continue }
  99. if($UserName -AND $Password -AND $Add -AND !$ResetPassword) { AddRemove-LocalAccount -UserName $UserName -Password $Password -Add -computerName $computerName}
  100. if($UserName -AND $Password -AND $ResetPassword) { AddRemove-LocalAccount -UserName $UserName -Password $Password -ResetPassword -computerName $computerName}
  101. if($UserName -AND $Remove) { AddRemove-LocalAccount -UserName $UserName -Remove -computerName $computerName}

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me