如何使用Powershell

时间:2017-06-09 07:59:12

标签: wpf powershell xaml

我有一段使用XAML的Poweshell代码,而且我不得不重复一些XAML项目#n次。

$ domainlist通过CollectDomains函数填充,此函数从Excel工作表中提取两个项目(主机名和域名)。该函数返回一个唯一域名列表。

foreach ($domain in $domainlist) { Start function to Repeat XAML code and returm credential objects per unique domainname ... }

该行的代码是:

<Rectangle Fill="#FFF1F1F1" HorizontalAlignment="Left" Height="64" VerticalAlignment="Top" Width="997" Margin="6,162,0,0" Stroke="#FFCDCDCD"/>
<Label x:Name="LblDomain1" Content="Domain:" HorizontalAlignment="Left" Margin="14,166,0,0" VerticalAlignment="Top" FontSize="10" Height="49" Width="221"/>
<Label x:Name="LblUsername1" Content="Username:" HorizontalAlignment="Left" Margin="240,166,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
<TextBox x:Name="TxtUsername1" HorizontalAlignment="Left" Height="25" Margin="240,190,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/>
<Label x:Name="LblPassword1" Content="Password:" HorizontalAlignment="Left" Margin="433,165,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
<TextBox x:Name="TxtPassword1" HorizontalAlignment="Left" Height="25" Margin="433,190,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/>
<Label x:Name="LblReuseCreds1" Content="Use credentials from domain:" HorizontalAlignment="Left" Margin="626,166,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
<ComboBox x:Name="CmbReuseCreds1" HorizontalAlignment="Left" Margin="626,190,0,0" VerticalAlignment="Top" Width="188" Height="25"/>

我尝试将Xaml代码拆分为$ XamlHeader,$ XamlContent和$ XamlFooter,但这对我没有意义。在WPF GUI中使用XAML项重复行的最佳方法是什么?

我在下面创建了一个简短的示例脚本:

$GuiXaml = @'
    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            Height="800" Width="1024">
        <Grid>
            <Rectangle Fill="White" HorizontalAlignment="Left" Height="553" VerticalAlignment="Top" Width="1016" Margin="0,156,0,0" Stroke="#FFCCCCCC"/>
            <Rectangle Fill="#FF007ABE" HorizontalAlignment="Left" Height="80" VerticalAlignment="Top" Width="1016" Margin="0,-1,0,0"/>
            <Rectangle Fill="#FFF0F0F0" HorizontalAlignment="Left" Height="77" VerticalAlignment="Top" Width="1016" Margin="0,79,0,0"/>
            <Label x:Name="GuiScreenTitle" Content="Push script to Excel list servers" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="24" Width="347" Height="42" Margin="10,18,0,0" Foreground="White" FontWeight="SemiBold" HorizontalContentAlignment="Left" FontFamily="Segoe UI Semilight"/>
            <Button x:Name="BtnBrowseXlsx" Content="Browse" HorizontalAlignment="Left" Margin="10,90,0,0" VerticalAlignment="Top" Width="100" Height="25"/>
            <Label x:Name="LblBrowseXlsx" Content="Chose the Excel file to use" HorizontalAlignment="Left" Margin="115,90,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="225"/>
            <Label x:Name="LblLastXlsxFile" Content="Last used: ........\TestServers.xlsx" HorizontalAlignment="Left" Margin="339,90,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="655" FontWeight="Bold"/>
            <Button x:Name="BtnBrowseScript" Content="Browse" HorizontalAlignment="Left" Margin="10,120,0,0" VerticalAlignment="Top" Width="100" Height="25"/>
            <Label x:Name="LblBrowseScript" Content="Chose the script file to push to remotely execute" HorizontalAlignment="Left" Margin="115,120,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="225"/>
            <Label x:Name="LblLastPs1File" Content="Last used: ........\SCCMClientConnectivityTests.ps1" HorizontalAlignment="Left" Margin="339,120,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="655" FontWeight="Bold"/>
            <Rectangle Fill="#FFF1F1F1" HorizontalAlignment="Left" Height="64" VerticalAlignment="Top" Width="997" Margin="6,162,0,0" Stroke="#FFCDCDCD"/>
            <Label x:Name="LblDomain1" Content="Domain:" HorizontalAlignment="Left" Margin="14,166,0,0" VerticalAlignment="Top" FontSize="10" Height="49" Width="221"/>
            <Label x:Name="LblUsername1" Content="Username:" HorizontalAlignment="Left" Margin="240,166,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
            <TextBox x:Name="TxtUsername1" HorizontalAlignment="Left" Height="25" Margin="240,190,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/>
            <Label x:Name="LblPassword1" Content="Password:" HorizontalAlignment="Left" Margin="433,165,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
            <TextBox x:Name="TxtPassword1" HorizontalAlignment="Left" Height="25" Margin="433,190,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/>
            <Label x:Name="LblReuseCreds1" Content="Use credentials from domain:" HorizontalAlignment="Left" Margin="626,166,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
            <ComboBox x:Name="CmbReuseCreds1" HorizontalAlignment="Left" Margin="626,190,0,0" VerticalAlignment="Top" Width="188" Height="25"/>
            <Rectangle Fill="#FFF1F1F1" HorizontalAlignment="Left" Height="64" VerticalAlignment="Top" Width="997" Margin="6,231,0,0" Stroke="#FFCDCDCD"/>
            <Label x:Name="LblDomain2" Content="Domain:" HorizontalAlignment="Left" Margin="14,235,0,0" VerticalAlignment="Top" FontSize="10" Height="49" Width="221"/>
            <Label x:Name="LblUsername2" Content="Username:" HorizontalAlignment="Left" Margin="240,235,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
            <TextBox x:Name="TxtUsername2" HorizontalAlignment="Left" Height="25" Margin="240,259,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/>
            <Label x:Name="LblPassword2" Content="Password:" HorizontalAlignment="Left" Margin="433,234,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
            <TextBox x:Name="TxtPassword2" HorizontalAlignment="Left" Height="25" Margin="433,259,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/>
            <Label x:Name="LblReuseCreds2" Content="Use credentials from domain:" HorizontalAlignment="Left" Margin="626,235,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
            <ComboBox x:Name="CmbReuseCreds2" HorizontalAlignment="Left" Margin="626,259,0,0" VerticalAlignment="Top" Width="188" Height="25"/>
            <Rectangle Fill="#FFF0F0F0" HorizontalAlignment="Left" Height="60" VerticalAlignment="Top" Width="1016" Margin="0,709,0,0"/>
            <Button x:Name="BtnStart" Content="Start" HorizontalAlignment="Left" Margin="809,723,0,0" VerticalAlignment="Top" Width="100" Height="25"/>
            <Button x:Name="BtnCancel" Content="Exit" HorizontalAlignment="Left" Margin="914,723,0,0" VerticalAlignment="Top" Width="80" Height="25"/>
        </Grid>
    </Window>
