Powershell WPF文本框放入数组

时间:2018-12-19 18:38:00

标签: wpf powershell xaml

我有一个WPF表单,该表单打开一个文本框供用户输入,用于查找PC​​名称的行分隔列表。无论我如何尝试将它们分开,所有值最终都作为一个项目返回,肯定是一个多行项目,但只有一个。我无法一次将它们排入阵列。 这是XAML,以及我通过多次尝试将其放入我的阵列中尝试的一些方法。请注意,尽管我阅读了textbox.lines,但似乎未返回任何内容,可以满足我的需要。

$inputXML = @"
<Window x:Name="Add_to_AD_Group" x:Class="Form.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:MSIForm"
        mc:Ignorable="d"
        Title="Add to AD for SCCM Deployment" Height="327.57" Width="283.937">
    <Grid RenderTransformOrigin="0.511,0.499">
        <TextBox x:Name="MachinesList" HorizontalAlignment="Left" Height="155" Margin="62,100,0,0" VerticalAlignment="Top" Width="163" AcceptsReturn="True" VerticalScrollBarVisibility="Auto"/>
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Enter Machine list below" VerticalAlignment="Top" Margin="62,73,0,0" Height="22" Width="163"/>
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Select AD Group" VerticalAlignment="Top" Margin="62,10,0,0" Height="24" Width="163"/>
        <ComboBox x:Name="ADGroupsList" HorizontalAlignment="Left" Margin="62,39,0,0" VerticalAlignment="Top" Width="163" IsEditable="True"/>
        <Button x:Name="OK" Content="OK" HorizontalAlignment="Left" Margin="46,268,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="Cancel" Content="Cancel" HorizontalAlignment="Left" Margin="161,268,0,0" VerticalAlignment="Top" Width="75" IsCancel="True"/>
    </Grid>
</Window>
"@

$TempVar = $WPFMachinesList.Text
$TempVar = $TempVar.Trim()
# = $TempVar.Split([Environment]::NewLine)
#, [StringSplitOptions]::RemoveEmptyEntries
#foreach ($item in $TempVar) {$Machinearray += $item}
#$TempVar = ($TempVar -split '/r?/n').Trim()
Out-GridView -InputObject $TempVar -Title "WPF Machines List"

Out-GridView显示如下内容: Out-Gridview test

感谢您的帮助!

更新:我正在尝试从文本框中获取机器列表,以针对每个机器名称通过一组命令运行,我尝试了各种字符串操作,并且无法将列表作为单个项返回,因此文本框中的单个项目。对于我的XAML,我使用VS来获取此xaml代码,并使用powershell对其进行启动和修改。例如。我有Powershell从OU获取组的列表,并使用它来填充组合框,然后将计算机列表添加到所选的组,因此xaml代码本身没有其他功能。如果可以帮助确定我做错了什么,这里是完整的xaml功能。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("presentationframework")

Function Get-MachineList {

$inputXML = @"
<Window x:Name="Add_to_AD_for_SCCM_Deployment" x:Class="Form.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:MSIForm"
        mc:Ignorable="d"
        Title="Add to AD for SCCM Deployment" Height="327.57" Width="283.937">
    <Grid RenderTransformOrigin="0.511,0.499">
        <TextBox x:Name="MachinesList" HorizontalAlignment="Left" Height="155" Margin="62,100,0,0" VerticalAlignment="Top" Width="163" AcceptsReturn="True" VerticalScrollBarVisibility="Auto"/>
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Enter Machine list below" VerticalAlignment="Top" Margin="62,73,0,0" Height="22" Width="163"/>
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Select AD Group" VerticalAlignment="Top" Margin="62,10,0,0" Height="24" Width="163"/>
        <ComboBox x:Name="ADGroupsList" HorizontalAlignment="Left" Margin="62,39,0,0" VerticalAlignment="Top" Width="163" IsEditable="True"/>
        <Button x:Name="OK" Content="OK" HorizontalAlignment="Left" Margin="46,268,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="Cancel" Content="Cancel" HorizontalAlignment="Left" Margin="161,268,0,0" VerticalAlignment="Top" Width="75" IsCancel="True"/>
    </Grid>
</Window>
"@
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace "x:C", "C" -replace '^<Win.*', '<Window' 


[xml]$XAML = $inputXML
#Read XAML

    $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
    try{$Form=[Windows.Markup.XamlReader]::Load($reader)}
    catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
    Write-Host $_.Exception.Message;exit}

#===========================================================================
# Load XAML Objects In PowerShell
#===========================================================================

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}


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{
}

#===========================================================================
# Actually make the objects work
#===========================================================================
$Script:GroupsList = Get-ADObject -Filter 'ObjectClass -eq "group"' -SearchBase "OU=Applications,OU=Groups,OU=AAM-Enterprise,DC=AAM,DC=NET" -Credential $creds | sort

foreach ($group in $GroupsList) {$WPFADGroupsList.Items.Add($group.Name)}

    $WPFOK.Add_Click{
        $TempVar = ($WPFMachinesList.Text).Trim()
        #$TempVar = @(($TempVar.Trim()) -split "`r`n,")
        # = $TempVar.Split([Environment]::NewLine)
        #, [StringSplitOptions]::RemoveEmptyEntries
        #foreach ($item in $TempVar) {$Machinearray += $item}
        Out-GridView -InputObject $TempVar -Title "WPF Machines List"
        #foreach ($machine in $TempVar) {
            #$line = $line | Out-String
            #$MachineArray = @()
            #$MachineArray += $($line)}
            #VerifySCCMClient $machine
            #Get-SelectedGroup
        #}
    }
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
        {
            $Error.Clear()
            $ADSCCMForm.Close()
        }
    }
    )
#Sample entry of how to add data to a field
#$vmpicklistView.items.Add([pscustomobject]@{'VMName'=($_).Name;Status=$_.Status;Other="Yes"})

#===========================================================================
# Shows the form
#===========================================================================
Write-Host "To show the form, run the following" -ForegroundColor Cyan
$Form.ShowDialog() | Out-Null
}

Get-MachineList

1 个答案:

答案 0 :(得分:0)

您拥有所需的一切:

  1. 修剪后的字符串

  2. [environment]::NewLine的拆分

  3. [System.StringSplitOptions]"RemoveEmptyEntries"摆脱空白行。

将其放在一起,该行如下所示:

$TempVar = ($WPFMachinesList.Text.Trim()).split([environment]::NewLine,[System.StringSplitOptions]"RemoveEmptyEntries")

这将从您在文本框中输入的行创建一个数组。