简体   繁体   English

Powershell 和 XAML GUI 在脚本结束之前不读取数据

[英]Powershell with XAML GUI does not read data until end of script

First of all, this is my first ever script with a XML GUI so it may be something very obvious that is missing.首先,这是我的第一个带有 XML GUI 的脚本,因此它可能缺少非常明显的东西。 Basically, I'm doing a simple script where you choose a customer (translates to OU) and look for a username based on first name.基本上,我正在做一个简单的脚本,您可以在其中选择一个客户(转换为 OU)并根据名字查找用户名。 However, the input window takes whatever it says when I run the script (so nothing) and never refresh after I filled in the form.但是,输入 window 会在我运行脚本时接受它所说的任何内容(所以什么都没有),并且在我填写表格后永远不会刷新。

To clarify, when I click the button I want it to run through the form and see what has been written澄清一下,当我单击按钮时,我希望它运行整个表单并查看已写的内容

If you guys need the XAML code let me know, the Powershell code looks like this:如果你们需要 XAML 代码,请告诉我,Powershell 代码如下所示:

Add-Type -AssemblyName PresentationFramework
$xamlFile = "$PSScriptRoot\MainWindow.xaml"

$inputXML = Get-Content $xamlFile -Raw
$inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'
[XML]$XAML = $inputXML
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
try 
{
    $window = [Windows.Markup.XamlReader]::Load($reader)
}
catch 
{
    Write-Warning $_.Exception
    throw
}
$xaml.SelectNodes("//*[@Name]") | ForEach-Object 
{
    try 
    {
        Set-Variable -Name "var_$($_.Name)" -Value $window.FindName($_.Name) -ErrorAction Stop
    }
    catch 
    {
        throw
    }
}
Get-Variable var_*

$customer = $var_CustomerDropDown.Text
$firstName = $var_FirstnameBox.Text

$var_CreateButton.Add_Click
({
    $var_LogText1.Text = ""
    if ($customer -match "Customer1") 
    {
        $result = Get-ADUser -Filter 'Name -like $firstName' | Select-Object -ExpandProperty Name
        foreach ($item in $result)
        {
            $var_LogText1.Text = $var_LogText1.Text + "$item`n" + "Customer1`n"
        }
    }
    if ($customer -match "Customer2") 
    {
        $result = Get-ADUser -Filter 'Name -like $firstName' | Select-Object -ExpandProperty Name
        foreach ($item in $result)
        {
            $var_LogText1.Text = $var_LogText1.Text + "$item`n" + "Customer2`n"
        }
    }     
    else 
    {
        $var_LogText1.Text = "No customer chosen"
    }            
})

$Null = $window.ShowDialog()

Without seeing the form or the xaml it is quite hard to understand what it is exactly you want.如果没有看到表格或 xaml,很难理解您到底想要什么。

First thing I noticed is that your filter for Get-ADUser won't work.我注意到的第一件事是您的Get-ADUser过滤器不起作用。
The reason is that the filter string should be enclosed in double-quotes " , not single-qoutes ' otherwise the variable $firstname will not get expanded and is taken literally.原因是过滤器字符串应该包含在双引号"中,而不是单引号'否则变量$firstname将不会被扩展并按字面意义进行。

Also, if you use the -like operator, you should also use wildchar characters.此外,如果您使用-like运算符,您还应该使用通配符。 Without it, -like works the same as -eq .没有它, -like-eq相同。

Use Get-ADUser -Filter "Name -like '$firstName*'"使用Get-ADUser -Filter "Name -like '$firstName*'"

Then, the loop to fill the textbox is not needed.然后,不需要循环来填充文本框。 You can do that with $var_LogText1.Text = $result -join [environment]::NewLine In fact, this can be written in just one line of code, skipping variable $result alltogether.您可以使用$var_LogText1.Text = $result -join [environment]::NewLine 。事实上,这可以用一行代码编写,完全跳过变量$result (see below) (见下文)

The various if ($customer -match "Customer1") {..} you have can also be simplyfied.您拥有的各种if ($customer -match "Customer1") {..}也可以简单化。 Since you are already using the regex -match operator, you can combine the options with the regex OR character |由于您已经在使用 regex -match运算符,因此可以将选项与 regex OR字符|结合使用。

$var_LogText1.Text = ""
# example match 'Customer1', 'Customer2' or 'John0' to 'John9'
if ($customer -match 'Customer[12]|John\d') {
    # try and get the full usernames and write them each on a separate line in the textbox
    $var_LogText1.Text = (Get-ADUser -Filter "Name -like '$firstName*'").Name -join [environment]::NewLine
}
else {
    $var_LogText1.Text = "No customer chosen"
}         
# if you need this, you can write these values to the console window too ("see what has been written")
Write-Host $var_LogText1.Text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM