Advertisement
Guest User

Untitled

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