Last active
April 22, 2016 13:44
-
-
Save micmaher/9d7332f793359cae8e3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires RunAsAdministrator | |
Function Create-WindowsForm(){ | |
<# | |
.DATE | |
11/3/16 | |
.NOTE | |
Send username, new password, old | |
Enables user account, creates home drive | |
.EXAMPLE | |
samAccountName,password | |
#> | |
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | |
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | |
$Form = New-Object System.Windows.Forms.Form | |
$Form.Text = "Sample Form" | |
$Form.AutoSizeMode = "GrowAndShrink" # or GrowOnly | |
$Form.BackColor = 'white' | |
$Form.AutoSize = $False | |
$Form.MinimizeBox = $False | |
$Form.MaximizeBox = $False | |
$Form.WindowState = "Normal" | |
$Form.StartPosition = "CenterScreen" | |
$Form.Height = 500 | |
$Form.Width = 470 | |
$Form.AutoScroll = $True | |
$Form.AcceptButton = $Button | |
$Form.KeyPreview = $True | |
# Scope of variable needs to be global to be available outside function | |
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$global:user=$objTextBox1.Text;$global:password=$objTextBox2.Text;$Form.Close()}}) | |
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$Form.Close()}}) | |
# Adds a button labelled OK | |
$OKButton = New-Object System.Windows.Forms.Button | |
$OKButton.Location = New-Object System.Drawing.Size(75,120) | |
$OKButton.Size = New-Object System.Drawing.Size(75,23) | |
$OKButton.Text = "OK" | |
# Scope of variable needs to be global to be available outside function | |
$OKButton.Add_Click({$global:user=$objTextBox1.Text;$global:password=$objTextBox2.Text;$Form.Close()}) | |
$Form.Controls.Add($OKButton) | |
# Adds a button labelled Cancel | |
# Each time you add a new control you’ll typically create a new instance of a .NET Framework class | |
$CancelButton = New-Object System.Windows.Forms.Button | |
$CancelButton.Location = New-Object System.Drawing.Size(150,120) | |
$CancelButton.Size = New-Object System.Drawing.Size(75,23) | |
$CancelButton.Text = "Cancel" | |
$CancelButton.Add_Click({$Form.Close()}) | |
$Form.Controls.Add($CancelButton) | |
$objLabel1 = New-Object System.Windows.Forms.Label | |
$objLabel1.Location = New-Object System.Drawing.Size(10,20) | |
$objLabel1.Size = New-Object System.Drawing.Size(280,20) | |
$objLabel1.Text = "Please enter the username:" | |
$Form.Controls.Add($objLabel1) | |
$objLabel2 = New-Object System.Windows.Forms.Label | |
$objLabel2.Location = New-Object System.Drawing.Size(10,70) | |
$objLabel2.Size = New-Object System.Drawing.Size(280,20) | |
$objLabel2.Text = "Please enter the password:" | |
$Form.Controls.Add($objLabel2) | |
$objTextBox1 = New-Object System.Windows.Forms.TextBox | |
$objTextBox1.Location = New-Object System.Drawing.Size(10,40) | |
$objTextBox1.Size = New-Object System.Drawing.Size(200,20) | |
$Form.Controls.Add($objTextBox1) | |
$objTextBox2 = New-Object System.Windows.Forms.TextBox | |
$objTextBox2.Location = New-Object System.Drawing.Size(10,90) | |
$objTextBox2.Size = New-Object System.Drawing.Size(200,20) | |
$Form.Controls.Add($objTextBox2) | |
# Bring window to the foreground | |
$Form.Topmost = $True | |
$Form.Add_Shown({$Form.Activate()}) | |
# Now show the form on-screen | |
[void] $Form.ShowDialog() | |
$user | |
$password | |
} | |
Create-WindowsForm; | |
#region Variables | |
$remoteServer = "filesrv" | |
$domain = "contoso.com" | |
$homeShare = "Home" | |
$ShareDrive = "E" | |
$Right="FullControl" | |
$absolutePath = "$($shareDrive):\homedrives" | |
$hiddenShare = "homeDrives$($shareDrive)$" | |
#endregion | |
import-module activeDirectory | |
#region Create User | |
if ($user){ | |
Write-Verbose "Working on $user" | |
Try{ | |
Enable-ADAccount -Identity $user | |
Write-Verbose "Enabled $user" | |
} | |
Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage} | |
Try{ | |
Set-ADAccountPassword -Identity $user -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $password -Force) | |
Write-Verbose "Reset password for $user" | |
} | |
Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage} | |
Try{ | |
Invoke-Command -computername $remoteServer {New-Item -path $args[0] -type directory} -Args "$($absolutePath)\$($user)" | |
Write-Verbose "Created directory $($absolutePath)\$($user)" | |
} | |
Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage} | |
Try{ | |
$rule=new-object System.Security.AccessControl.FileSystemAccessRule($user, $Right, "ContainerInherit, ObjectInherit", "None", "Allow") | |
$acl=get-acl "\\$($remoteServer)\$($hiddenShare)\$($user)" | |
$acl.SetAccessRule($rule) | |
set-acl "\\$($remoteServer)\$($hiddenShare)\$($user)" $acl | |
Write-Verbose "Set ACLs on Home Directory" | |
} | |
Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage} | |
Try{ | |
Write-Verbose "Created DFS Link for Home Directory" | |
Invoke-Command -computername $remoteServer {New-DfsnFolder -path $args[0] -targetpath $args[1]} -Args "\\$($domain)\$($homeShare)\$($user)", "\\$($remoteServer)\$($hiddenShare)\$($user)" | |
} | |
Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage} | |
Try{ | |
Set-ADUser -Identity $user -HomeDrive "H:" -HomeDirectory "\\$($domain)\$($homeShare)\$($user)" | |
Write-Verbose "Set Home Directory mapping" | |
} | |
Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage} | |
Try{ | |
Invoke-Command -computername $remoteServer {New-FsrmQuota -Path $args[0] -Template "2GB HomeDir Limit"} -Args "$($absolutePath)\$($user)" | |
Write-Verbose "Set 2GB Quota for Home Directory" | |
} | |
Catch{Write-Verbose $error[0].ToString() + $error[0].InvocationInfo.PositionMessage} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment