Powershell Xml gui冻结

Powershell Xml gui冻结,powershell,Powershell,我有一个带有小gui的powershell脚本。我想把脚本的输出写在名为output的教科书中。但是当我使用$OutputBox.text时,脚本崩溃。我还尝试了.appendtext。当我只写控制台时,它工作得很好。有类似gui线程的东西吗?这是我的第一个powerShell脚本plez帮助 脚本如下所示: Add-Type -AssemblyName PresentationFramework [xml]$xaml = @" <Window xmlns=

我有一个带有小gui的powershell脚本。我想把脚本的输出写在名为output的教科书中。但是当我使用$OutputBox.text时,脚本崩溃。我还尝试了.appendtext。当我只写控制台时,它工作得很好。有类似gui线程的东西吗?这是我的第一个powerShell脚本plez帮助

脚本如下所示:

  Add-Type -AssemblyName PresentationFramework

[xml]$xaml =
@"
<Window
        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"
        Title="MainWindow" Height="450" Width="800">
    <Grid Margin="0,0,0,-6">
        <TextBox x:Name="patch_to_file" HorizontalAlignment="Left" Margin="82,63,0,0" Text="Insert the path to the file" TextWrapping="Wrap" VerticalAlignment="Top" Width="670" SelectionOpacity="2" Height="36" AutomationProperties.IsRequiredForForm="True" FontSize="24"/>
        <TextBox x:Name="patch_to_file_dump" HorizontalAlignment="Left" Margin="82,147,0,0" Text="insert the path to where you want the files" TextWrapping="Wrap" VerticalAlignment="Top" Width="670" SelectionOpacity="2" Height="36" AutomationProperties.IsRequiredForForm="True" FontSize="24"/>
        <TextBox x:Name="output_box" HorizontalAlignment="Left" Margin="82,250,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="125" Width="670"/>
        <Label Content="Output" HorizontalAlignment="Left" Margin="82,218,0,0" VerticalAlignment="Top" Width="114" Height="27"/>
        <Button x:Name="GoButton" Content="GoButton" HorizontalAlignment="Left" Margin="637,218,0,0" VerticalAlignment="Top" Width="115" Background="#FF00FF46" BorderBrush="#FF020202" Foreground="Black"/>
    </Grid>
</Window>
"@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load($reader)

$OutputBox = $Window.FindName("output_box")
$filePath = $Window.FindName("patch_to_file")
$dumpPath = $Window.FindName("patch_to_file_dump")
$GoButton = $Window.FindName('GoButton')

$filePath.add_GotFocus({
    $filePath.Clear()
})

$dumpPath.add_GotFocus({
    $dumpPath.Clear()
})

$GoButton.Add_Click({
    BtnStart
})

$list = @()
$noDnsRespons = @()
$dnsRespons = @()
$ipsDontMatch = @()
$ipsMatch = @()




Function BtnStart {

    $OutputBox.text = "Start"
    

    $DNSlist = Import-Csv -Path $filePath.Text  -delimiter ';' 
    

    foreach ($i in $DNSlist) {
        $object = New-Object -TypeName PSobject -Property @{
            dnsName = $i.Dns_name
            ip = $i.Ip
            hasDns = $true
            IpRomDnsCall = ""
            canPing = $true
        }
        $Script:list += $object
    }

    ForEach($i in $list) {
        

        try {
            Resolve-DnsName -name $i.DnsName -ErrorAction SilentlyContinue}
        catch{
            "DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName"
        }
        if($Error) {
            $i.hasDns = $false
        }
    }

    foreach($i in $list) {
        if($i.hasDns -eq $false) {
            $i.canPing -eq $false
            $noDnsRespons += $i 
        }
        else {
            $dnsRespons += $i
        }
    }

    foreach($i in $dnsRespons) {
        $i.IpRomDnsCall = Resolve-DnsName $i.dnsName | Select-Object -ExpandProperty IPAddress

        if($i.IpRomDnsCall -eq $i.ip) {
            $ipsMatch += $i
        }
        else {
            $ipsDontMatch += $i 
            $i.IpRomDnsCall -eq $false
        }

        Write-Host "Køre stadig"

        foreach($i in $ipsMatch){
            if(-Not (Test-Connection $i.ip)) {
                $i.canPing -eq $false
            }

        
        }
    }

    $ipsDontMatch | Export-Csv -Path $dumpPath.Text\falseIp.csv -Delimiter ';' -NoTypeInformation 
    $noDnsRespons | Export-Csv -Path $dumpPath.Text\densWithNoRespons.csv -Delimiter ';' -NoTypeInformation
    $ipsMatch | Export-Csv -Path $dumpPath.Text\ListWithCanPing.csv -Delimiter ';' -NoTypeInformation
}

$Window.ShowDialog() 
添加类型-AssemblyName PresentationFramework
[xml]$xaml=
@"
"@
$reader=(新对象System.Xml.XmlNodeReader$xaml)
$Window=[Windows.Markup.XamlReader]::加载($reader)
$OutputBox=$Window.FindName(“输出框”)
$filePath=$Window.FindName(“补丁到文件”)
$dumpPath=$Window.FindName(“补丁到文件转储”)
$GoButton=$Window.FindName('GoButton'))
$filePath.add\u GotFocus({
$filePath.Clear()
})
$dumpPath.add\u GotFocus({
$dumpPath.Clear()
})
$GoButton.Add\u单击({
BtnStart
})
$list=@()
$noDnsRespons=@()
$dnsRespons=@()
$ipsDontMatch=@()
$ipsMatch=@()
函数BtnStart{
$OutputBox.text=“开始”
$DNSlist=Import Csv-Path$filePath.Text-分隔符“;”
foreach($DNSlist中的i){
$object=新对象-TypeName PSobject-Property@{
dnsName=$i.Dns\u名称
ip=$i.ip
hasDns=$true
IpRomDnsCall=“”
canPing=$true
}
$Script:list+=$object
}
ForEach($列表中的i){
试一试{
解析DnsName-name$i.DnsName-ErrorAction SilentlyContinue}
抓住{
DNS\u错误\u RCODE\u NAME\u错误,Microsoft.DnsClient.Commands.ResolveDnsName
}
如果($Error){
$i.hasDns=$false
}
}
foreach($列表中的i){
如果($i.hasDns-eq$false){
$i.canPing-eq$false
$noDnsRespons+=$i
}
否则{
$dnsRespons+=$i
}
}
foreach($dnsRespons中的i){
$i.IpRomDnsCall=解析DnsName$i.DnsName |选择对象-ExpandProperty IPAddress
如果($i.IpRomDnsCall-等式$i.ip){
$ipsMatch+=$i
}
否则{
$ipsDontMatch+=$i
$i.IpRomDnsCall-等式$false
}
写主机“Køre stadig”
foreach($i中的$ipsMatch){
如果(-Not(测试连接$i.ip)){
$i.canPing-eq$false
}
}
}
$ipsDontMatch |导出Csv-路径$dumpPath.Text\falseIp.Csv-分隔符“;”-非类型信息
$NODNRespons |导出Csv-路径$dumpPath.Text\densWithNoRespons.Csv-分隔符“;”-非类型信息
$ipsMatch |导出Csv-路径$dumpPath.Text\ListWithCanPing.Csv-分隔符“;”-非类型信息
}
$Window.ShowDialog()

在GUI线程上执行任何需要一段时间的操作,例如解析名称或等待ping响应,都将冻结GUI线程。目前PowerShell脚本中只有一个线程,因此所有操作都会导致GUI挂起。如果您想解决这个问题,除了使用后台线程或进程来完成工作之外,没有其他方法。您可以从查看
Start Job
cmdlet及其同级可以为您做什么开始。在GUI线程上执行任何需要一段时间的操作,例如解析名称或等待ping响应,都将冻结GUI线程。目前PowerShell脚本中只有一个线程,因此所有操作都会导致GUI挂起。如果您想解决这个问题,除了使用后台线程或进程来完成工作之外,没有其他方法。您可以从查看
Start Job
cmdlet及其同级可以为您做什么开始。