Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

PowerShell Form displays properly in ISE but not the regular PowerShell prompt


Recommended Posts

Posted

I'm at something of a loss here.

 

I'm writing a PowerShell form which displays a subset of AD users which are either disabled or expired. The form has a listview, the listview is supposed to group them according to whether they're disabled or expired. When I run the script in the ISE, it works. When I run it in a regular PowerShell prompt, the users appear in the right order but they're not grouped.

 

This is the form running in the ISE, this is what it's supposed to look like:

attachment.php?attachmentid=45710&d=1507634219

 

This is the form running in a normal PowerShell prompt:

attachment.php?attachmentid=45711&d=1507634244

 

This is my code:

    Function ViewAllInactiveUsers {
       $ViewAllInactiveUsersForm = New-Object system.Windows.Forms.Form
       $ViewAllInactiveUsersForm.Text = "View All Inactive Users"
       $ViewAllInactiveUsersForm.TopMost = $false
       $ViewAllInactiveUsersForm.Width = 900
       $ViewAllInactiveUsersForm.Height = 700
       $ViewAllInactiveUsersForm.MinimizeBox = $false
       $ViewAllInactiveUsersForm.MaximizeBox = $false
       $ViewAllInactiveUsersForm.FormBorderStyle = "FixedDialog"

    $InactiveListView_DoubleClick={
        $InactiveUserTextBox.text = $InactiveListView.SelectedItems[0].SubItems[0].Text

    }
   
       #Define Header
       $InactiveImage1 = [system.Drawing.Image]::FromFile("c:\Users\i.north\Documents\CWP\habs.png")
   
       $InactiveImage = New-Object System.Windows.Forms.PictureBox
       $InactiveImage.Width = $InactiveImage1.Size.Width
       $InactiveImage.Height = $InactiveImage1.Size.Height
       $InactiveImage.location = New-Object System.Drawing.Size(20,0)
       $InactiveImage.image = $InactiveImage1

       $ViewAllInactiveUsersForm.Controls.Add($InactiveImage)

       $InactiveTitleFont = New-Object System.Drawing.Font("Calibri",20,[system.drawing.fontstyle]::Regular)
       $InactiveTitleText = New-Object System.Windows.Forms.Label
       $InactiveTitleText.Font = $TitleFont
       $InactiveTitleText.Text = "View All Inactive Guests"
       $InactiveTitleText.Location = New-Object System.Drawing.Size(330,20)
       $InactiveTitleText.Size = New-Object System.Drawing.Size(400,100)
       $ViewAllInactiveUsersForm.Controls.Add($InactiveTitleText)
   
       #List box with Inactive users in
       $InactiveListView = New-Object system.windows.Forms.ListView
 
       $InactiveListView.Width = 840
       $InactiveListView.Height = 300
       $InactiveListView.location = new-object system.drawing.point(20,139)
       $InactiveListView.View = [system.Windows.Forms.View]::Details
       $InactiveListView.HeaderStyle = "nonclickable"
       $InactiveListView.FullRowSelect = $true
       $InactiveListView.add_SelectedIndexChanged({
    
        If ($InactiveListView.Items.Count -gt 0)
        {
            $InactiveExtendButton.Enabled = $true
               $InactiveDisableButton.Enabled = $true
               $InactiveResetButton.Enabled = $true
               $InactiveUserTextBox.Enabled = $true
               $InactiveDurationBox.Enabled = $true
               $InactiveUserTextBox.BackColor = "#ffffff"
        }
        Else
        {
            $InactiveExtendButton.Enabled = $false
               $InactiveDisableButton.Enabled = $false
               $InactiveResetButton.Enabled = $false
        }
    
    })
       $InactiveListView.add_Click({$InactiveUserTextBox.text = $InactiveListView.SelectedItems[0].SubItems[0].Text})

       $ViewAllInactiveUsersForm.controls.Add($InactiveListView)

       $InactiveListView.Columns.Add("Guest User Name", -2)
       $InactiveListView.Columns.Add("Guest Forename", -2)
       $InactiveListView.Columns.Add("Guest Surname", -2)
       $InactiveListView.Columns.Add("Guest Company", 250)
       $InactiveListView.Columns.Add("Guest Sponsor", -2)
       $InactiveListView.Columns.Add("Account Status", -2 )
       $InactiveListView.Columns.Add("Account Expiry Time", -2)
   
       $InactiveListViewDisabledGroup = New-Object System.Windows.Forms.ListViewGroup
       $InactiveListViewDisabledGroup.Header = "Disabled Accounts"
       $InactiveListView.Groups.Add($InactiveListViewDisabledGroup)

       $InactiveListViewExpiredGroup = New-Object System.Windows.Forms.ListViewGroup
       $InactiveListViewExpiredGroup.Header = "Expired Accounts"
       $InactiveListView.Groups.Add($InactiveListViewExpiredGroup)
       $InactiveListView.ShowGroups = $true

       #Get all Inactive guest AD users and put them into the list view
   
       $GuestUsersExpired = get-aduser -filter {enabled -eq $true} -searchbase "ou=Guest Wifi Users,ou=Guest Wifi,dc=hahc,dc=internal"  -Properties displayname,company,mail,officephone,description,office,accountexpires,AccountExpirationDate | Where-Object { $_.AccountExpirationDate -ne $null -and $_.AccountExpirationDate -lt (Get-Date) }
       $GuestUsersExpired | ForEach-Object {
               $Sponsor = $_.description
               $SponsorSplit = $Sponsor.IndexOf("-")
               $InactiveListViewItem = New-Object System.Windows.Forms.ListViewItem($_.SamAccountName)
               $InactiveListViewItem.SubItems.Add($_.GivenName)
               $InactiveListViewItem.SubItems.Add($_.Surname)
               $InactiveListViewItem.SubItems.Add($_.Company)
               $InactiveListViewItem.SubItems.Add($Sponsor.substring($SponsorSplit+2))
               $InactiveListViewItem.SubItems.Add("Expired")
               $InactiveListViewItem.SubItems.Add("$(Get-Date([datetime]::FromFileTime($_.AccountExpires)) -Format "ddd d MMM yyyy, HH:mm")")
               $InactiveListViewItem.Group = $InactiveListViewExpiredGroup
               $InactiveListView.Items.Add($InactiveListViewItem)
           
       }

       $GuestUsersDisabled = get-aduser -filter {enabled -eq $false} -searchbase "ou=Guest Wifi Users,ou=Guest Wifi,dc=hahc,dc=internal"  -Properties displayname,company,mail,officephone,description,office,accountexpires,AccountExpirationDate 
       $GuestUsersDisabled | ForEach-Object {
               $Sponsor = $_.description
               $SponsorSplit = $Sponsor.IndexOf("-")
               $InactiveListViewItem = New-Object System.Windows.Forms.ListViewItem($_.SamAccountName)
               $InactiveListViewItem.SubItems.Add($_.GivenName)
               $InactiveListViewItem.SubItems.Add($_.Surname)
               $InactiveListViewItem.SubItems.Add($_.Company)
               $InactiveListViewItem.SubItems.Add($Sponsor.substring($SponsorSplit+2))
               $InactiveListViewItem.SubItems.Add("Disabled")
               $InactiveListViewItem.SubItems.Add("$(Get-Date([datetime]::FromFileTime($_.AccountExpires)) -Format "ddd d MMM yyyy, HH:mm")")
               $InactiveListViewItem.Group = $InactiveListViewDisabledGroup
               $InactiveListView.Items.Add($InactiveListViewItem)
           
       }
               

       #Extend/Reset/Delete User area

       #Put it all into a groupbox, make it look a little neater
       $InactiveUserAreaGroupBox = New-Object System.Windows.Forms.GroupBox
       $InactiveUserAreaGroupBox.Location = New-Object System.Drawing.Size(20,460)
       $InactiveUserAreaGroupBox.Height = 135
       $InactiveUserAreaGroupBox.Width = 840
       $InactiveUserAreaGroupBox.Text = "User Control Area"
       $ViewAllInactiveUsersForm.Controls.Add($InactiveUserAreaGroupBox)

       #Label
       $InactiveUserLabel = New-Object System.Windows.Forms.Label
       $InactiveUserLabel.Location = New-Object System.Drawing.Size(20,20)
       $InactiveUserLabel.Size = New-Object System.Drawing.Size(110,50)
       $InactiveUserLabel.Text = "Guest UserName"
       $InactiveUserAreaGroupBox.Controls.Add($InactiveUserLabel)
   
       #TextBox
       $InactiveUserTextBox = New-Object System.Windows.Forms.TextBox
       $InactiveUserTextBox.Location = New-Object System.Drawing.Size(130,20)
       $InactiveUserTextBox.Size = New-Object System.Drawing.Size(200,50)
       $InactiveUserTextBox.Enabled = $false
       $InactiveUserTextBox.ReadOnly = $true
       $InactiveUserAreaGroupBox.Controls.Add($InactiveUserTextBox)
   

       #disable user button
       $InactiveDisableButton = New-Object System.Windows.Forms.Button
       $InactiveDisableButton.Text = "Disable Guest Account"
       $InactiveDisableButton.Width = 150
       $InactiveDisableButton.Height = 40
       $InactiveDisableButton.Location = New-Object System.Drawing.Point(670,70)
       $InactiveDisableButton.Enabled = $false
       $InactiveDisableButton.add_click({
           Get-ADUser -Filter {name -eq $InactiveUserTextBox.Text} | Set-ADUser -Enabled $false
           [system.Windows.Forms.MessageBox]::Show("User $($InactiveUserTextBox.Text) has been disabled")
       
           $InactiveListView.Items.Remove($InactiveListView.SelectedItems[0])


           })
       $InactiveUserAreaGroupBox.Controls.Add($InactiveDisableButton)

       #reset password button
       $InactiveResetButton = New-Object System.Windows.Forms.Button
       $InactiveResetButton.Text = "Reset Guest Password"
       $InactiveResetButton.Width = 150
       $InactiveResetButton.Height = 40
       $InactiveResetButton.Location = New-Object System.Drawing.Point(505,70)
       $InactiveResetButton.Enabled = $false
       $InactiveResetButton.add_click({
           $Password = GenerateRandomishPassword
           $PasswordSecure = $Password | ConvertTo-SecureString -AsPlainText -Force
           $UserToCreate = $InactiveUserTextBox.Text
           Get-ADUser -Filter {name -eq $InactiveUserTextBox.Text } | Set-ADAccountPassword -NewPassword $PasswordSecure
       
           UserCreationResultsForm
           })
       $InactiveUserAreaGroupBox.Controls.Add($InactiveResetButton)

       #extend combobox
       $InactiveDurationLabel = New-Object System.Windows.Forms.Label
       $InactiveDurationLabel.Location = New-Object System.Drawing.Size(340,20)
       $InactiveDurationLabel.Size = New-Object System.Drawing.Size(150,40)
       $InactiveDurationLabel.Text = "Change Expiry Time to this amount of time from now:"
       $InactiveUserAreaGroupBox.Controls.Add($InactiveDurationLabel)



       $InactiveDurationBox = New-Object System.Windows.Forms.ComboBox
       $InactiveDurationBox.Location = New-Object System.Drawing.Size(505,20)
       $InactiveDurationBox.Size = New-Object System.Drawing.Size(150,40)
       $InactiveDurationBox.Text = "8 Hours"
       $InactiveDurationBox.Enabled = $false
       $InactiveDurationBox.DropDownStyle = [system.Windows.Forms.ComboBoxStyle]::DropDownList
       $InactiveUserAreaGroupBox.Controls.Add($InactiveDurationBox)
       $InactiveDurationOptions=@("1 Hour","4 Hours","8 Hours","1 Day","1 Week","1 Month")

       Foreach ($InactiveDuration in $InactiveDurationOptions) {
           [void] $InactiveDurationBox.items.add($InactiveDuration)
           }
       #extend button
       $InactiveExtendButton = New-Object System.Windows.Forms.Button
       $InactiveExtendButton.Text = "Extend Guest Account Expiry Time"
       $InactiveExtendButton.Width = 150
       $InactiveExtendButton.Height = 40
       $InactiveExtendButton.Location = New-Object System.Drawing.Point(670,20)
       $InactiveExtendButton.Enabled = $false
       $InactiveExtendButton.add_click({
           $DurationExpiryTime = $InactiveDurationBox.Text
               switch ($DurationExpiryTime) {
               "1 Hour" { $TS = New-TimeSpan -Hours 1 }
               "4 Hours" { $TS = New-TimeSpan -Hours 4 }
               "8 Hours" { $TS = New-TimeSpan -Hours 8 }
               "1 Day" { $TS = New-TimeSpan -Days 1 }
               "1 Week" { $TS = New-TimeSpan -Days 7 }
               "1 Month" { $TS = New-TimeSpan -Days 28 }
               Default { $TS = New-TimeSpan -hours 8 }
               }
           Get-ADUser -filter {name -eq $InactiveUserTextBox.Text } | Set-ADAccountExpiration -TimeSpan $TS
           [system.Windows.Forms.MessageBox]::Show("Expiration extended by $($InactiveDurationBox.Text)")
           })
       $InactiveUserAreaGroupBox.Controls.Add($InactiveExtendButton)

       #end of groupbox, everything outside of there now

       #Close Button
       $InactiveClose = New-Object system.windows.Forms.Button
       $InactiveClose.Text = "Close"
       $InactiveClose.Width = 150
       $InactiveClose.Height = 40
       $InactiveClose.location = new-object system.drawing.point(710,600)
       $InactiveClose.Add_Click({$ViewAllInactiveUsersForm.close()})
       $ViewAllInactiveUsersForm.controls.Add($InactiveClose)

       #Activate Form
       $ViewAllInactiveUsersForm.Add_Shown({$ViewAllInactiveUsersForm.activate()})
       [void]  $ViewAllInactiveUsersForm.ShowDialog()

       }
   Import-Module ActiveDirectory
   [void] [system.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
   [void] [system.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  
   ViewAllInactiveUsers

 

Any idea what I've done wrong? I'm running Windows 10 1703, PowerShell 5

ise.PNG

normalprompt.PNG

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...