Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. Function Create-WindowsForm(){
  2. <#
  3. .DATE
  4. 11/3/16
  5.  
  6. .NOTE
  7. Send username, new password, old
  8. Enables user account, creates home drive
  9.  
  10. .EXAMPLE
  11. samAccountName,password
  12. #>
  13. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  14. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  15.  
  16. $Form = New-Object System.Windows.Forms.Form
  17. $Form.Text = "Sample Form"
  18. $Form.AutoSizeMode = "GrowAndShrink" # or GrowOnly
  19. $Form.BackColor = 'white'
  20. $Form.AutoSize = $False
  21. $Form.MinimizeBox = $False
  22. $Form.MaximizeBox = $False
  23. $Form.WindowState = "Normal"
  24. $Form.StartPosition = "CenterScreen"
  25. $Form.Height = 500
  26. $Form.Width = 470
  27. $Form.AutoScroll = $True
  28. $Form.AcceptButton = $Button
  29.  
  30. $Form.KeyPreview = $True
  31. # Scope of variable needs to be global to be available outside function
  32. $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$global:user=$objTextBox1.Text;$global:password=$objTextBox2.Text;$Form.Close()}})
  33. $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$Form.Close()}})
  34.  
  35. # Adds a button labelled OK
  36. $OKButton = New-Object System.Windows.Forms.Button
  37. $OKButton.Location = New-Object System.Drawing.Size(75,120)
  38. $OKButton.Size = New-Object System.Drawing.Size(75,23)
  39. $OKButton.Text = "OK"
  40. # Scope of variable needs to be global to be available outside function
  41. $OKButton.Add_Click({$global:user=$objTextBox1.Text;$global:password=$objTextBox2.Text;$Form.Close()})
  42. $Form.Controls.Add($OKButton)
  43.  
  44. # Adds a button labelled Cancel
  45. # Each time you add a new control you’ll typically create a new instance of a .NET Framework class
  46. $CancelButton = New-Object System.Windows.Forms.Button
  47. $CancelButton.Location = New-Object System.Drawing.Size(150,120)
  48. $CancelButton.Size = New-Object System.Drawing.Size(75,23)
  49. $CancelButton.Text = "Cancel"
  50. $CancelButton.Add_Click({$Form.Close()})
  51. $Form.Controls.Add($CancelButton)
  52.  
  53. $objLabel1 = New-Object System.Windows.Forms.Label
  54. $objLabel1.Location = New-Object System.Drawing.Size(10,20)
  55. $objLabel1.Size = New-Object System.Drawing.Size(280,20)
  56. $objLabel1.Text = "Please enter the username:"
  57. $Form.Controls.Add($objLabel1)
  58.  
  59.  
  60. $objLabel2 = New-Object System.Windows.Forms.Label
  61. $objLabel2.Location = New-Object System.Drawing.Size(10,70)
  62. $objLabel2.Size = New-Object System.Drawing.Size(280,20)
  63. $objLabel2.Text = "Please enter the password:"
  64. $Form.Controls.Add($objLabel2)
  65.  
  66. $objTextBox1 = New-Object System.Windows.Forms.TextBox
  67. $objTextBox1.Location = New-Object System.Drawing.Size(10,40)
  68. $objTextBox1.Size = New-Object System.Drawing.Size(200,20)
  69. $Form.Controls.Add($objTextBox1)
  70.  
  71.  
  72. $objTextBox2 = New-Object System.Windows.Forms.TextBox
  73. $objTextBox2.Location = New-Object System.Drawing.Size(10,90)
  74. $objTextBox2.Size = New-Object System.Drawing.Size(200,20)
  75. $Form.Controls.Add($objTextBox2)
  76.  
  77. # Bring window to the foreground
  78. $Form.Topmost = $True
  79. $Form.Add_Shown({$Form.Activate()})
  80.  
  81. # Now show the form on-screen
  82. [void] $Form.ShowDialog()
  83.  
  84. $user
  85. $password
  86.  
  87. }
  88.  
  89. Create-WindowsForm;
  90.  
  91. #region Variables
  92. $remoteServer = "filesrv"
  93. $domain = "contoso.com"
  94. $homeShare = "Home"
  95. $ShareDrive = "E"
  96. $Right="FullControl"
  97. $absolutePath = "$($shareDrive):\homedrives"
  98. $hiddenShare = "homeDrives$($shareDrive)$"
  99. #endregion
  100.  
  101.  
  102. import-module activeDirectory
  103.  
  104. #region Create User
  105. if ($user){
  106. Write-Verbose "Working on $user"
  107.  
  108. Try{
  109. Enable-ADAccount -Identity $user
  110. Write-Verbose "Enabled $user"
  111. }
  112. Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage}
  113.  
  114.  
  115. Try{
  116. Set-ADAccountPassword -Identity $user -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $password -Force)
  117. Write-Verbose "Reset password for $user"
  118. }
  119. Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage}
  120.  
  121.  
  122. Try{
  123. Invoke-Command -computername $remoteServer {New-Item -path $args[0] -type directory} -Args "$($absolutePath)\$($user)"
  124. Write-Verbose "Created directory $($absolutePath)\$($user)"
  125. }
  126. Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage}
  127.  
  128.  
  129. Try{
  130. $rule=new-object System.Security.AccessControl.FileSystemAccessRule($user, $Right, "ContainerInherit, ObjectInherit", "None", "Allow")
  131. $acl=get-acl "\\$($remoteServer)\$($hiddenShare)\$($user)"
  132. $acl.SetAccessRule($rule)
  133. set-acl "\\$($remoteServer)\$($hiddenShare)\$($user)" $acl
  134. Write-Verbose "Set ACLs on Home Directory"
  135. }
  136. Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage}
  137.  
  138.  
  139. Try{
  140. Write-Verbose "Created DFS Link for Home Directory"
  141. Invoke-Command -computername $remoteServer {New-DfsnFolder -path $args[0] -targetpath $args[1]} -Args "\\$($domain)\$($homeShare)\$($user)", "\\$($remoteServer)\$($hiddenShare)\$($user)"
  142. }
  143. Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage}
  144.  
  145.  
  146. Try{
  147. Set-ADUser -Identity $user -HomeDrive "H:" -HomeDirectory "\\$($domain)\$($homeShare)\$($user)"
  148. Write-Verbose "Set Home Directory mapping"
  149. }
  150. Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage}
  151.  
  152.  
  153. Try{
  154. Invoke-Command -computername $remoteServer {New-FsrmQuota -Path $args[0] -Template "2GB HomeDir Limit"} -Args "$($absolutePath)\$($user)"
  155. Write-Verbose "Set 2GB Quota for Home Directory"
  156. }
  157. Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage}
  158.  
  159. }
  160.  
  161. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement