e6171d70-d3d6-465b-9d2a-2e95422713df
1.2
Access
142
Account
213
param(
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
[System.Windows.Forms.ListView]$ListView,
[Parameter(Mandatory=$true)]
[int]$ColumnIndex,
[System.Windows.Forms.SortOrder]$SortOrder = 'None')
if(($ListView.Items.Count -eq 0) -or ($ColumnIndex -lt 0) -or ($ColumnIndex -ge $ListView.Columns.Count))
{
return;
}
#region Define ListViewItemComparer
try{
$local:type = [ListViewItemComparer]
}
catch{
Add-Type -ReferencedAssemblies ('System.Windows.Forms') -TypeDefinition @"
using System;
using System.Windows.Forms;
using System.Collections;
public class ListViewItemComparer : IComparer
{
public int column;
public SortOrder sortOrder;
public ListViewItemComparer()
{
column = 0;
sortOrder = SortOrder.Ascending;
}
public ListViewItemComparer(int column, SortOrder sort)
{
this.column = column;
sortOrder = sort;
}
public int Compare(object x, object y)
{
if(column >= ((ListViewItem)x).SubItems.Count)
return sortOrder == SortOrder.Ascending ? -1 : 1;
if(column >= ((ListViewItem)y).SubItems.Count)
return sortOrder == SortOrder.Ascending ? 1 : -1;
if(sortOrder == SortOrder.Ascending)
return String.Compare(((ListViewItem)x).SubItems[column].Text, ((ListViewItem)y).SubItems[column].Text);
else
return String.Compare(((ListViewItem)y).SubItems[column].Text, ((ListViewItem)x).SubItems[column].Text);
}
}
"@ | Out-Null
}
#endregion
if($ListView.Tag -is [ListViewItemComparer])
{
#Toggle the Sort Order
if($SortOrder -eq [System.Windows.Forms.SortOrder]::None)
{
if($ListView.Tag.column -eq $ColumnIndex -and $ListView.Tag.sortOrder -eq 'Ascending')
{
$ListView.Tag.sortOrder = 'Descending'
}
else
{
$ListView.Tag.sortOrder = 'Ascending'
}
}
else
{
$ListView.Tag.sortOrder = $SortOrder
}
$ListView.Tag.column = $ColumnIndex
$ListView.Sort()#Sort the items
}
else
{
if($Sort -eq [System.Windows.Forms.SortOrder]::None)
{
$Sort = [System.Windows.Forms.SortOrder]::Ascending
}
#Set to Tag because for some reason in PowerShell ListViewItemSorter prop returns null
$ListView.Tag = New-Object ListViewItemComparer ($ColumnIndex, $SortOrder)
$ListView.ListViewItemSorter = $ListView.Tag #Automatically sorts
}
}
function Add-ListViewItem
{
<#
.SYNOPSIS
Adds the item(s) to the ListView and stores the object in the ListViewItem's Tag property.
.DESCRIPTION
Adds the item(s) to the ListView and stores the object in the ListViewItem's Tag property.
.PARAMETER ListView
The ListView control to add the items to.
.PARAMETER Items
The object or objects you wish to load into the ListView's Items collection.
.PARAMETER ImageIndex
The index of a predefined image in the ListView's ImageList.
.PARAMETER SubItems
List of strings to add as Subitems.
.PARAMETER Group
The group to place the item(s) in.
.PARAMETER Clear
This switch clears the ListView's Items before adding the new item(s).
.EXAMPLE
Add-ListViewItem -ListView $listview1 -Items "Test" -Group $listview1.Groups[0] -ImageIndex 0 -SubItems "Installed"
#>
Param(
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
[System.Windows.Forms.ListView]$ListView,
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
$Items,
[int]$ImageIndex = -1,
[string[]]$SubItems,
[System.Windows.Forms.ListViewGroup]$Group,
[switch]$Clear)
if($Clear)
{
$ListView.Items.Clear();
}
if($Items -is [Array])
{
$ListView.BeginUpdate()
foreach ($item in $Items)
{
$listitem = $ListView.Items.Add($item.ToString(), $ImageIndex)
#Store the object in the Tag
$listitem.Tag = $item
if($SubItems -ne $null)
{
$listitem.SubItems.AddRange($SubItems)
}
if($Group -ne $null)
{
$listitem.Group = $Group
}
}
$ListView.EndUpdate()
}
else
{
#Add a new item to the ListView
$listitem = $ListView.Items.Add($Items.ToString(), $ImageIndex)
#Store the object in the Tag
$listitem.Tag = $Items
if($SubItems -ne $null)
{
$listitem.SubItems.AddRange($SubItems)
}
if($Group -ne $null)
{
$listitem.Group = $Group
}
}
}
#endregion
$buttonLoad_Click={
$items = get-acl c:\scripts | select -expand access |
select @{Name='Account';E={$_.identityreference}},@{N='Access';E={$_.FileSystemRights}}
foreach($itm in $items){
$lvitem=$listview1.Items.Add($itm.Account.ToString())
$lvitem.SubItems.Add($itm.Access.ToString())
}
}
]]>
1
System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\System.Windows.Forms.dll
System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a|C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\System.Drawing.dll
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll
System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll
System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.XML.dll
System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a|C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.DirectoryServices.dll
System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll
1
-1
0
2
False
0
1.0.0.0
1.0.0.0
Designed Systems & Services