【发布时间】:2018-02-14 20:46:46
【问题描述】:
我试图将以下查询的结果输出到两列中:
get-mailbox -identity *@$SearchDomain | where ismailboxenabled -eq true
这是我现在所做的:
$mailboxes = get-mailbox -identity *@$SearchDomain | where ismailboxenabled -eq true
foreach($line in $mailboxes){
$new = new-object System.Windows.Forms.ListViewItem($line.Name)
$new.SubItems.Add($line.Alias + "@" + $SearchDomain)
$WPFLstActiveMailboxes.Items.Add($new)
}
我的问题是我的 GUI 中显示的内容是这样的:
黑匣子后面是我想要的数据
编辑 1:
$inputXML = @"
<Window x:Name="TMS_MailboxToolkit" x:Class="TMS_MailboxToolki.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TMS"
mc:Ignorable="d"
Title="TMS - Mailbox Toolkit" Height="354.303" Width="527.152">
<Grid Margin="0,0,-8,-5">
<Button x:Name="BtnCalculate" Content="Calculate" HorizontalAlignment="Left" Margin="432,289,0,0" VerticalAlignment="Top" Width="75"/>
<Label x:Name="LblSearchDomain" Content="Search domain" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<TextBox x:Name="TxtSearchDomain" HorizontalAlignment="Left" Height="23" Margin="10,42,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="malicis.com"/>
<Label x:Name="LblActiveMailboxes" Content="Active mailboxes" HorizontalAlignment="Left" Margin="10,74,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<Label x:Name="LblCurrentCount" Content="Current count:" HorizontalAlignment="Left" Margin="10,286,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="TxtCurrentCount" HorizontalAlignment="Left" Height="23" Margin="101,290,0,0" TextWrapping="Wrap" Text="00000" VerticalAlignment="Top" Width="45" IsEnabled="False" FontWeight="Bold"/>
<ProgressBar x:Name="PrgStatus" HorizontalAlignment="Left" Height="10" Margin="345,70,0,0" VerticalAlignment="Top" Width="162"/>
<TextBox x:Name="TxtStatus" HorizontalAlignment="Left" Height="23" Margin="345,42,0,0" TextWrapping="Wrap" IsEnabled="False" Text="idle" VerticalAlignment="Top" Width="162" FontStyle="Italic"/>
<Label x:Name="LblStatus" Content="Status" HorizontalAlignment="Left" Margin="345,10,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<ListView x:Name="LstActiveMailboxes" HorizontalAlignment="Left" Height="181" Margin="10,100,0,0" VerticalAlignment="Top" Width="497">
<ListView.View>
<GridView>
<GridViewColumn Header="Disable">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="5, 0" IsChecked="False"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn >
<GridViewColumn Header="Name"/>
<GridViewColumn Header="Alias"/>
<GridViewColumn Header="Mailbox"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
"@
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged properties (PowerShell cannot process them)"
throw}
#===========================================================================
# Load XAML Objects In PowerShell
#===========================================================================
$xaml.SelectNodes("//*[@Name]") | %{"trying item $($_.Name)";
try {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop}
catch{throw}
}
Function Get-FormVariables{
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
}
Get-FormVariables
#===========================================================================
# Shows the form
#===========================================================================
Add-Type -AssemblyName System.Windows.Forms
Set-Variable -Name credsalreadyprovided -Value $false -Scope global
Set-Variable -Name CONFIG_SERVER -Value "exchpd01" -Scope global
$WPFBtnCalculate.Add_Click({
if ($credsalreadyprovided -eq $true){
calculate
}
else{
initiatesession
calculate
}
})
function initiatesession{
$WPFPrgStatus.Value = 0
Set-Variable -Name cred -Value (get-credential) -Scope global
$credsalreadyprovided = $true
$WPFPrgStatus.Value = 25
$WPFTxtStatus.Text = "Establishing session with " + $CONFIG_SERVER + "..."
Set-Variable -Name session -Value (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$CONFIG_SERVER/PowerShell/ -credential $cred) -Scope global
$WPFPrgStatus.Value = 50
$WPFTxtStatus.Text = "Importing session..."
Import-PSSession $session -AllowClobber
}
function calculate{
$WPFPrgStatus.Value = 100
$WPFTxtStatus.Text = "Querying " + $CONFIG_SERVER + "..."
$SearchDomain=$WPFTxtSearchDomain.Text
$WPFTxtCurrentCount.Text = (get-Mailbox -identity *@$SearchDomain | where ismailboxenabled -eq true).count
#$mailboxes = get-mailbox -identity *@$SearchDomain | where ismailboxenabled -eq true
get-mailbox -identity *@$SearchDomain | where ismailboxenabled -eq true | ForEach-Object{
$name = $_.Name
$entry = New-Object System.Windows.Controls.ListViewItem($name)
$alias = $_.Alias
$entry.SubItems.Add($alias)
$mailbox = $_.Alias + "@" + $SearchDomain
$entry.SubItems.Add($mailbox)
$WPFLstActiveMailboxes.Items.Add($entry)
}
}
$Form.ShowDialog() | out-null
【问题讨论】:
-
ListViewItem 是否是您可以访问的属性 - 即
$WPFLstActiveMailboxes.Items.Add($new.ListViewItem) -
这是 WPF 吗?还是 WinForms?您正在创建一个 winform ListViewItem。 WPF 将是 System.Windows.Controls.ListViewItem。
-
是的,它是 WPF,我的错。我仍然无法正确显示结果,请参阅我的编辑 1。
标签: wpf powershell listview listviewitem