-
-
Save anonymous/c0af8a332115c26c41c3 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
#GUI Elements inspired by http://foxdeploy.com/ | |
#Not on PowerShell 3.0+, uncomment following line | |
#import-module activedirectory | |
#Random Password Function by http://blog.oddbit.com/2012/11/04/powershell-random-passwords/ | |
Function random-password ($length = 8) | |
{ | |
$punc = 46..46 | |
$digits = 48..57 | |
$letters = 65..90 + 97..122 | |
# Thanks to | |
# https://blogs.technet.com/b/heyscriptingguy/archive/2012/01/07/use-pow | |
$password = get-random -count $length ` | |
-input ($punc + $digits + $letters) | | |
% -begin { $aa = $null } ` | |
-process {$aa += [char]$_} ` | |
-end {$aa} | |
return $password | |
} | |
# namespace: System.Web.Security | |
# assembly: System.Web (in System.Web.dll) | |
# method: GeneratePassword(int length, int numberOfNonAlphanumericCharacters) | |
#Load "System.Web" assembly in PowerShell console | |
#[Reflection.Assembly]::LoadWithPartialName("System.Web") | |
#Calling GeneratePassword Method and storing in a varible | |
$newpassword = random-password | |
#Import Exchange cmdlets | |
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<exchangeserver>/PowerShell/ -Authentication Kerberos | |
Import-PSSession $Session | |
#Define GUI | |
#ERASE ALL THIS AND PUT XAML BELOW between the @" "@ | |
$inputXML = @" | |
<Window x:Class="WpfApplication1.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:local="clr-namespace:WpfApplication1" | |
mc:Ignorable="d" | |
Title="New User Onboarding" Height="350" Width="525"> | |
<Grid> | |
<Image x:Name="image" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="100" Source="<picture source>"/> | |
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="138,21,0,0" TextWrapping="Wrap" Text="This tool is used to create new employee active directory accounts, enable their mailbox, and generate an onboarding letter containing useful information. All fields are required." VerticalAlignment="Top" Height="89" Width="354"/> | |
<Button x:Name="submitbutton" Content="Submit" HorizontalAlignment="Left" Margin="433,271,0,0" VerticalAlignment="Top" Width="75" Height="40"/> | |
<Label x:Name="label" Content="First Name" HorizontalAlignment="Left" Margin="10,154,0,0" VerticalAlignment="Top"/> | |
<TextBox x:Name="firstname_textBox" HorizontalAlignment="Left" Height="23" Margin="92,158,0,0" TextWrapping="Wrap" Text="FIrst Name" VerticalAlignment="Top" Width="120"/> | |
<Label x:Name="label1" Content="Username" HorizontalAlignment="Left" Margin="7,192,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.437,-0.056"/> | |
<Label x:Name="label2" Content="Mailbox Database" HorizontalAlignment="Left" Margin="10,253,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.014,-0.756"/> | |
<Label x:Name="label3" Content="Last Name" HorizontalAlignment="Left" Margin="226,158,0,0" VerticalAlignment="Top"/> | |
<TextBox x:Name="lastname_textBox" HorizontalAlignment="Left" Height="23" Margin="354,158,0,0" TextWrapping="Wrap" Text="Last Name" VerticalAlignment="Top" Width="120"/> | |
<TextBox x:Name="username_textBox" HorizontalAlignment="Left" Height="23" Margin="92,192,0,0" TextWrapping="Wrap" Text="Windows Username" VerticalAlignment="Top" Width="120"/> | |
<Label x:Name="label4" Content="Organizational Unit" HorizontalAlignment="Left" Margin="226,192,0,0" VerticalAlignment="Top"/> | |
<ComboBox x:Name="ou_comboBox" HorizontalAlignment="Left" Margin="354,195,0,0" VerticalAlignment="Top" Width="120"/> | |
<ComboBox x:Name="database_comboBox" HorizontalAlignment="Left" Margin="127,256,0,0" VerticalAlignment="Top" Width="120"/> | |
<Label x:Name="label5" HorizontalAlignment="Left" Margin="92,220,0,0" VerticalAlignment="Top" Visibility="Hidden"/> | |
</Grid> | |
</Window> | |
"@ | |
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window' | |
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') | |
[xml]$XAML = $inputXML | |
#Read XAML | |
$reader=(New-Object System.Xml.XmlNodeReader $xaml) | |
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )} | |
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."} | |
#=========================================================================== | |
# Load XAML Objects In PowerShell | |
#=========================================================================== | |
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)} | |
Function Get-FormVariables{ | |
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true} | |
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan | |
get-variable WPF* | |
} | |
Get-FormVariables | |
#=========================================================================== | |
# Actually make the objects work | |
#=========================================================================== | |
#Load Orgnizational Units we want to use into an array | |
$ous = (get-adobject -SearchBase "<baseOU>" -filter 'ObjectClass -eq "organizationalUnit"') | select DistinguishedName | |
$defaultOU = (get-adobject -filter 'ObjectClass -eq "domain"' -Properties wellKnownObjects).wellknownobjects.Split("`n")[-0].Split(':') | select -Last 1 | |
$ous | ForEach-Object {$_.DistinguishedName} | ForEach-Object {$WPFou_comboBox.AddChild($_)} | |
"<additionalOU>","<additionalOU>" | ForEach-object {$WPFou_comboBox.AddChild($_)} | |
$database = (get-mailboxdatabase -Server <exchange server>) | select name | |
$database | ForEach-Object {$_.Name} | ForEach-Object {$WPFdatabase_comboBox.AddChild($_)} | |
#$user = $WPFusername_textBox.Text | |
#Populate variables from combo boxes | |
$WPFusername_textBox.add_LostFocus({ | |
$user = $WPFusername_textBox.Text | |
$user | |
if (Get-ADUser -Filter {samaccountname -eq $user} ) { | |
# Exists | |
$WPFlabel5.Visibility = 'Visible' | |
$WPFlabel5.Background = '#FFEBFF00' | |
$WPFlabel5.Content = "Username in use" | |
} | |
else { | |
# Doesn't Exist | |
$WPFlabel5.Visibility = 'Visible' | |
$WPFlabel5.Background = "#00FFFFFF" | |
$WPFlabel5.Content = "Username available" | |
} | |
}) | |
function Get-FormFields { | |
$location = if ($WPFou_comboBox.Text -ne $null){$WPFou_comboBox.Text}else{$defaultOU} | |
$HashArguments = | |
@{ Name = $WPFusername_textBox.Text; | |
GivenName=$WPFfirstname_textBox.Text; | |
SurName = $WPFlastname_textBox.Text; | |
AccountPassword=($newpassword | ConvertTo-SecureString -AsPlainText -Force); | |
Path=$location; | |
} | |
$HashArguments | |
} | |
function Get-ExchangeFields { | |
$database = $WPFdatabase_comboBox.Text | |
$ExHash = | |
@{ Identity = $WPFusername_textBox.Text; | |
Database = $database; | |
} | |
$ExHash | |
} | |
$WPFsubmitbutton.Add_Click({ | |
#Resolve Form Settings | |
$hash = Get-FormFields | |
New-ADUser @hash -whatif -PassThru -Enabled:$true -ChangePasswordAtLogon:$true | |
start-sleep -Seconds 30 | |
$ExchangeHash = Get-ExchangeFields | |
Enable-Mailbox @ExchangeHash -whatif | |
$Form.Close()}) | |
#Sample entry of how to add data to a field | |
#$vmpicklistView.items.Add([pscustomobject]@{'VMName'=($_).Name;Status=$_.Status;Other="Yes"}) | |
#=========================================================================== | |
# Shows the form | |
#=========================================================================== | |
write-host "To show the form, run the following" -ForegroundColor Cyan | |
'$Form.ShowDialog() | out-null' | |
$Form.ShowDialog() | out-null | |
#Define Variables for AD Account that are populated via GUI | |
$firstname = $WPFfirstname_textBox.Text | |
$lastname = $WPFlastname_textBox.Text | |
$empname = $firstname + " " + $lastname | |
$newuser = $WPFusername_textBox.Text | |
#Creating Word Onboarding Document | |
$word = new-object -ComObject "Word.Application" | |
$doc = $word.documents.Add() | |
#$word.Visible = $true | |
#Set header image | |
$Doc.ActiveWindow.ActivePane.View.SeekView=$wdSeekPrimaryHeader | |
$selection = $word.Selection | |
$objShape = $Selection.InlineShapes.AddPicture("<image path here>") | |
#Leave header section | |
$wdSeekPrimaryHeader = 0 | |
$Doc.ActiveWindow.ActivePane.View.SeekView=$wdSeekPrimaryHeader | |
#Start creating document | |
$wdAlignParagraphRight = 1 | |
$selection.ParagraphFormat.Alignment=$wdAlignParagraphRight | |
$selection.TypeText((get-date)) | |
$selection.TypeParagraph() | |
$wdAlignParagraphRight = 0 | |
$selection.ParagraphFormat.Alignment=$wdAlignParagraphRight | |
$selection.TypeText(("<word doc text> $($empname). <word doc text>")) | |
$selection.TypeText(("<word doc text>")) | |
$selection.TypeParagraph() | |
$selection.Font.Bold=$True | |
$selection.TypeText(("Your Network and E-Mail username is: ")) | |
$selection.Font.Bold=$False | |
$selection.TypeText(("$($newuser)")) | |
$selection.TypeParagraph() | |
$selection.Font.Bold=$True | |
$selection.TypeText(("Your one-time password is: ")) | |
$selection.Font.Bold=$False | |
$selection.TypeText(("$($newpassword)")) | |
$doc.SaveAs([ref]"C:\temp\$($newuser)_onboarding.docx") | |
$doc.Close() | |
$word.Quit() | |
Send-MailMessage -Subject "<email subject>" -Body "<email body>" -Attachments <filepath>\"$newuser"_onboarding.docx -from <from address> -SmtpServer <smtpserver> -to <email address> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment