Powershell output in color - powershell

I am using a Powershell form to output some data and I am wondering how I can get the output in color?
I am not using write-host. That is not what I am looking for. I know you can use -ForegroundColor for that.
It's for Get-ADUser -Filter "UserPrincipalName -like 'Username'" | Select Enabled
If output is False it needs to be in Red. If output is true just regular color.
Anyone who can help me?
Many thanks.
Ralph.

A follow-up to my comment
#region Begin functions and code behind
function RunCode {
$ProcessList = (Get-Process).Name
If ($ProcessList -ge 10)
{$DataSet.ForeColor = 'red'}
else {$DataSet.ForeColor = 'black'}
[void] $DataSet.Items.Addrange($ProcessList)
}
#endregion End functions and code behind
#region Begin GUI code
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '511,501'
$Form.text = "Form"
$Form.TopMost = $false
$RunCode = New-Object system.Windows.Forms.Button
$RunCode.text = "RunCode"
$RunCode.width = 90
$RunCode.height = 30
$RunCode.location = New-Object System.Drawing.Point(19,17)
$RunCode.Font = 'Microsoft Sans Serif,10'
$DataSet = New-Object system.Windows.Forms.ListBox
$DataSet.text = "listBox"
$DataSet.width = 204
$DataSet.height = 144
$DataSet.location = New-Object System.Drawing.Point(17,98)
$Form.controls.AddRange(#(
$RunCode,
$DataSet
))
$RunCode.Add_Click({ RunCode })
#endregion Begin GUI code
# Call the GUI
[void]$Form.ShowDialog()

Related

How can I uninstall Hotfix remotely with Powershell