'@

Function CollectDomains {   
    $ServerList = Import-XLSX -Path $ImportFile -Header Hostname, Domain, RemotePowershell, RemoteBatchfile
    $DomainResults = @()

    Foreach ($ExcelDomain in ($ServerList.Domain | sort-object | Get-Unique)) {
        $ExcelDomainItem = New-Object PSObject
        $ExcelDomainItem | Add-Member -type NoteProperty -Name 'DomainName' -Value $ExcelDomain
        $DomainResults += $ExcelDomainItem
    }

    Return $DomainResults
}

function ShowLoginScreen {

$DomainList = CollectDomains

Foreach ($Domain in $DomainList) {
$XamlText = @'
    <Rectangle Fill="#FFF1F1F1" HorizontalAlignment="Left" Height="64" VerticalAlignment="Top" Width="997" Margin="6,$c,0,0" Stroke="#FFCDCDCD"/>
    <Label x:Name="LblDomain$b" Content="Domain:" HorizontalAlignment="Left" Margin="14,$c,0,0" VerticalAlignment="Top" FontSize="10" Height="49" Width="221"/>
    <Label x:Name="LblUsername$b" Content="Username:" HorizontalAlignment="Left" Margin="240,$c,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
    <TextBox x:Name="TxtUsername$b" HorizontalAlignment="Left" Height="25" Margin="240,$c,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/>
    <Label x:Name="LblPassword$b" Content="Password:" HorizontalAlignment="Left" Margin="433,$c,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
    <TextBox x:Name="TxtPassword$b" HorizontalAlignment="Left" Height="25" Margin="433,$c,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/>
    <Label x:Name="LblReuseCreds$b" Content="Use credentials from domain:" HorizontalAlignment="Left" Margin="626,$c,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/>
    <ComboBox x:Name="CmbReuseCreds$b" HorizontalAlignment="Left" Margin="626,$c,0,0" VerticalAlignment="Top" Width="188" Height="25"/>
'@
}

CreateGui -Xaml $GuiXaml

$Gui | % {
    $_.Title = "Voer inlog gegevens in"
    $_.WindowStartupLocation ="CenterScreen"
    $_.ShowInTaskbar = "True"
    $_.ResizeMode ="NoResize"
    $_.Add_KeyDown({
        if ($args[1].key -eq 'Return') { $Gui.Close(); StartTests }
        elseif ($args[1].key -eq 'Escape') { $Gui.Close(); Exit }
    })
}

$BtnStart.Add_Click({ $Gui.Close(); StartTests })
$BtnCancel.Add_Click({ $Gui.Close(); Exit })

$Gui.Showdialog() | Out-Null

}

ShowLoginScreen

# Remove leftover functions
Remove-Item function:\add*

0 个答案:

没有答案