2017-10-10 12 views
1

リストビューで有効期限が切れて無効になっているActive Directoryユーザーを表示するスクリプトを作成しようとしています。私はリストビューに、無効になったアカウントと有効期限の切れたアカウントでアカウントをグループ化します。私はISEでスクリプトを実行すると、それが適切に表示されます。PowerShellフォーム - ListViewグループはISEでは正しく表示されますが、プレーンなPowerShellプロンプトでは表示されません。

Groups Work Properly

私はプロンプト通常のPowerShellでスクリプトを実行すると、グループ化は動作しません:

Groups Don't Work Properly

私は」 Windows 10でPowerShell 5を実行する1703.これは私のコードですが、どうしたのですか?

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) 
    [email protected]("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 
+0

iseと通常のpowershellには2つの異なる環境があります。たとえば、両方のプロファイルを使用します。iseはMicrosoft.PowerShellISE_profile.ps1をロードし、powershellを使用するとMicrosoft.PowerShell_profile.ps1をロードします。 – Aravinda

+0

powershell.exeの結果 - noexit-file c:\ yourpowershell.ps1? – Aravinda

+0

@Aravinda、ちょうど同じ残念ながら、グループ化はまだ機能しません。 – Norphus

答えて

1

レイアウトと密接に関連している場合は、コントロール(シャドウなど)の違いが表示されます。
あなたの問題は、ISE Visual Stylesがデフォルトでenabledであるという事実によって引き起こされると思います。参照してください:Windows Forms look different in PowerShell and Powershell ISE. Why?

したがって、解決策は、コマンドレットに[Windows.Forms.Application]::EnableVisualStyles()を設定することです。

+0

私はちょうど投稿する前にテストしていたが、あなたはそれに私を打つ笑はいこれは正確に何が必要です –

+0

それは、ありがとう、それを持っている。 – Norphus

関連する問題