Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Default Variable Settings
- $ErrorActionPreference = "Continue"
- #Arrays
- $Prefix = ("Blue","Green","Pink","Copy","Club","Love","Hate","Cold","Warm","Help","Park","Text","Work","Head","Hair","Hand","Face","Fire","Moon","Dark","Math","Snow","Corn","Card","Lost","Book","Town","Gold","Fish","Drop","Wild","Test","Tool","Bank","Song","Ring","Weed","Flag","Dash","Crap","Food","Wing","Wish","Star","Soda","Slow","Dirt","Girl","Brat","Grid","Date")
- #######################################
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -Name Window -Namespace Console -MemberDefinition '
- [DllImport("Kernel32.dll")]
- public static extern IntPtr GetConsoleWindow();
- [DllImport("user32.dll")]
- public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
- '
- #Function storage
- Function Hide-Console{
- $consolePtr = [Console.Window]::GetConsoleWindow()
- #0 hide
- [Console.Window]::ShowWindow($consolePtr, 0)
- }
- Function GetUserInfo {
- $Global:UserInfo = Get-ADUser $Global:User -properties *
- #Determine if the password has ever been set, or if they're using a temp password, as opposed to just erroring out.
- If (!($Global:UserInfo.PasswordLastSet)) { $Global:PasswordLastSet = "Temp password is set."; $Global:PasswordNextSet = "Temp password is set."}
- ElseIf (($Global:UserInfo.PasswordLastSet)) {$Global:PasswordLastSet = $UserInfo.PasswordLastSet; $Global:PasswordNextSet = ($Global:userinfo.passwordlastset).adddays(60)}
- Else {"The users password is in a super position of existing and not existing. Radical."}
- $Global:TextBox_Name.Text = $Global:UserInfo.Name
- $Global:TextBox_PasswordLastSet.Text = $Global:PasswordLastSet
- $Global:TextBox_PasswordNextSet.Text = $Global:PasswordNextSet
- $TextBox_UserLockoutStatus.Text = $Global:UserInfo.LockedOut
- $TextBox_LastLoginDate.Text = Get-Date $UserInfo.LastLogon -UFormat "%D %R"
- $TextBox_DisabledStatus.Text = $Global:UserInfo.Enabled
- $TextBox_UserName.Text = $Global:UserInfo.SamAccountName
- }
- Function LookupID {
- $Global:User = Get-ADUser -Filter "EmployeeID -Eq $ID"
- #Verify The user exists
- If (!($Global:User)) {$TextBox_Name.Text = "User does not exist"}
- Else {GetUserInfo}
- }
- ########################End of Function storage
- ##############################################Code for GUI
- Add-Type -AssemblyName PresentationFramework
- [xml]$xaml = @"
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Window_Main"
- Title="Staff Password Manager" Height="392.192" Width="523.256" ResizeMode="CanMinimize">
- <Grid Margin="0">
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="278" Margin="-2,0,0,-2" Stroke="Black" VerticalAlignment="Bottom" Width="236"/>
- <Button x:Name="Button_LookupID" Content="Lookup ID" HorizontalAlignment="Left" Margin="129,44,0,0" VerticalAlignment="Top" Width="75" IsDefault="True" TabIndex="1"/>
- <TextBox x:Name="TextBox_EnterID" HorizontalAlignment="Left" Height="20" Margin="10,44,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="105" TabIndex="0"/>
- <TextBlock x:Name="Label_UserID" HorizontalAlignment="Left" Height="14" Margin="10,25,0,0" TextWrapping="Wrap" Text="Enter a User ID" VerticalAlignment="Top" Width="105"/>
- <TextBlock x:Name="TextBlock_NameTitle" HorizontalAlignment="Left" Height="19" Margin="236,20,0,0" TextWrapping="Wrap" Text="Legal Name" VerticalAlignment="Top" Width="81"/>
- <TextBox x:Name="TextBox_Name" HorizontalAlignment="Left" Height="20" Margin="236,44,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="249" IsTabStop="False" IsEnabled="False"/>
- <TextBlock HorizontalAlignment="Left" Height="16" Margin="15,93,0,0" TextWrapping="Wrap" Text="Date Password Was Last Set" VerticalAlignment="Top" Width="151"/>
- <TextBox x:Name="TextBox_PasswordLastSet" HorizontalAlignment="Left" Height="19" Margin="15,114,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsEnabled="False"/>
- <TextBlock HorizontalAlignment="Left" Height="16" Margin="15,138,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="141" Text="Next Reset Time
"/>
- <TextBox x:Name="TextBox_PasswordNextSet" HorizontalAlignment="Left" Height="19" Margin="15,156,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsEnabled="False"/>
- <TextBlock HorizontalAlignment="Left" Height="16" Margin="15,261,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="98" Text="Lockout Status"/>
- <TextBox x:Name="TextBox_LastLoginDate" HorizontalAlignment="Left" Height="19" Margin="15,197,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsEnabled="False"/>
- <TextBlock HorizontalAlignment="Left" Height="16" Margin="15,180,0,0" TextWrapping="Wrap" Text="Last Login Date" VerticalAlignment="Top" Width="117"/>
- <TextBox x:Name="TextBox_UserLockoutStatus" HorizontalAlignment="Left" Height="19" Margin="15,279,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsEnabled="False"/>
- <Rectangle Fill="#FFF4F4F5" Height="278" Margin="0,0,-2,-2" Stroke="Black" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="285"/>
- <TextBox x:Name="TextBox_ResetPassword" HorizontalAlignment="Left" Height="19" TextWrapping="Wrap" VerticalAlignment="Top" Width="143" Margin="305,197,0,0" IsEnabled="False"/>
- <Button x:Name="Button_ResetUserPassword" Content="Reset the Users Password" HorizontalAlignment="Left" Height="19" Margin="305,156,0,0" VerticalAlignment="Top" Width="143" TabIndex="2"/>
- <TextBlock HorizontalAlignment="Left" Height="16" Margin="305,183,0,0" TextWrapping="Wrap" Text="New Password" VerticalAlignment="Top" Width="143" TextAlignment="Center"/>
- <TextBlock HorizontalAlignment="Left" Height="17" Margin="277,239,0,0" TextWrapping="Wrap" Text="Errors / Notifications" VerticalAlignment="Top" Width="208" TextAlignment="Center"/>
- <TextBox x:Name="TextBox_ErrorNotification" HorizontalAlignment="Right" Height="77" Margin="0,269,17,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="245" IsEnabled="False"/>
- <Button x:Name="Button_UnlockUserAccount" Content="Unlock User's Account" HorizontalAlignment="Left" Height="19" Margin="305,114,0,0" VerticalAlignment="Top" Width="143" TabIndex="3"/>
- <TextBlock HorizontalAlignment="Left" Height="16" Margin="15,221,0,0" TextWrapping="Wrap" Text="User is Enabled?" VerticalAlignment="Top" Width="97"/>
- <TextBox x:Name="TextBox_DisabledStatus" HorizontalAlignment="Left" Height="17" Margin="15,239,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsEnabled="False"/>
- <TextBlock x:Name="TextBlock_UserName" HorizontalAlignment="Left" Height="19" Margin="15,303,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="65"><Run Text="User "/><Run Text="Name"/></TextBlock>
- <TextBox x:Name="TextBox_UserName" HorizontalAlignment="Left" Height="19" Margin="15,327,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsTabStop="False" IsEnabled="False"/>
- </Grid>
- </Window>
- "@
- $reader = (New-Object System.Xml.XmlNodeReader $xaml)
- $window = [Windows.Markup.XamlReader]::Load($reader)
- ##########################End of Code for GUI#####################
- #########################################Powershell Code goes here
- #Hide the console window from the user.
- Hide-Console
- #Feed TextBox and Buttons from the GUI into the script.
- #TextBoxes
- $TextBox_EnterID = $Window.FindName("TextBox_EnterID")
- $TextBox_Name = $Window.FindName("TextBox_Name")
- $TextBox_PasswordLastSet = $Window.FindName("TextBox_PasswordLastSet")
- $TextBox_PasswordNextSet = $Window.FindName("TextBox_PasswordNextSet")
- $TextBox_UserLockoutStatus = $Window.FindName("TextBox_UserLockoutStatus")
- $TextBox_LastLoginDate = $Window.FindName("TextBox_LastLoginDate")
- $TextBox_ResetPassword = $Window.FindName("TextBox_ResetPassword")
- $TextBox_ErrorNotification = $Window.FindName("TextBox_ErrorNotification")
- $TextBox_DisabledStatus = $Window.FindName("TextBox_DisabledStatus")
- $TextBox_UserName = $Window.FindName("TextBox_UserName")
- #Buttons
- $Button_ResetUserPassword = $Window.FindName("Button_ResetUserPassword")
- $Button_UnlockUserAccount = $Window.FindName("Button_UnlockUserAccount")
- $Button_LookupID = $Window.FindName("Button_LookupID")
- #Actions if the lookup ID button is clicked.
- $Button_LookupID.Add_Click({
- #Clear all text fields.
- $Global:TextBox_Name.Text = " "
- $Global:TextBox_PasswordLastSet.Text = " "
- $Global:TextBox_PasswordNextSet.Text = " "
- $TextBox_UserLockoutStatus.Text = " "
- $TextBox_LastLoginDate.Text = " "
- $TextBox_ResetPassword.Text = " "
- $TextBox_ErrorNotification.Text = " "
- $TextBox_UserName.Text = " "
- $ID = $TextBox_EnterID.Text
- If ($ID.Length -Eq "5" -And $ID -Match '^\d+$') {LookupID}
- Else {[System.Windows.MessageBox]::Show("The ID number must be 5 numbers.")}
- })
- #Actions if the reset password button is clicked.
- $Button_ResetUserPassword.Add_Click({
- If ($TextBox_Name.Text -Eq "User does not exist") {
- $TextBox_ResetPassword.Text = "Password cannot be set."
- }
- Else {
- #Generate Randome password from Illa's list.
- $Word = ("Blue","Green","Pink","Copy","Club","Love","Cold","Warm","Help","Park","Text","Work","Head","Bulk","Hand","Face","Fire","Moon","Dark","Math","Snow","Corn","Card","Lost","Book","Town","Gold","Fish","Drop","Wild","Test","Tool","Bank","Song","Ring","Flag","Dash","Food","Wing","Wish","Soda","Slow","Dirt","Girl","Brat","Grid","Date","Tool","Daft","Echo","Iris","Tofu","Ruby","Puma")
- $Suffix = Get-Random -Maximum 9999
- $Suffix = $Suffix.ToString("0000")
- $Prefix = $Word[(Get-Random -Maximum $Word.Count)]
- $TempPassword = $Prefix+$Suffix
- $Identity = $Global:User.SamAccountName
- #Capture output from resetting password to show as an error or sucess.
- $PassowrdSetError = $($OutputOnly= .{
- Set-ADAccountPassword -Identity $Global:User.SamAccountName -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$TempPassword" -Force)
- Set-ADUser -ChangePasswordAtLogon $True -Identity $Global:User.SamAccountName -Confirm:$False -Verbose
- If ($Global:UserInfo.LockedOut -EQ "True") {Unlock-ADAccount -Identity $Global:User.SamAccountName}
- Else {}
- }) 4>&1
- #Present password to GUI
- $TextBox_ResetPassword.Text = $TempPassword
- $TextBox_ErrorNotification.Text = "$PassowrdSetError"
- #Unlock user account if required.
- }
- })
- #Actions if Unlock User Button is clicked.
- $Button_UnlockUserAccount.Add_Click({
- $PassowrdSetError = $($OutputOnly= .{
- If (($Global:User)) {Unlock-ADAccount -Identity $Global:User.SamAccountName}
- Else {}
- }) 4>&1
- $TextBox_ErrorNotification.Text = "$PassowrdSetError"
- $Global:UserInfo = Get-ADUser $Global:User -properties *
- $TextBox_UserLockoutStatus.Text = "$Global:UserInfo.LockedOut"
- })
- #Get OS version
- $OS = (Get-WmiObject Win32_OperatingSystem).Caption
- #Determine OS and exit if unsupported.
- If ($OS -Like "*Windows 10*") {$OSVersion = "10"}
- ElseIf ($OS -Like "*Windows 7*") {$OSVersion = "7"}
- #Exit if not Windows 10 or 7
- Else {[System.Windows.MessageBox]::Show("Unable to determine operation system, or Operating system not supported."); Exit}
- #Make sure this isn't being run on a student computers
- If ($Env:Computername -Like "*STA*" -And $Env:Computername -NotLike "*STU*" -And $Env:Computername -NotLike "*GIS*" -And $Env:Computername -NotLike "*SPED*") {}
- #Scold user and exit if not run on a staff computer.
- Else {[System.Windows.MessageBox]::Show("I refuse to run on a student computer. Try again on a staff computer.", "Error");Exit}
- #Verify ADUC is installed.
- $ADUC = test-path "C:\windows\system32\dsa.msc"
- If ($ADUC-eq "True") {}
- #If ADUC is not installed prompt user for installation.
- Else {
- $InstallADUC = [System.Windows.Forms.MessageBox]::Show("Active directory users and computers is not installed. Would you like me to try and install it for you? I will need to reboot immediately after install." , "Error" , 3)
- #If the user wants to install ADUC
- If ($InstallADUC -Eq "Yes") {
- $BundleList = zac bl
- If ($BundleList -Like "*Remote Server Admin Tools for*") {zac bin "Remote Server Admin Tools for Windows $OSVersion"; Restart-Computer -Force}
- Else {[System.Windows.Forms.MessageBox]::Show("Looks like the ADUC bundle, AKA Remote Server Admin Tools, is not assigned to this computer. That's rough brah.", "Bummer"); Exit}
- }
- #If the user didn't want to install ADUC
- Elseif ($InstallADUC -Eq "No") {[System.Windows.Forms.MessageBox]::Show("Later brah." , "Byeeee"); Exit}
- Elseif ($InstallADUC -Eq "Cancel") {[System.Windows.Forms.MessageBox]::Show("Later brah." , "Byeeee"); Exit}
- Else {[System.Windows.Forms.MessageBox]::Show("Later brah." , "Byeeee"); Exit}
- }
- $window.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement