Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1.  
  2. <#
  3. .SYNOPSIS
  4. This script will create users out of a csv file and creates home folders for them
  5. .DESCRIPTION
  6. The CSV file needs the following structure: Surname,GivenName,Username,Password
  7. .PARAMENTER filename
  8. The absolute path to the file
  9. .PARAMENTER ouname
  10. The name for the created OU
  11. .EXAMPLE
  12. aufgabe2.ps1 -filename "C:\students.csv" -ouname "students"
  13. #>
  14. #Parameters are fetched
  15. param
  16. (
  17.  
  18. [string]$filename,
  19. [string]$ouName
  20.  
  21. )
  22.  
  23. function createOU{
  24. Write-Host $args
  25. [string]$name = $args
  26. if(Get-ADOrganizationalUnit -ErrorAction SilentlyContinue -Identity "OU= $args , DC= $DC , DC= $DCD"){
  27. write-host "Deleting old OU..." -ForegroundColor Red
  28. #Removing protection From Accidental Deletion
  29. Set-ADOrganizationalUnit -Identity "OU= $args , DC= $DC , DC= $DCD " -ProtectedFromAccidentalDeletion $false
  30. Remove-ADOrganizationalUnit -Recursive -Identity "OU= $args , DC= $DC , DC= $DCD " -Confirm:$false
  31. }
  32. New-ADOrganizationalUnit -Name $name -ProtectedFromAccidentalDeletion $false -Path "DC= $DC, DC= $DCD"
  33. }
  34.  
  35. function getUsers{
  36. #Importing the CSV in a variable delimited by ","
  37. $users = Import-Csv $args[0] -Delimiter ","
  38. #The variable (array) is iterated and every object is send to the create user function
  39. foreach($user in $users){
  40. createUser $user
  41. }
  42. }
  43.  
  44. function createUser{
  45. $username = $args.Username
  46. #Converting the password to a hash
  47. $password = $args.Password | ConvertTo-SecureString -AsPlainText -Force
  48. $fullName = $args.Surname + " " + $args.GivenName
  49. #Password will never expire -> for testing easiest way
  50. New-ADUser -Name $username -AccountPassword $password -PasswordNeverExpires 1 -HomeDirectory "C:\Homes\$username" -GivenName $fullName -Path "OU= $ouName , DC= $DC , DC= $DCD"
  51. #AD account must be enables
  52. Enable-ADAccount -Identity $username
  53. #Home folder will be created
  54. createHomeDir $username
  55. }
  56.  
  57. function createHomeDir{
  58. #Fetching the username from args
  59. $username = $args[0]
  60. #Searching up the just created user
  61. $user = get-aduser -Filter {Name -eq "$username"}
  62. #Creates the folder
  63. if((Test-Path -Path "C:\Homes\$username") -eq $false){
  64. write-host "Creating folder for $username" -ForegroundColor Green
  65. #Creating the folder and piping the information to nirvana
  66. New-Item -Path "C:\Homes\" -name $username -ItemType Directory | Out-Null
  67. #Permissions for the folder
  68. $Rights = [System.Security.AccessControl.FileSystemRights]"FullControl"
  69. #Inheritation type
  70. $Inheritance = [System.Security.AccessControl.InheritanceFlags]::"ContainerInherit", "ObjectInherit"
  71. #Propagation type
  72. $Propagation = [System.Security.AccessControl.PropagationFlags]::None
  73. #The Access controll type, can be Allow or Deny
  74. $AC =[System.Security.AccessControl.AccessControlType]::Allow
  75. #Creating the new ACL-Object with the vars from above
  76. $NewACL = New-Object System.Security.AccessControl.FileSystemAccessRule ($username, $Rights, $Inheritance, $Propagation, $AC)
  77. #Fetching the current acl
  78. $ACL = Get-Acl -Path "C:\Homes\$username"
  79. $ACL.SetAccessRuleProtection($True, $False)
  80. #Adding the rule to the current ACL
  81. $ACL.SetAccessRule($NewACL)
  82.  
  83. #ACL for SYSTEM
  84. $NewACL = New-Object System.Security.AccessControl.FileSystemAccessRule ("SYSTEM", $Rights, $Inheritance, $Propagation, $AC)
  85. $ACL.SetAccessRule($NewACL)
  86. #ACL for Administrator securtity group
  87. $NewACL = New-Object System.Security.AccessControl.FileSystemAccessRule ("Administrators", $Rights, $Inheritance, $Propagation, $AC)
  88. $ACL.SetAccessRule($NewACL)
  89. #Setting and applying the new ACL
  90. Set-Acl -Path "C:\Homes\$username" -AclObject $ACL
  91.  
  92. #Mounting the Created folder as H:
  93. $homeDrive = "H:"
  94. $homeDir = '\\JACLO187AUTWI\$Homes\' + $username
  95. Set-ADUser -Identity $username -Replace @{HomeDirectory=$homeDir}
  96. Set-ADUser -Identity $username -Replace @{HomeDrive=$homeDrive}
  97.  
  98. }
  99.  
  100. }
  101.  
  102. Function Get-FileName($initialDirectory)
  103. {
  104. #Open file dialog with applied .csv filter and
  105. $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
  106. $OpenFileDialog.initialDirectory = $initialDirectory
  107. $OpenFileDialog.filter = "CSV Files| *.csv"
  108. $OpenFileDialog.ShowDialog() | Out-Null
  109. $OpenFileDialog.filename
  110. }
  111.  
  112. clear
  113. #requires -Version 2
  114. $ErrorActionPreference = 'SilentlyContinue'
  115. #Setting the Domain Controller ass global variable
  116. $DC = "JACLO187"
  117. $DCD = "lam"
  118. #Starting script
  119. if(!$ouname){
  120. Write-Host "Choose your OU name. Existing OUs will be overridden!" -ForegroundColor red
  121. $ouName = Read-Host -Prompt "OU Name"
  122. }
  123. if(!$filename){
  124. Write-Host "Choose your CSV file." -ForegroundColor red
  125. $filename = Get-FileName("C:")
  126. }
  127. createOU $ouName
  128. getUsers $filename
  129. #Resets this variable so other scripts wont be affected
  130. $ErrorActionPreference = 'Continue'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement