2

I am trying to write a script which displays Active Directory users which are expired and disabled in a listview. I want the listview to group the accounts by disabled and expired accounts. When I run the script in the ISE, it displays properly:

Groups Work Properly

When I run the script in a normal PowerShell prompt, grouping doesn't work:

Groups Don't Work Properly

I'm running PowerShell 5 on Windows 10 1703. This is my code, what have I done wrong?

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()

    }

ViewAllInactiveUsers
Norphus
  • 23
  • 6
  • ise and normal powershell has two different environments.. for example both use different profiles .. when you use , ise it loads Microsoft.PowerShellISE_profile.ps1 and when you use powershell it loads Microsoft.PowerShell_profile.ps1 – Aravinda Oct 10 '17 at 11:23
  • what could be the result for powershell.exe -noexit -file c:\yourpowershell.ps1 ? – Aravinda Oct 10 '17 at 11:34
  • @Aravinda, exactly the same unfortunately, grouping still doesn't work. – Norphus Oct 10 '17 at 11:39

1 Answers1

2

If you closely to the layout you will see more differences between the controls (such as shadows).
I believe your issue is caused by the fact that under the ISE Visual Styles are enabled by default. See: Windows Forms look different in PowerShell and Powershell ISE. Why?

So, I guess the solution is to set [Windows.Forms.Application]::EnableVisualStyles() in your cmdlet.

iRon
  • 20,463
  • 10
  • 53
  • 79