First of all, this is my first ever script with a XML GUI so it may be something very obvious that is missing. Basically, I'm doing a simple script where you choose a customer (translates to OU) and look for a username based on first name. However, the input window takes whatever it says when I run the script (so nothing) and never refresh after I filled in the form.
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:
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()
CodePudding user response:
Without seeing the form or the xaml it is quite hard to understand what it is exactly you want.
First thing I noticed is that your filter for Get-ADUser
won't work.
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.
Also, if you use the -like
operator, you should also use wildchar characters. Without it, -like
works the same as -eq
.
Use 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. (see below)
The various if ($customer -match "Customer1") {..}
you have can also be simplyfied. Since you are already using the regex -match
operator, you can combine the options with the regex OR character |
$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