Advertisement
UlrichC

Untitled

Jul 10th, 2019
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Default Variable Settings
  2. $ErrorActionPreference = "Continue"
  3. #Arrays
  4. $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")
  5. #######################################
  6. Add-Type -AssemblyName System.Windows.Forms
  7.  
  8. Add-Type -Name Window -Namespace Console -MemberDefinition '
  9. [DllImport("Kernel32.dll")]
  10. public static extern IntPtr GetConsoleWindow();
  11.  
  12. [DllImport("user32.dll")]
  13. public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
  14. '
  15.  
  16. #Function storage
  17. Function Hide-Console{
  18.     $consolePtr = [Console.Window]::GetConsoleWindow()
  19.     #0 hide
  20.     [Console.Window]::ShowWindow($consolePtr, 0)
  21. }
  22.  
  23. Function GetUserInfo {
  24.  $Global:UserInfo = Get-ADUser $Global:User -properties *
  25.  
  26.  
  27.  
  28.  #Determine if the password has ever been set, or if they're using a temp password, as opposed to just erroring out.
  29. If (!($Global:UserInfo.PasswordLastSet)) { $Global:PasswordLastSet = "Temp password is set."; $Global:PasswordNextSet = "Temp password is set."}
  30. ElseIf (($Global:UserInfo.PasswordLastSet)) {$Global:PasswordLastSet = $UserInfo.PasswordLastSet; $Global:PasswordNextSet = ($Global:userinfo.passwordlastset).adddays(60)}
  31. Else {"The users password is in a super position of existing and not existing. Radical."}
  32.  
  33. $Global:TextBox_Name.Text = $Global:UserInfo.Name
  34. $Global:TextBox_PasswordLastSet.Text = $Global:PasswordLastSet
  35. $Global:TextBox_PasswordNextSet.Text = $Global:PasswordNextSet
  36. $TextBox_UserLockoutStatus.Text = $Global:UserInfo.LockedOut
  37. $TextBox_LastLoginDate.Text = Get-Date $UserInfo.LastLogon -UFormat "%D %R"
  38. $TextBox_DisabledStatus.Text = $Global:UserInfo.Enabled
  39. $TextBox_UserName.Text = $Global:UserInfo.SamAccountName
  40.  
  41. }
  42.  
  43. Function LookupID {
  44. $Global:User = Get-ADUser -Filter "EmployeeID -Eq $ID" 
  45.  
  46. #Verify The user exists
  47. If (!($Global:User)) {$TextBox_Name.Text = "User does not exist"}
  48. Else {GetUserInfo}
  49. }
  50.  
  51.  
  52. ########################End of Function storage
  53.  
  54. ##############################################Code for GUI
  55. Add-Type -AssemblyName PresentationFramework
  56.  
  57. [xml]$xaml = @"
  58. <Window
  59.  
  60.  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  61.  
  62.  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Window_Main"
  63.  
  64.  Title="Staff Password Manager" Height="392.192" Width="523.256" ResizeMode="CanMinimize">
  65.  
  66.    <Grid Margin="0">
  67.        <Grid.ColumnDefinitions>
  68.            <ColumnDefinition/>
  69.        </Grid.ColumnDefinitions>
  70.  
  71.         <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="278" Margin="-2,0,0,-2" Stroke="Black" VerticalAlignment="Bottom" Width="236"/>
  72.  
  73.         <Button x:Name="Button_LookupID" Content="Lookup ID" HorizontalAlignment="Left" Margin="129,44,0,0" VerticalAlignment="Top" Width="75" IsDefault="True" TabIndex="1"/>
  74.         <TextBox x:Name="TextBox_EnterID" HorizontalAlignment="Left" Height="20" Margin="10,44,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="105" TabIndex="0"/>
  75.         <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"/>
  76.         <TextBlock x:Name="TextBlock_NameTitle" HorizontalAlignment="Left" Height="19" Margin="236,20,0,0" TextWrapping="Wrap" Text="Legal Name" VerticalAlignment="Top" Width="81"/>
  77.         <TextBox x:Name="TextBox_Name" HorizontalAlignment="Left" Height="20" Margin="236,44,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="249" IsTabStop="False" IsEnabled="False"/>
  78.         <TextBlock HorizontalAlignment="Left" Height="16" Margin="15,93,0,0" TextWrapping="Wrap" Text="Date Password Was Last Set" VerticalAlignment="Top" Width="151"/>
  79.         <TextBox x:Name="TextBox_PasswordLastSet" HorizontalAlignment="Left" Height="19" Margin="15,114,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsEnabled="False"/>
  80.         <TextBlock HorizontalAlignment="Left" Height="16" Margin="15,138,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="141" Text="Next Reset Time&#xA;"/>
  81.         <TextBox x:Name="TextBox_PasswordNextSet" HorizontalAlignment="Left" Height="19" Margin="15,156,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsEnabled="False"/>
  82.         <TextBlock HorizontalAlignment="Left" Height="16" Margin="15,261,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="98" Text="Lockout Status"/>
  83.         <TextBox x:Name="TextBox_LastLoginDate" HorizontalAlignment="Left" Height="19" Margin="15,197,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsEnabled="False"/>
  84.         <TextBlock HorizontalAlignment="Left" Height="16" Margin="15,180,0,0" TextWrapping="Wrap" Text="Last Login Date" VerticalAlignment="Top" Width="117"/>
  85.         <TextBox x:Name="TextBox_UserLockoutStatus" HorizontalAlignment="Left" Height="19" Margin="15,279,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsEnabled="False"/>
  86.         <Rectangle Fill="#FFF4F4F5" Height="278" Margin="0,0,-2,-2" Stroke="Black" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="285"/>
  87.         <TextBox x:Name="TextBox_ResetPassword" HorizontalAlignment="Left" Height="19" TextWrapping="Wrap" VerticalAlignment="Top" Width="143" Margin="305,197,0,0" IsEnabled="False"/>
  88.         <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"/>
  89.         <TextBlock HorizontalAlignment="Left" Height="16" Margin="305,183,0,0" TextWrapping="Wrap" Text="New Password" VerticalAlignment="Top" Width="143" TextAlignment="Center"/>
  90.         <TextBlock HorizontalAlignment="Left" Height="17" Margin="277,239,0,0" TextWrapping="Wrap" Text="Errors / Notifications" VerticalAlignment="Top" Width="208" TextAlignment="Center"/>
  91.         <TextBox x:Name="TextBox_ErrorNotification" HorizontalAlignment="Right" Height="77" Margin="0,269,17,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="245" IsEnabled="False"/>
  92.         <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"/>
  93.         <TextBlock HorizontalAlignment="Left" Height="16" Margin="15,221,0,0" TextWrapping="Wrap" Text="User is Enabled?" VerticalAlignment="Top" Width="97"/>
  94.         <TextBox x:Name="TextBox_DisabledStatus" HorizontalAlignment="Left" Height="17" Margin="15,239,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsEnabled="False"/>
  95.         <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>
  96.         <TextBox x:Name="TextBox_UserName" HorizontalAlignment="Left" Height="19" Margin="15,327,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" IsTabStop="False" IsEnabled="False"/>
  97.  
  98.     </Grid>
  99. </Window>
  100. "@
  101.  
  102. $reader = (New-Object System.Xml.XmlNodeReader $xaml)
  103. $window = [Windows.Markup.XamlReader]::Load($reader)
  104. ##########################End of Code for GUI#####################
  105.  
  106. #########################################Powershell Code goes here
  107.  
  108. #Hide the console window from the user.
  109. Hide-Console
  110.  
  111. #Feed TextBox and Buttons from the GUI into the script.
  112. #TextBoxes
  113. $TextBox_EnterID = $Window.FindName("TextBox_EnterID")
  114. $TextBox_Name = $Window.FindName("TextBox_Name")
  115. $TextBox_PasswordLastSet = $Window.FindName("TextBox_PasswordLastSet")
  116. $TextBox_PasswordNextSet = $Window.FindName("TextBox_PasswordNextSet")
  117. $TextBox_UserLockoutStatus = $Window.FindName("TextBox_UserLockoutStatus")
  118. $TextBox_LastLoginDate = $Window.FindName("TextBox_LastLoginDate")
  119. $TextBox_ResetPassword = $Window.FindName("TextBox_ResetPassword")
  120. $TextBox_ErrorNotification = $Window.FindName("TextBox_ErrorNotification")
  121. $TextBox_DisabledStatus = $Window.FindName("TextBox_DisabledStatus")
  122. $TextBox_UserName = $Window.FindName("TextBox_UserName")
  123.  
  124. #Buttons
  125. $Button_ResetUserPassword = $Window.FindName("Button_ResetUserPassword")
  126. $Button_UnlockUserAccount = $Window.FindName("Button_UnlockUserAccount")
  127. $Button_LookupID = $Window.FindName("Button_LookupID")
  128.  
  129. #Actions if the lookup ID button is clicked.
  130. $Button_LookupID.Add_Click({
  131. #Clear all text fields.
  132. $Global:TextBox_Name.Text = " "
  133. $Global:TextBox_PasswordLastSet.Text = " "
  134. $Global:TextBox_PasswordNextSet.Text = " "
  135. $TextBox_UserLockoutStatus.Text = " "
  136. $TextBox_LastLoginDate.Text = " "
  137. $TextBox_ResetPassword.Text = " "
  138. $TextBox_ErrorNotification.Text = " "
  139. $TextBox_UserName.Text = " "
  140.  
  141. $ID = $TextBox_EnterID.Text
  142. If ($ID.Length -Eq "5" -And $ID -Match '^\d+$') {LookupID}
  143. Else {[System.Windows.MessageBox]::Show("The ID number must be 5 numbers.")}
  144. })
  145.  
  146. #Actions if the reset password button is clicked.
  147. $Button_ResetUserPassword.Add_Click({
  148. If ($TextBox_Name.Text -Eq "User does not exist") {
  149. $TextBox_ResetPassword.Text = "Password cannot be set."
  150. }
  151. Else {
  152.    #Generate Randome password from Illa's list.
  153.    $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")
  154.    $Suffix = Get-Random -Maximum 9999
  155.    $Suffix = $Suffix.ToString("0000")
  156.    $Prefix = $Word[(Get-Random -Maximum $Word.Count)]
  157.    $TempPassword = $Prefix+$Suffix
  158.    $Identity = $Global:User.SamAccountName
  159.  
  160.    #Capture output from resetting password to show as an error or sucess.
  161.    $PassowrdSetError = $($OutputOnly= .{
  162.    Set-ADAccountPassword -Identity $Global:User.SamAccountName -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$TempPassword" -Force)
  163.    Set-ADUser -ChangePasswordAtLogon $True -Identity $Global:User.SamAccountName -Confirm:$False -Verbose
  164.    If ($Global:UserInfo.LockedOut -EQ "True") {Unlock-ADAccount -Identity $Global:User.SamAccountName}
  165.    Else {}
  166.    
  167.    }) 4>&1
  168.  
  169.    #Present password to GUI
  170.    $TextBox_ResetPassword.Text = $TempPassword
  171.    $TextBox_ErrorNotification.Text = "$PassowrdSetError"
  172.    #Unlock user account if required.
  173.  
  174.    }
  175. })
  176.  
  177. #Actions if Unlock User Button is clicked.
  178. $Button_UnlockUserAccount.Add_Click({
  179. $PassowrdSetError = $($OutputOnly= .{
  180. If (($Global:User)) {Unlock-ADAccount -Identity $Global:User.SamAccountName}
  181. Else {}
  182. }) 4>&1
  183. $TextBox_ErrorNotification.Text = "$PassowrdSetError"
  184. $Global:UserInfo = Get-ADUser $Global:User -properties *
  185. $TextBox_UserLockoutStatus.Text = "$Global:UserInfo.LockedOut"
  186. })
  187.  
  188.  
  189.  
  190. #Get OS version
  191. $OS = (Get-WmiObject Win32_OperatingSystem).Caption
  192.  
  193. #Determine OS and exit if unsupported.
  194. If ($OS -Like "*Windows 10*") {$OSVersion = "10"}
  195. ElseIf ($OS -Like "*Windows 7*") {$OSVersion = "7"}
  196. #Exit if not Windows 10 or 7
  197. Else {[System.Windows.MessageBox]::Show("Unable to determine operation system, or Operating system not supported."); Exit}
  198.  
  199. #Make sure this isn't being run on a student computers
  200. If ($Env:Computername -Like "*STA*" -And $Env:Computername -NotLike "*STU*" -And $Env:Computername -NotLike "*GIS*" -And $Env:Computername -NotLike "*SPED*") {}
  201. #Scold user and exit if not run on a staff computer.
  202. Else {[System.Windows.MessageBox]::Show("I refuse to run on a student computer. Try again on a staff computer.", "Error");Exit}
  203.  
  204. #Verify ADUC is installed.
  205. $ADUC = test-path "C:\windows\system32\dsa.msc"
  206. If ($ADUC-eq "True") {}
  207. #If ADUC is not installed prompt user for installation.
  208. Else {
  209. $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)
  210.    
  211.     #If the user wants to install ADUC 
  212.    If ($InstallADUC -Eq "Yes") {
  213.             $BundleList = zac bl
  214.             If ($BundleList -Like "*Remote Server Admin Tools for*") {zac bin "Remote Server Admin Tools for Windows $OSVersion"; Restart-Computer -Force}
  215.             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}
  216.     }      
  217.     #If the user didn't want to install ADUC
  218.     Elseif ($InstallADUC -Eq "No") {[System.Windows.Forms.MessageBox]::Show("Later brah." , "Byeeee"); Exit}
  219.     Elseif ($InstallADUC -Eq "Cancel") {[System.Windows.Forms.MessageBox]::Show("Later brah." , "Byeeee"); Exit}
  220.     Else {[System.Windows.Forms.MessageBox]::Show("Later brah." , "Byeeee"); Exit}
  221.     }
  222.  
  223. $window.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement