# ============================================================================= # # NAME:CreateLocalUser.ps1 # # AUTHOR: Ed Wilson , microsoft # DATE : 12/7/2007 # # COMMENT: # Uses Params to allow modification of script at runtime # Uses funHelp function to display help # Uses funLine function to underline output # Uses [adsi] type accelerator to use ADSI to create a local group # Uses throw to generate error if $group is not present # Uses the WinNT provider to connect to local SAM database. WinNT is case sensitive # This script must run with ADMIN rights # # ============================================================================= param( $computer="localhost", $user, $password, $description="scripted user", $text, [switch]$whatif, [switch]$help, [switch]$examples, [switch]$min, [switch]$full ) #end param # Begin Functions function funHelp() { $descriptionText= ` @" NAME: CreateLocalUser.ps1 DESCRIPTION: Creates a local user on a local or remote machine. This script allows you to enter from both the command line and from a csv text file. It supports prototyping of the command via the -whatif parameter. Using this script you can set the user name, password, and description. The user is an enabled user upon completion of the script. It does not, however, allow you to bypass security restrictions governing password policy. The password must meet the requisite complexity requirements. PARAMETERS: -computer computer upon which to run the command -user Name of the user to create. Required -password Password for the new user. Required -description of user account -text reads user, password, and description from a csv file with these exact headings. The format is: user, password, description testuser1, P@ssw0rd11, "from script" When the -text parameter is used, the -user, -password and -description parameters are ignored -whatif Prototypes the command. Works with both command line input and text input -help prints help description and parameters file -examples prints only help examples of syntax -full prints complete help information -min prints minimal help. Modifies -help "@ #end descriptionText $examplesText= ` @" SYNTAX: CreateLocalUser.ps1 Displays an error missing parameter, and calls help CreateLocalUser.ps1 -computer MunichServer -user myUser -password Passw0rd^&! Creates a local user called myUser on a computer named MunichServer with a password of Passw0rd^&! CreateLocalUser.ps1 -user myUser -password Passw0rd^&! Creates a local user called myUser on local computer with a password of Passw0rd^&! CreateLocalUser.ps1 -user myuser -password Password -whatif Displays what if: Perform operation create user myuser with password Password on computer localhost CreateLocalUser.ps1 -help Prints the help topic for the script CreateLocalUser.ps1 -help -full Prints full help topic for the script CreateLocalUser.ps1 -help -examples Prints only the examples for the script CreateLocalUser.ps1 -examples Prints only the examples for the script "@ #end examplesText $remarks = ` " REMARKS For more information, type: $($MyInvocation.ScriptName) -help -full " #end remarks if($examples) { $examplesText ; $remarks ; exit } if($full) { $descriptionText; $examplesText ; exit } if($min) { $descriptionText ; exit } $descriptionText; $remarks exit } #end funHelp function function funline ( $strIN, $char = "=", $sColor = "Yellow", $uColor = "darkYellow", [switch]$help ) { if($help) { $local:helpText = ` @" Funline accepts inputs: -strIN for input string and -char for seperator -sColor for the string color, and -uColor for the underline color. Only the -strIn is required. The others have the following default values: -char: =, -sColor: Yellow, -uColor: darkYellow Example: funline -strIN "Hello world" funline -strIn "Morgen welt" -char "-" -sColor "blue" -uColor "yellow" funline -help "@ $local:helpText break } #end funline help $strLine= $char * $strIn.length Write-Host -ForegroundColor $sColor $strIN Write-Host -ForegroundColor $uColor $strLine } #end funLine function Function funWhatIf() { if($text) { if(test-path $text) { $user=import-csv -path $text foreach($strUser in $user) { $user = $struser.user $password = $struser.password $description = $struser.description "what if: Perform operation create user $user with password $password and description of $description on computer $computer" } #end foreach exit } #end if test-path } #end if $text ELSE { "what if: Perform operation create user $user with password $password and description of $description on computer $computer" } exit } #end funWhatIf Function funCreateLocalUser() { $erroractionpreference = "SilentlyContinue" $error.clear() Clear-Host $OBJou = [ADSI]"WinNT://$computer" $objUser = $objOU.Create("User", $user) $objUser.setpassword($password) $objUser.SetInfo() $objUser.description = $description $objUser.SetInfo() funError } #end funCreateLocalUser Function funText() { if(Test-Path $text) { $user = import-csv -path $text foreach($strUser in $user) { $erroractionpreference = "SilentlyContinue" $error.clear() $user = $struser.user $password = $struser.password $description = $struser.description Clear-Host $OBJou = [ADSI]"WinNT://$computer" $objUser = $objOU.Create("User", $user) $objUser.setpassword($password) $objUser.SetInfo() $objUser.description = $description $objUser.SetInfo() funError } #end foreach $user exit } #end if test-path ELSE { write-warning "Unable to locate $text" exit } #end else } #end funText Function funError() { if($error.count -ne 0) { FunLine("$($error.count) errors occurred on the operation.") For($i = 0 ; $i -le $error.count -1; $i++) { FunLine("Error $i details follow:") $error[$i].categoryInfo $error[$i].invocationinfo $error[$i].exception } #end for $error.clear() } #end if Else { "There are no errors" } } #end funError # Entry Point if($help) { funhelp } if($examples) { funhelp } if($full) { funhelp } if($whatif) { funWhatIf } if($text) { funText } if(!$user -or !$password) { "Missing parameter" ; funhelp} funCreateLocalUser
kmichalo1