Advertisement
plezzz

Create New User And Folder

Apr 9th, 2021
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $CreateNewUser = $false;
  2.  
  3. function anotherUser () {
  4.   Write-Host "Do you want to create another User:"
  5.   $Answer = Read-Host "Type Y for yes or N for No"
  6.  
  7.   if ($Answer -eq "Y" -or $Answer -eq "Yes") {
  8.     createUserAndFolder
  9.   } else {
  10.     Write-Host "Good-Bye"
  11.   }
  12. }
  13.  
  14. function createUserAndFolder () {
  15.   $Counter = 0
  16.   $CheckPassword = $false
  17.   $FirstName = ""
  18.   $LastName = ""
  19.   $SecurePassword = ""
  20.  
  21.   function getInput () {
  22.     $str = "What is first name?"
  23.  
  24.     if ($counter -eq 1) {
  25.       $str = "What is last name?"
  26.     } elseif ($counter -eq 2) {
  27.       $str = "What is password"
  28.     }
  29.  
  30.     return Read-Host $str
  31.   }
  32.  
  33.   function getPassword () {
  34.     return Read-Host
  35.   }
  36.  
  37.   do
  38.   {
  39.     $Result = getInput
  40.     if ($Counter -lt 2) {
  41.       if ($Result -cmatch '^[A-Z]{1}[a-z]{2,32}$') {
  42.         if ($Counter -eq 0) {
  43.           $FirstName = $Result
  44.         } elseif ($Counter -eq 1) {
  45.           $LastName = $Result
  46.         }
  47.         $Counter++
  48.       } else {
  49.         if ($Counter -eq 0) {
  50.           Write-Host "$Result is not a valid name " -ForegroundColor red
  51.           Write-Host "Name must be between 3 and 33 characters and start with upper letter!" -ForegroundColor red
  52.           Write-Host "For example: Pesho" -ForegroundColor yellow
  53.         } elseif ($Counter -eq 1) {
  54.           Write-Host "$Result is not a valid name " -ForegroundColor red
  55.           Write-Host "Name must be between 3 and 33 characters and start with upper letter!" -ForegroundColor red
  56.           Write-Host "For example: Dimitrov" -ForegroundColor yellow }
  57.       }
  58.  
  59.     } else {
  60.       if ($Result -cmatch '^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$') {
  61.  
  62.         $SecurePassword = $Result | ConvertTo-SecureString -AsPlainText -Force
  63.         $Counter++
  64.       } else {
  65.         Write-Host $Result
  66.         Write-Host "Is not a valid password" -ForegroundColor red
  67.         Write-Host "Password must contains minimum 8 characters, at least 1 uppercase letter, 1 lowercase letter and 1 number!" -ForegroundColor red
  68.       }
  69.     }
  70.  
  71.   } until ($Counter -gt 2)
  72.  
  73.  
  74.   $Name = "$($Firstname) $($LastName)"
  75.   $SamName = "$($Firstname.ToLower()).$($LastName.ToLower())"
  76.  
  77.   New-ADUser -Name $Name -SamAccountName $SamName -AccountPassword $Password -Enabled $true
  78.   New-Item -Path "C:\Shared" -Name $SamName -ItemType "directory"
  79.   anotherUser
  80. }
  81.  
  82.  
  83. createUserAndFolder
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement