Reputation: 5604
I'm trying to take an inputted text string like WIN
from a PowerShell form and find all computers having WIN
as the first three letters of their computer name.
To do this I built off of a Microsoft Technet example and only very slightly modified the code to take the inputted text string and save it as variable $global:x. I pass that variable ($global:x
) into a filter in the last line of code which outputs to a file but the output file is blank. I've done some googling, and changed the variable from $x
to $global:x
but that didn't work.
File is still blank. My only changes to the TechNet code example was changing the variable from $x
to $global:x
and add the filter at the end; the filter is the very last line in the code and is supposed to pipe the variable results into the output file.
The entire code is below.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "FindSimiliarComputers"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$global:x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$global:x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Please enter the partial computer name"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
Get-ADComputer -filter {name -like "$global:x*" -and Enabled -eq "true" } | Select -expand Name | out-file C:\Users\Admin1\Desktop\computers.txt
Upvotes: 3
Views: 1045
Reputation: 29048
Your Get-ADComputer filter syntax is broken
Change it to :
Get-ADComputer -filter "name -like '$global:x*' -and Enabled -eq 'true'" | Select -expand Name | out-file C:\Users\Admin1\Desktop\computers.txt
And your script will work.
The article you followed is very out of date - there is discussion of what's wrong with it, and new updated examples from 2014 on The Scripting Guys' blog here
Upvotes: 3