Графический интерфейс Powershell, вызов определенного свойства из Active Directory из ListView - PullRequest
0 голосов
/ 11 октября 2018

Я создаю графический интерфейс PowerShell, который я использую для удаления компьютеров из Active Directory.

Мне удалось получить представление списка для заполнения имени компьютера, пути OU и IP-адреса.

Однако, когда я создаю сценарий для удаления объекта, я сталкиваюсь свопрос.

Я бы хотел, чтобы пользователь мог выбрать компьютерный объект в представлении списка и нажать клавишу удаления.

Однако при использовании SelectedItems можно получить все 3 свойства (имя, путь OU и IP-адрес).

Вывод: @ {Имя = КОМПЬЮТЕР;DistinguishedName = CN = хх, НУ = хх, OU = хх, OU = хх, OU = хх, НУ = хх, DC = XX, DC = хх, DC = хх;IPv4Address = xx.xx.xx.xx}

Как это сделать, чтобы SelectedItems захватывал только имя компьютера, чтобы я мог передать его в переменную, которую можно использовать с Remove-ADComputer?

Я прошу прощения за любые вопросы форматирования.Я очень новичок в этом.

XML:

<Window x:Class="SCCMAD.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:SCCMAD"
        mc:Ignorable="d"
        Title="SCCMAD" Height="353.176" Width="586.987">
    <Grid Margin="0,0,2,-31" HorizontalAlignment="Right" Width="577" Height="353" VerticalAlignment="Bottom">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBox x:Name="Input" HorizontalAlignment="Left" Height="23" Margin="50,86,129,0" TextWrapping="Wrap" Text="Type Device Name here..." VerticalAlignment="Top" Width="398" Grid.ColumnSpan="2" RenderTransformOrigin="0.501,0.522"/>
        <Label x:Name="secondTitle" Content="Tool used for removal of devices from SCCM and/or AD" HorizontalAlignment="Left" Margin="130,34,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="324" FontWeight="Bold"/>
        <ListView x:Name="Grid" Margin="49,114,46,133" Grid.ColumnSpan="2">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" DisplayMemberBinding ="{Binding Name}" Width="120"/>
                    <GridViewColumn Header="Distinguished Name" DisplayMemberBinding ="{Binding DistinguishedName}" Width="120"/>
                    <GridViewColumn Header="IP Address" DisplayMemberBinding ="{Binding IPv4Address}" Width="120"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button x:Name="OK" Content="OK" Grid.Column="1" HorizontalAlignment="Left" Margin="163,86,0,0" VerticalAlignment="Top" Width="79" Height="23"/>
        <Button x:Name="SCCM" Content="SCCM" HorizontalAlignment="Left" Margin="130,256,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="Both" Grid.ColumnSpan="2" Content="Both" HorizontalAlignment="Left" Margin="254,256,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="AD" Content="AD" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Grid.Column="1" Margin="89,256,0,0"/>
        <Label Content="Remove from:" HorizontalAlignment="Left" Margin="242,225,0,0" VerticalAlignment="Top" Width="95" Grid.ColumnSpan="2" FontWeight="Bold"/>
        <StatusBar x:Name="StatusBar" HorizontalAlignment="Left" Height="30" Margin="0,290,0,0" VerticalAlignment="Top" Width="577" Grid.ColumnSpan="2"/>

    </Grid>
</Window>

Powershell:

Function Get-ADDevice {
param($computername =$env:COMPUTERNAME) 
    Get-ADComputer -Filter "name -like '$computername*'" -Properties Name, DistinguishedName, IPv4Address | Select-Object Name, DistinguishedName, IPv4Address
}
$WPFOK.Add_Click({
    try
    {
        $WPFGrid.Clear()
        Get-ADDevice -computername $WPFInput.Text | % {$WPFGrid.AddChild($_)}
    }
    catch
    {
        $WPFStatusBar.Text = $_.Exception.Message
    }
}) 
$WPFAD.Add_Click({
    foreach($item in $WPFGrid.SelectedItems)
    { 
        Write-Host $item
    }
})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...