I have designed a Powershell Gui Tool, the tool is designed works pretty well but I am stuck on how to populate the listview columns with the data from several xml files.
I have four column that I want populate
# ListView
$LvMain= New-Object System.Windows.Forms.Listview
$LvMain.Location = '300, 55'
$LvMain.Name = "ListViewInfo"
$LvMain.Size = "850, 600"
$LvMain.Text = "Pattern text"
$LvMain.Scrollable = $true
$LvMain.ContextMenuStrip = $ContextMenu
$LvMain.FullRowSelect = $True
$LvMain.GridLines = $True
$LvMain.UseCompatibleStateImageBehavior = $False
$LvMain.View = "Details"
$LvMain.font ="lucida console"
$LvMain.Controls.Add($ApplicationFormListViewInfo)
# Columns
$LvMain.Columns.Add("FileName")
$LvMain.Columns.Add("Folder")
$LvMain.Columns.Add("Line")
$LvMain.Columns.Add("Path")
It is meant when i hit the button " Run "
I will draw out the data from several xml files with
$GetXmlData = Get-ChildItem -Path $Path -Recurse -force -Include $FileTypes | Select-String -Pattern $Pattern
And populate the columns with the $Patterns Filename,Foldername,Line ( which line the pattern is ) and Path ( UncPath ).
However, a realy good tutorial on how listview works is hard to find.
Anyone who knows how to guide me to the right way ?
Related
i need to create an Script that checks the Free and Used storages of Hosts within our Network. The Hosts are loaded into an array and than those disks with Free Space and Size are being shown in the PowerShell.
Now i want that all of those things are being exported to an csv File so you can analyse the data easier.
Here is the script:
$servers = #("server1", "server2", "server3")
Foreach ($server in $servers)
{
$disks = Get-WmiObject Win32_LogicalDisk -ComputerName $server -Filter DriveType=3 |
Select-Object DeviceID,
#{'Name'='Size'; 'Expression'={[math]::truncate($_.size / 1GB)}},
#{'Name'='Freespace'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}
$server
foreach ($disk in $disks)
{
$disk.DeviceID + $disk.FreeSpace.ToString("N0") + "GB / " + $disk.Size.ToString("N0") + "GB"
}
}
Thx btw :)
Instead of trying to control the output format by manually outputting the $server value, you'll want to "glue" the server name onto each associated $disk object, this will make it much easier to export to CSV (with the correct server name):
$servers = #("server1", "server2", "server3")
$allDisks = foreach ($server in $servers)
{
# Instead of assigning to `$disks` inside the loop,
# we let the output from all loop executions "bubble up"
# to the assignment to `$allDisks`
Get-WmiObject Win32_LogicalDisk -ComputerName $server -Filter DriveType=3 |
Select-Object #{'Name'='ComputerName'; 'Expression'={$server}},
DeviceID,
#{'Name'='Size'; 'Expression'={[math]::truncate($_.size / 1GB)}},
#{'Name'='Freespace'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}
}
Now that each object has the correct server name attached, we can easily export to CSV:
$allDisks |Export-Csv .\path\to\output.csv -NoTypeInformation
... and we can also use PowerShell's native formatting subsystem for manipulating the output in the interactive shell nicely:
$allDisks |Format-Table DeviceID,Size,FreeSpace -GroupBy ComputerName
I have a button on a WinForm. After clicking on the button, a function will be called which should execute Get-ADUser cmdlet.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Data
Import-Module ActiveDirectory
$ctl_frm_aduserlist = New-Object System.Windows.Forms.Form -Property #{
Size = New-Object System.Drawing.Size(500,500)
StartPosition = "CenterScreen" }
$ctl_btn_generatepreview = New-Object System.Windows.Forms.Button -Property #{
Size = New-Object System.Drawing.Size(200,30)
Location = New-Object System.Drawing.Point(10,30)
Text = "Generate Preview" }
$ctl_frm_aduserlist.Controls.Add($ctl_btn_generatepreview)
$ctl_btn_generatepreview.Add_Click({ GeneratePreview })
function GeneratePreview(){
Write-Host "GO"
Get-ADUser -Identity "user123" -Properties Name,SamAccountName | select Name,SamAccountName
Write-Host "END" }
$ctl_frm_aduserlist.ShowDialog()
Only the two "Write-Host" cmdlets will be executed by clicking on the button.
If I only execute the single line Get-ADUser in the ISE console, it works and I get the user object.
Why does Get-ADUser not work when triggered via button?
Thanks
First off, I don't have a full solution as I'm not an expert in WinForms, to feel free to correct/complete this answer.
As far as I know, the command is actually executed. You can check it by stepping though the code or Write-Host-ing bits of the object you collected with Get-ADUser and it will display the correct info:
function GeneratePreview() {
Get-ADUser -Identity "someone" | Write-Host
}
Note that if you replace Write-Host by Write-Output it does not work anymore.
The thing is, when you make a WinForms application, the standard output isn't the console anymore. I don't know where it is redirected to, but it's not visible by default. You need to either specify that you want to see your data in the console with write-host, export it to a file (Set-Content, Export-Csv, you name it) or display it in a WinForm element:
I added a new TextBox to your form (called $TextBox1 in my example), and changed GeneratePreview like this:
function GeneratePreview() {
$user = Get-ADUser "someone"
$TextBox1.Text = "$($user.name),$($user.samaccountname)"
}
I want to generate an import script for a MSSQL DB via Powershell (related to this question).
I tried doing this:
#Set-ExecutionPolicy RemoteSigned
$DB_NAME = "<<dbName>>"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
$srv = new-object "Microsoft.SqlServer.Management.SMO.Server" "<<server>>"
$conContext = $srv.ConnectionContext
$conContext.LoginSecure = $false
$conContext.Login = "<<user>>"
$conContext.Password = "<<password>>"
$srv = new-object Microsoft.SqlServer.Management.Smo.Server($conContext)
$srv.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.View], "IsSystemObject")
$db = $srv.databases[$DB_NAME]
$scripter = new-object "Microsoft.SqlServer.Management.Smo.Scripter" $srv
$scripter.Options.ScriptSchema = $false
$scripter.Options.ScriptData = $true
$scripter.Options.ScriptDrops = $false
$scripter.Script($db)
But executing this throws an error:
"This method does not support scripting data"
I also tried to set the output file option but this doesn't change anything.
Can you tell me what I did wrong?
Thanks!
Per the error, Scripter.Script does not support scripting data. This is documented. What isn't documented is what you're supposed to use instead, but it's EnumScript:
$scripter.EnumScript(#($db.Tables))
You must pass the tables, since simply scripting the database will yield nothing (as, technically, the database itself contains no data, its tables do).
(The #() forcibly converts the Tables collection to an array, since that's what EnumScript expects.)
I am trying to create in PowerShell a GUI window with dynamic content. I need to:
create a window with a random count of buttons (or other clickable items)
after a click button and related text label will be removed from the window
IMPORTANT: I cannot use a list or datagrid.
I have the following code but it still returns only last item value.
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(400,400)
$Array = New-Object System.Collections.ArrayList
$Array.add('AAA')
$Array.add('BBB')
$Array.add('CCC')
$Array.add('DDD')
foreach ($item in $Array) {
New-Variable -Force -Name "button$membershipCount" -Value (New-Object System.Windows.Forms.Button)
$thisButton = Get-Variable -ValueOnly -Include "button$membershipCount"
$thisButton.Location = New-Object System.Drawing.Size(175,(35+26*$membershipCount))
$thisButton.Size = New-Object System.Drawing.Size(250,23)
$thisButton.Text = $item
$thisButton.Add_Click({(Write-Host $thisButton.Text | Out-Null)})
$form.Controls.Add($CHANGEButton)
}
I also tried Invoke-Expression, but it doesn't return expected results:
Invoke-Expression -Command "`$thisButton.Add_Click({`$x=`"$($item)`";`write-host $x})"
Or any better idea how can I get details which button was clicked since the number of buttons is random?
The problem is that the scriptblock inside add_Click() now contains a reference to $thisbutton, which at runtime will have been replaced with the last value in the foreach loop - this is expected behavior.
You can do 1 of 2 things here.
1. Capture the $thisButton.Text (or $item) value in a closure:
# Piping to Out-Null has zero effect here, just remove it
$thisButton.Add_Click({Write-Host $thisButton.Text}.GetNewClosure())
2. Use the event arguments to determine which button was clicked at runtime:
$thisButton.Add_Click({param($Sender,$EventArgs) Write-Host $Sender.Text})
Hi i am trying to create a dynamic form in pwoershell , this is a form which has 5 buttons(color names) and each button opens a different text file(such as if red button is clicked,it should open red.txt; here is the full code;
Script Start
$var = "Red","Blue","Yellow","Black","White"
$testForm = New-Object System.Windows.Forms.Form
$testForm.Text = "Color List"
$testForm.AutoSize = $True
$testForm.AutoSizeMode = "GrowAndShrink"
$Font = New-Object System.Drawing.Font("Times New Roman",24, [System.Drawing.FontStyle]::Bold)
$testForm.Font = $Font
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Select the Color"
$Label.AutoSize = $True
$testForm.Controls.Add($Label)
$x=100
$y=50
foreach($color in $var)
{
$run = New-Object System.Windows.Forms.Button
$run.Location = New-Object System.Drawing.Size($x,$y)
$run.Size = New-Object System.Drawing.Size(100,50)
$run.Text = "$Color"
$run.Add_Click({ Invoke-Expression "notepad C:\Users\User\$color.txt" })
$testForm.Controls.Add($run)
$Font = New-Object System.Drawing.Font("Times New Roman",14,[System.Drawing.FontStyle]::Regular)
$run.font = $Font
$run.AutoSize = $True
$y+=50
}
$testForm.ShowDialog()
END Script
Everything went fine until, when the form opens with buttons, and all the buttons when clicked, open the file "White.txt" since its the last element in the array; is there any way to change the script to make each button open only their respective files and not the last color file?
please do let me know if any further questions or clarifications needed.
Looks like the problem is the Add_Click line. The script block contains a link to the variable $color, rather than evaluating it immediately to create a new "notepad..." string for Invoke-Expression and linking to that. The string for Invoke-Expression will be created when the button is clicked. By this time $color is White 'cos the loop has finished, so all the buttons end up creating a string using White.
You can fix it with a call to GetNewClosure() which will build cause the string for the Invoke-Expression to be created during the loop, not later on when the button is clicked. So change the line to:
$run.Add_Click({ Invoke-Expression "notepad C:\Users\User\$color.txt" }.GetNewClosure())
And it should work as expected.