This what I have so far it brings up a Syntax screen for wusa. I have confirmed that the Trim is working. If I leave out the remote computer name is works on the local computer. I will be adding this to a much larger script just trying to get this working before trying to add it.
<#
.NAME
Template
#>
$comp = "Remote Pc Name Goes Here"
$str = $Hotfix_TextBox.Text. Trim("K","B")
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,400)
$Form.text = "Form"
$Form.TopMost = $false
$gethotfix = New-Object system.Windows.Forms.Button
$gethotfix.text = "Get Hotfixes"
$gethotfix.width = 120
$gethotfix.height = 30
$gethotfix.location = New-Object System.Drawing.Point(100,81)
$gethotfix.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$removehotfix = New-Object system.Windows.Forms.Button
$removehotfix.text = "Remove Hotfix"
$removehotfix.width = 120
$removehotfix.height = 30
$removehotfix.location = New-Object System.Drawing.Point(100,120)
$removehotfix.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Hotfix_TextBox = New-Object system.Windows.Forms.TextBox
$Hotfix_TextBox.Text = ""
$Hotfix_TextBox.multiline = $false
$Hotfix_TextBox.width = 174
$Hotfix_TextBox.height = 20
$Hotfix_TextBox.location = New-Object System.Drawing.Point(12,235)
$Hotfix_TextBox.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
<#
$Trimmed_TextBox = New-Object system.Windows.Forms.TextBox
#$Trimmed_TextBox.Text = "$str"
$Trimmed_TextBox.multiline = $false
$Trimmed_TextBox.width = 174
$Trimmed_TextBox.height = 20
$Trimmed_TextBox.location = New-Object System.Drawing.Point(12,265)
$Trimmed_TextBox.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
#>
$Form.controls.AddRange(#($gethotfix,$removehotfix,$Hotfix_TextBox))
$gethotfix.Add_Click({ GetHotfix })
$removehotfix.Add_Click({ RemoveHotfix })
#region Logic
function GetHotfix {$Hotfix_TextBox.Text = Get-Hotfix -Computername $comp |
Select-Object -ExpandProperty 'HotFixID'|
Out-GridView -Title 'Installed Hotfixes' -PassThru }
#$Hotfix_TextBox.Text. Trim("K","B")
#$Hotfix_TextBox.Text = "$str"
function RemoveHotfix{
#$Trimmed_TextBox.Text = "$str"
$comp = "dus-xtdfed9r386"
#Uninstall-HotFix -ComputerName $comp
wusa -computername /$comp | /uninstall | /kb:$str
}
#endregion
[void]$Form.ShowDialog()
This turned out to be a fiasco. Even when I could get it to work there are many KBs that cannot be removed, as well as we had policies that would not allow us to remove many others. Just not worth the effort. Perhaps someone with less strengent policies in place can do something with this. Have Fun.

Text parameter not working with Powershell Progress Bar

I've been looking around for the past half hour on how to show the text inside of a GUI ProgressBar in Powershell, and everything I've tried has failed. I've even been referencing MSoft docs on it.
Am I doing something wrong? How do I add in the text?
This isn't my full script or exactly how I'll be using it - I just made an example so I could try to get it working.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Size = '500,300'
$form.StartPosition = 'CenterScreen'
$form.Topmost = $true
$computerList = 'server01', 'server02', 'server03', 'server04', 'server05', 'server06', 'server07', 'server08', 'server09', 'server10'
$progressbar1 = New-Object System.Windows.Forms.ProgressBar
$progressbar1.Size = '300, 20'
$progressbar1.Location = '20,60'
$progressbar1.Text = "Processing..."
$progressbar1.Maximum = $computerList.Count
$progressbar1.Step = 1
$progressbar1.Value = 0
foreach ($computer in $computerList){
$progressbar1.PerformStep()
}
$form.Controls.Add($progressbar1)
$form.ShowDialog()
I guess the easiest way is to have a Label control above the progressbar and update the text in there:
$progressLabel = New-Object System.Windows.Forms.Label
$progressLabel.Size = '300, 20'
$progressLabel.Location = '20,40'
$progressLabel.Text = "Processing..."
$form.Controls.Add($progressLabel)
foreach ($computer in $computerList){
$progressLabel.Text = "Doing stuff on computer '$computer'.."
$progressbar1.PerformStep()
# perform your action on $computer here
}

List AD Users in List Box in Powershell

I'm currently making my own administration tool and one function should end up being "Disable Account" (Active Directory User).
The Code I currently have is the following:
#Assemblies
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#Frame
$frmDisableUser = New-Object system.Windows.Forms.Form
$frmDisableUser.ClientSize = New-Object System.Drawing.Point(378,99)
$frmDisableUser.text = "Disable User"
$frmDisableUser.TopMost = $false
$frmDisableUser.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")
$frmDisableUser.TopMost = $false
$frmDisableUser.FormBorderStyle = "FixedSingle"
$frmDisableUser.startposition = "CenterScreen"
$frmDisableUser.MaximizeBox = $false
#AD Users Listbox
$lstADUsers = New-Object system.Windows.Forms.ListBox
$lstADUsers.width = 356
$lstADUsers.height = 20
$lstADUsers.location = New-Object System.Drawing.Point(9,18)
$lstADUsers.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
#Disable Account Button
$btnDisableAccount = New-Object system.Windows.Forms.Button
$btnDisableAccount.text = "Disable"
$btnDisableAccount.width = 100
$btnDisableAccount.height = 30
$btnDisableAccount.location = New-Object System.Drawing.Point(265,53)
$btnDisableAccount.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$btnDisableAccount.Add_Click({
})
#Adds all elements into th eframe
$frmDisableUser.controls.AddRange(#($lstADUsers,$btnDisableAccount))
#Shows the frame
$frmDisableUser.ShowDialog()
The Command I use to get all AD Users as an output is the following:
Get-ADUser -Filter {(Enabled -eq "true")} | Select-Object Name
I think the easiest way is to make it using arrays but I'm not really familiar with arrays to be honest... I'd be very happy if you could help me!
Thanks for your time!
You can add items to the listbox with
$lstADUsers.Items.Add()
I've updated your code to demonstrate this.
#Assemblies
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#Frame
$frmDisableUser = New-Object system.Windows.Forms.Form
$frmDisableUser.ClientSize = New-Object System.Drawing.Point(388,299)
$frmDisableUser.text = "Disable User"
$frmDisableUser.TopMost = $false
$frmDisableUser.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")
$frmDisableUser.TopMost = $false
$frmDisableUser.FormBorderStyle = "FixedSingle"
$frmDisableUser.startposition = "CenterScreen"
$frmDisableUser.MaximizeBox = $false
#AD Users Listbox
$lstADUsers = New-Object system.Windows.Forms.ListBox
$lstADUsers.width = 356
$lstADUsers.height = 220
$lstADUsers.location = New-Object System.Drawing.Point(9,18)
$lstADUsers.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$lstADUsers.AutoSize = $false
Get-ADUser -Filter {(Enabled -eq "true")} | foreach{[void]$lstADUsers.Items.Add($_.name)}
#Disable Account Button
$btnDisableAccount = New-Object system.Windows.Forms.Button
$btnDisableAccount.text = "Disable"
$btnDisableAccount.width = 100
$btnDisableAccount.height = 30
$btnDisableAccount.location = New-Object System.Drawing.Point(265,249)
$btnDisableAccount.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$btnDisableAccount.Add_Click({
})
#Adds all elements into th eframe
$frmDisableUser.controls.AddRange(#($lstADUsers,$btnDisableAccount))
#Shows the frame
$frmDisableUser.ShowDialog()
Note the cast to [void] - this is to suppress the output from the .Add() method. It emits the the index number for that item in the array.
I also suggest you check out https://poshgui.com/ if you haven't already. It can help you not only design forms but also learn how to interact with the GUI components.
The Items property of a ListBox (which is an ObjectCollection object) has a method called AddRange with which you can enter an array.
Just get a string array of usernames and enter it in one go.
To make the list better readable, sort it alphabetically. You could also set the 'Sorted' property of the Listbox to $true, but sorting before adding to the listbox is more efficient.
$users = (Get-ADUser -Filter "Enabled -eq 'True'").Name | Sort-Object
Next add the array to the listbox
$lstADUsers.Items.AddRange($users)
P.S. If you need to refresh the listbox data with the result of a new call to Get-ADUser, clear the listbox items first with $lstADUsers.Items.Clear()

Password Generator - Generate password on demand

I have been working on a scrip that generates a password with specific characters with a specific length, but with random numbers at a specific length.
The script has an GUI (it's a work in progress, I will finish it eventually).
The issue that I'm facing, is that, whenever I press "Generate Password", it creates a password, but it does not give me a new one after generating it. It just gives the same password that it generates it the first time.
I was looking on the web on how to retrieve a new password each time the button is pressed, but I did not found anything.
Can someone help with some tips?
Thank you.
The script is:
Function Button_Click()
{
[System.Windows.Forms.MessageBox]::Show($DefinedLetters)
}
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Password Generator"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# Add Button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Generate Password"
$Form.Controls.Add($Button)
#Add Button event
$Button.Add_Click({Button_Click})
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
# Password generator #
Function DefinedLetters
{
$DefinedLetters = 'Summer'
$numbers = 0..5
$array = #()
$array += $DefinedLetters.Split(',') | Get-Random -Count 4
$DefinedLetters += $numbers | Get-Random -Count 4
($DefinedLetters | Get-Random -Count $DefinedLetters.Count) -join ""
}
#Call the Function
Generate-Form
This isn't a great method of generating passwords, but here is a version of your code that produces a 'random' password each time using 4 letters from 'summer' and 4 numbers from (0,1,2,3,4,5):
# Password generator #
Function DefinedLetters {
$DefinedLetters = 'Summer'
$numbers = 0..5
$array = #()
$array += $DefinedLetters.ToCharArray() | Get-Random -Count 4
$array += $numbers | Get-Random -Count 4
($array | Get-Random -Count $array.Count) -join ""
}
Function Button_Click() {
$DefinedLetters = DefinedLetters
[System.Windows.Forms.MessageBox]::Show($DefinedLetters)
}
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Password Generator"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# Add Button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Generate Password"
$Form.Controls.Add($Button)
#Add Button event
$Button.Add_Click({Button_Click})
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
#Call the Function
Generate-Form
In Button_Click() you just refer to variable, which doesn't call function. You need to assign the value form function to variable like:
Function Button_Click()
{
$PW = DefinedLetters
[System.Windows.Forms.MessageBox]::Show($PW)
}
Your function DefinedLetters doesn't/can't work as you intended
You don't use the function but a variable to show.
Function Button_Click(){
[System.Windows.Forms.MessageBox]::Show((DefinedLetters))
}
Function Generate-Form {
#... snipped for brevity ...
}
Function DefinedLetters{
(([char[]]'Summer' | Get-Random -Count 4) -join '')+
((0..5| Get-Random -Count 4) -join '')
}
#Call the Function
Generate-Form

Get number of dates as selected in Windows Forms MonthCalendar for Powershell

I am using Powershell to create a GUI calendar and I need to be able assign a variable to the number of dates selected as the user selects them. I imagine I would have to use an event but am not sure how to procede. Basically, if a user selects a range of more than 10 days I need a checkbox on the same form to be checked. I am doing something similar with a listbox but I can't seem to figure out how to do the same with the calendar. Thanks.
Listbox code:
$Listbox2 = New-Object System.Windows.Forms.ListBox
$Listbox2.Location = New-Object System.Drawing.Size(240,80)
$Listbox2.Size = New-Object System.Drawing.Size(140,210)
$Listbox2.Height = 210
$Listbox2.SelectionMode = "MultiExtended"
$Listbox2Content | ForEach-Object {[void] $Listbox2.Items.Add($_)}
$Listbox2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",11,0,3,1)
$Listbox2.Add_SelectedValueChanged({
If (($Listbox2.SelectedItems).Count -ge 10) {$Checkbox2.Checked = $True}
If (($Listbox2.SelectedItems).Count -lt 10) {$Checkbox2.Checked = $False}
})
Calendar Code:
$Calendar = New-Object System.Windows.Forms.MonthCalendar
$Calendar.Location = New-Object System.Drawing.Size(12,80)
$Calendar.ShowTodayCircle = $False
$Calendar.ShowToday = $True
$Calendar.MaxDate = (Get-Date).AddDays(1)
$Calendar.MinDate = $OldestLog
$Calendar.MaxSelectionCount = "$CalendarDateRange"
$MenuBox.Controls.Add($Calendar)
The $data_selected scriptblock prints the number of selected dates. I attached it to the DataSelected event so it fires when you click on a date or make a range selection.
Add-Type -AssemblyName System.Windows.Forms
$date_selected = {
write-host (($_.End - $_.Start).Days + 1)
}
$form = New-Object Windows.Forms.Form
$form.text = "Calendar"
$form.FormBorderStyle = 'FixedDialog'
$form.Size = New-Object Drawing.Size(190,190)
$cal = New-Object System.Windows.Forms.MonthCalendar
$cal.add_DateSelected($date_selected)
$form.Controls.Add($cal)
$form.Add_Shown($form.Activate())
$form.showdialog() | Out-Null