LindyS
asked on
Help With A PowerShell Script
I have a script that queries all of our DCs, looking for the lastlogon of specific users.
The user name is entered in a dialog box when the script is executed.
When the user is entered in the box, the box closes.
Is there a way to have the name entered in the script, and keep the box open to enter the next user name?
The script so far is as follows;
$today = Get-Date
$rundate = $($today.adddays(-0)).tosh ortdatestr ing()
[void] [System.Reflection.Assembl y]::LoadWi thPartialN ame("Syste m.Drawing" )
[void] [System.Reflection.Assembl y]::LoadWi thPartialN ame("Syste m.Windows. Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,20 0)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objF orm.Close( )}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Butto n
$OKButton.Location = New-Object System.Drawing.Size(75,120 )
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$o bjTextBox. Text;$objF orm.Close( )})
$objForm.Controls.Add($OKB utton)
$CancelButton = New-Object System.Windows.Forms.Butto n
$CancelButton.Location = New-Object System.Drawing.Size(150,12 0)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$ objForm.Cl ose()})
$objForm.Controls.Add($Can celButton)
$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 = "Enter the user name in the space below:"
$objForm.Controls.Add($obj Label)
$objTextBox = New-Object System.Windows.Forms.TextB ox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20 )
$objForm.Controls.Add($obj TextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objFo rm.Activat e()})
$objForm.ShowDialog()
$x
$dcs = Get-Content "C:\temp\serverlist.txt"
foreach($dc in $dcs){ get-QADUser $x -Service $dc -Enabled -SearchRoot 'auab.subdomain.domainname .com/AUAB' | Select-Object Name,sAMAccountName,Descri ption,last logon }
Write-Host "`nRun time was $(((get-date) - $today).totalseconds) seconds."
The user name is entered in a dialog box when the script is executed.
When the user is entered in the box, the box closes.
Is there a way to have the name entered in the script, and keep the box open to enter the next user name?
The script so far is as follows;
$today = Get-Date
$rundate = $($today.adddays(-0)).tosh
[void] [System.Reflection.Assembl
[void] [System.Reflection.Assembl
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,20
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objF
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Butto
$OKButton.Location = New-Object System.Drawing.Size(75,120
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$o
$objForm.Controls.Add($OKB
$CancelButton = New-Object System.Windows.Forms.Butto
$CancelButton.Location = New-Object System.Drawing.Size(150,12
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$
$objForm.Controls.Add($Can
$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 = "Enter the user name in the space below:"
$objForm.Controls.Add($obj
$objTextBox = New-Object System.Windows.Forms.TextB
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20
$objForm.Controls.Add($obj
$objForm.Topmost = $True
$objForm.Add_Shown({$objFo
$objForm.ShowDialog()
$x
$dcs = Get-Content "C:\temp\serverlist.txt"
foreach($dc in $dcs){ get-QADUser $x -Service $dc -Enabled -SearchRoot 'auab.subdomain.domainname
Write-Host "`nRun time was $(((get-date) - $today).totalseconds) seconds."
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I ended up using a Do-While loop.
do {
$today = Get-Date
$rundate = $($today.adddays(-0)).tosh ortdatestr ing()
[void] [System.Reflection.Assembl y]::LoadWi thPartialN ame("Syste m.Drawing" )
[void] [System.Reflection.Assembl y]::LoadWi thPartialN ame("Syste m.Windows. Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,20 0)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objF orm.Close( )}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Butto n
$OKButton.Location = New-Object System.Drawing.Size(75,120 )
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$o bjTextBox. Text;$objF orm.Close( )})
$objForm.Controls.Add($OKB utton)
$CancelButton = New-Object System.Windows.Forms.Butto n
$CancelButton.Location = New-Object System.Drawing.Size(150,12 0)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$ objForm.Cl ose()})
$objForm.Controls.Add($Can celButton)
$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 = "Enter the user name in the space below:"
$objForm.Controls.Add($obj Label)
$objTextBox = New-Object System.Windows.Forms.TextB ox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20 )
$objForm.Controls.Add($obj TextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objFo rm.Activat e()})
$objForm.ShowDialog()
$x
$dcs = Get-Content "C:\temp\serverlist.txt"
foreach($dc in $dcs){ get-QADUser $x -Service $dc -Enabled -SearchRoot 'Domain/OU' | Select-Object Name,sAMAccountName,Descri ption,last logon }
Write-Host "Run time was $(((get-date) - $today).totalseconds) seconds."
} while ($x -ne 0)
do {
$today = Get-Date
$rundate = $($today.adddays(-0)).tosh
[void] [System.Reflection.Assembl
[void] [System.Reflection.Assembl
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,20
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objF
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Butto
$OKButton.Location = New-Object System.Drawing.Size(75,120
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$o
$objForm.Controls.Add($OKB
$CancelButton = New-Object System.Windows.Forms.Butto
$CancelButton.Location = New-Object System.Drawing.Size(150,12
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$
$objForm.Controls.Add($Can
$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 = "Enter the user name in the space below:"
$objForm.Controls.Add($obj
$objTextBox = New-Object System.Windows.Forms.TextB
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20
$objForm.Controls.Add($obj
$objForm.Topmost = $True
$objForm.Add_Shown({$objFo
$objForm.ShowDialog()
$x
$dcs = Get-Content "C:\temp\serverlist.txt"
foreach($dc in $dcs){ get-QADUser $x -Service $dc -Enabled -SearchRoot 'Domain/OU' | Select-Object Name,sAMAccountName,Descri
Write-Host "Run time was $(((get-date) - $today).totalseconds) seconds."
} while ($x -ne 0)
Anyway - did you try placing the script in a While ($true){} loop?