【问题标题】:PowerShell Form - ListView Group displays properly in ISE but not in plain PowerShell promptPowerShell 表单 - ListView 组在 ISE 中正确显示,但在普通 PowerShell 提示符中不正确显示
【发布时间】:2017-10-10 11:02:33
【问题描述】:

我正在尝试编写一个脚本,以在列表视图中显示已过期和禁用的 Active Directory 用户。我希望列表视图按禁用和过期帐户对帐户进行分组。当我在 ISE 中运行脚本时,它会正确显示:

当我在正常的 PowerShell 提示符下运行脚本时,分组不起作用:

我在 Windows 10 1703 上运行 PowerShell 5。这是我的代码,我做错了什么?

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

【问题讨论】:

  • ise 和普通 powershell 有两个不同的环境。例如,两者都使用不同的配置文件。当你使用时,它会加载 Microsoft.PowerShellISE_profile.ps1,当你使用 powershell 时,它会加载 Microsoft.PowerShell_profile.ps1
  • powershell.exe -noexit -file c:\yourpowershell.ps1 的结果可能是什么?
  • @Aravinda,不幸的是完全一样,分组仍然不起作用。

标签: powershell


【解决方案1】:

如果您仔细观察布局,您会看到控件之间的更多差异(例如阴影)。
我相信您的问题是由于默认情况下在 ISE 下 Visual Stylesenabled 引起的。见:Windows Forms look different in PowerShell and Powershell ISE. Why?

所以,我想解决方案是在您的 cmdlet 中设置 [Windows.Forms.Application]::EnableVisualStyles()

【讨论】:

  • 我只是在发布之前进行了测试,但你打败了我,哈哈,是的,这正是所需要的
猜你喜欢
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
相关资源
最近更新 更多