I have a small GUI that will connect to a vCenter in a list. The forms open in the order I want, but I can't carry values between them. I've tried so many return calls I don't know what else to do.
Here is the code. Basically, the first form opens and asks for which vCenter you want. Then prompts you to authenticate (this all works wonderfully). But the problem comes in after the script is done. I don't get any return values.
function Connect-Vc ($vcArg)
{
$Form2 = $null
$selectedVC = $vcArg.selecteditem
$viconnection = $null
if ($global:defaultviserver -ne $null)
{
Disconnect-viserver * -confirm:$false
}
$clusterselection = $null
$DropDownTo = $null
$credential = Get-Credential -Message "Please Enter Elevated Credentials To Access $selectedVC. You can enter assuming you're an administrator on $selectedVC."
$username = $credential.Username
$password = $credential.GetNetworkCredential().Password
$viconnection = connect-viserver $selectedVC -User $username -Password $password
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form2 = New-Object System.Windows.Forms.Form
$Form2.AutoSize = $True
$Form2.AutoSizeMode = "GrowAndShrink"
$Form2.width = 600
$Form2.height = 500
$Form2.Text = ”Vcenter Selection”
$Form2.StartPosition = "CenterScreen"
#First item selection*******************
$DropDownLabel1 = new-object System.Windows.Forms.Label
$DropDownLabel1.Location = new-object System.Drawing.Size(140,10)
$DropDownLabel1.size = new-object System.Drawing.Size(130,30)
$DropDownLabel1.Text = "$selectedVC"
$Form2.Controls.Add($DropDownLabel1)
$DropDownLabel2 = new-object System.Windows.Forms.Label
$DropDownLabel2.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel2.size = new-object System.Drawing.Size(100,40)
$DropDownLabel2.Text = "Connected to Vcenter"
$Form2.Controls.Add($DropDownLabel2)
#Second item selection*******************
[array]$clusterselection = get-cluster
$DropDownTo = New-Object System.Windows.Forms.ComboBox
$DropDownTo.Location = New-Object System.Drawing.Size(140,50)
$DropDownTo.Size = New-Object System.Drawing.Size(130,30)
$DropdownTo.DropDownStyle = 2
ForEach ($Item2 in $clusterselection)
{
[void] $DropDownTo.Items.Add($Item2)
}
$Form2.Controls.Add($DropDownTo)
$DropDownLabel3 = new-object System.Windows.Forms.Label
$DropDownLabel3.Location = new-object System.Drawing.Size(10,50)
$DropDownLabel3.size = new-object System.Drawing.Size(100,40)
$DropDownLabel3.Text = "Select Cluster Location"
$Form2.Controls.Add($DropDownLabel3)
#Third item selection*******************
$DropDownType = New-Object System.Windows.Forms.ComboBox
$DropDownType.Location = New-Object System.Drawing.Size(140,90)
$DropDownType.Size = New-Object System.Drawing.Size(130,30)
$DropDownType.DropDownStyle = 2
ForEach ($Item3 in $typeOfServer)
{
[void] $DropDownType.Items.Add($Item3)
}
$Form2.Controls.Add($DropDownType)
$DropDownLabel4 = new-object System.Windows.Forms.Label
$DropDownLabel4.Location = new-object System.Drawing.Size(10,90)
$DropDownLabel4.size = new-object System.Drawing.Size(100,40)
$DropDownLabel4.Text = "Select Type of Server"
$Form2.Controls.Add($DropDownLabel4)
#Fourth item Input*******************
$InputName = New-Object System.Windows.Forms.Textbox
$InputName.Location = New-Object System.Drawing.Size(140,130)
$InputName.Size = New-Object System.Drawing.Size(130,30)
$InputName.MaxLength = 15
$Form2.Controls.Add($InputName)
$inputLabel = new-object System.Windows.Forms.Label
$inputLabel.Location = new-object System.Drawing.Size(10,130)
$inputLabel.size = new-object System.Drawing.Size(100,40)
$inputLabel.Text = "Enter Name of Server"
$Form2.Controls.Add($inputLabel)
#Fifth item selection*******************
$dropDownMem = New-Object System.Windows.Forms.Combobox
$dropDownMem.Location = New-Object System.Drawing.Size(140,170)
$dropDownMem.Size = New-Object System.Drawing.Size(130,30)
$dropDownMem.DropDownStyle = 2
ForEach ($Item5 in $mem)
{
[void] $dropDownMem.Items.Add($Item5)
}
$Form2.Controls.Add($dropDownMem)
$DropDownLabel5 = new-object System.Windows.Forms.Label
$DropDownLabel5.Location = new-object System.Drawing.Size(10,170)
$DropDownLabel5.size = new-object System.Drawing.Size(100,40)
$DropDownLabel5.Text = "Select Memory Quantity in GB"
$Form2.Controls.Add($DropDownLabel5)
#Sixth item selection*******************
$dropDownvCpu = New-Object System.Windows.Forms.Combobox
$dropDownvCpu.Location = New-Object System.Drawing.Size(140,210)
$dropDownvCpu.Size = New-Object System.Drawing.Size(130,30)
$dropDownvCpu.DropDownStyle = 2
ForEach ($Item6 in $vCpu)
{
[void] $dropDownvCpu.Items.Add($Item6)
}
$Form2.Controls.Add($dropDownvCpu)
$DropDownLabel6 = new-object System.Windows.Forms.Label
$DropDownLabel6.Location = new-object System.Drawing.Size(10,210)
$DropDownLabel6.size = new-object System.Drawing.Size(100,40)
$DropDownLabel6.Text = "Select Virtual CPU's"
$Form2.Controls.Add($DropDownLabel6)
#Seventh item selection*******************
$dropDownOS = New-Object System.Windows.Forms.Combobox
$dropDownOS.Location = New-Object System.Drawing.Size(140,250)
$dropDownOS.Size = New-Object System.Drawing.Size(130,30)
$dropDownOS.DropDownStyle = 2
ForEach ($Item7 in $operatingSystem)
{
[void] $dropDownOS.Items.Add($Item7)
}
$Form2.Controls.Add($dropDownOS)
$DropDownLabel7 = new-object System.Windows.Forms.Label
$DropDownLabel7.Location = new-object System.Drawing.Size(10,250)
$DropDownLabel7.size = new-object System.Drawing.Size(100,40)
$DropDownLabel7.Text = "Select Operating System"
$Form2.Controls.Add($DropDownLabel7)
#Defines buttons for click to accept*********************
#$Button1 = new-object System.Windows.Forms.Button
#$Button1.Location = new-object System.Drawing.Size(10,300)
#$Button1.Size = new-object System.Drawing.Size(100,20)
#$Button1.Text = "Provision Vm"
#$Button1.Add_Click({Provision-VM})
#$Button1.Add_Click({Get-Account $oRphanedAcct})
#Defines buttons for click to accept*********************
$Button2 = new-object System.Windows.Forms.Button
$Button2.Location = new-object System.Drawing.Size(10,330)
$Button2.Size = new-object System.Drawing.Size(100,20)
$Button2.Text = "Provision Vm"
$Button2.Add_Click({Provision-VM -arg1 "$choice4" -arg2 "$choice5" -arg3 "$Choice3"})
#Defines buttons for click to accept*********************
$Button3 = new-object System.Windows.Forms.Button
$Button3.Location = new-object System.Drawing.Size(140,330)
$Button3.Size = new-object System.Drawing.Size(100,20)
$Button3.Text = "Close Form"
#$Button3.Add_Click({Return-Drop})
$Button3.Add_Click({$Form2.Dispose()})
#Defines buttons for click to accept*********************
$Button4 = new-object System.Windows.Forms.Button
$Button4.Location = new-object System.Drawing.Size(275,330)
$Button4.Size = new-object System.Drawing.Size(100,20)
$Button4.Text = "Ok Button"
$Button4.Dialogresult = [System.Windows.Forms.DialogResult]::OK
$Form2.AcceptButton = $Button4
$Form2.Controls.Add($Button2)
$Form2.Controls.Add($Button3)
$Form2.Controls.Add($Button4)
$Form2.Topmost = $True
$Form2.ShowDialog()
$Form2.refresh()
return $DropdownTo.SelectedItem
return $DropDownType.SelectedItem.ToString()
return $InputName.SelectedItem.ToString()
return $DropDownMem.SelectedItem.ToString()
return $DropDownVcpu.SelectedItem.ToString()
return $DropDownOS.SelectedItem.ToString()
return $Button3
return $Button4
}
#Main function definition********************
function Menu
{
$clusterselection = $null
$global:defaultviserver = $null
[array]$VCItems = "VCSelection","VCSelection2","VCSelection3"
[array]$typeOfServer = "Vanilla", "IIS", "SQL", "SQL_Cluster_Node"
[array]$operatingSystem = "Windows2012", "Windows2016"
[array]$mem = "2","4","6","8","10","12","32","64"
[array]$vCpu = "2","4","6","8","10","12"
#Function for Closing Form Upon no input
function Return-Drop
{
If (($dropDownVC.SelectedItem -ne $null) -or ($DropDownTO.SelectedItem -ne $null) -or ($DropDownType.SelectedItem -ne $null))
{
$Form.Close()
}
}
#Function for connecting to Vcenter
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object System.Windows.Forms.Form
$Form.AutoSize = $True
$Form.AutoSizeMode = "GrowAndShrink"
$Form.width = 600
$Form.height = 500
$Form.Text = ”Vcenter Selection”
$Form.StartPosition = "CenterScreen"
#First item selection*******************
$DropDownVC = New-Object System.Windows.Forms.ComboBox
$DropDownVC.Location = New-Object System.Drawing.Size(140,10)
$DropDownVC.Size = New-Object System.Drawing.Size(130,30)
$DropDownVC.DropDownStyle = 2
$DropDownVC.TabIndex = 0
ForEach ($Item1 in $VCItems)
{
[void] $DropDownVC.Items.Add($Item1)
}
$DropDownVC.SelectedItem = $DropDownVC.Items[0]
$Form.Controls.Add($DropDownVC)
$DropDownLabel1 = new-object System.Windows.Forms.Label
$DropDownLabel1.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel1.size = new-object System.Drawing.Size(100,40)
$DropDownLabel1.Text = "Select Vcenter to connect to"
$Form.Controls.Add($DropDownLabel1)
#Defines buttons for click to connect to VC*********************
$Button5 = new-object System.Windows.Forms.Button
$Button5.Location = new-object System.Drawing.Size(290,10)
$Button5.Size = new-object System.Drawing.Size(100,20)
$Button5.Text = "Connect"
$Button5.Add_Click({$form.visible = $false;$t = Connect-Vc -vcArg $DropDownVC})
$Form.Controls.Add($Button5)
$Form.Topmost = $True
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
$Choice1 = $DropDownVC.SelectedItem.ToString()
enter code here
return $Choice1
}
Related
trying to create simple win form in PowerShell. there will be some automatically calculated checkbox and i'm struggling with sizes - seems that 'AutoSize' is a bullsh*t and values returned are from nowhere. can anyone please help and suggest how you create dynamic positioning? based on which values?
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$nrOfNIC = 2
$nrOfDisks = 3
$nrOfPIP = 1
$vShift = 20
$allChkb = 1
$chkForm = New-Object system.Windows.Forms.Form
$chkForm.text = "Remove Resources"
$chkForm.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$chkForm.AutoSize = $true
$chkForm.StartPosition = 'CenterScreen'
$chkForm.FormBorderStyle = 'Fixed3D'
$chkForm.Icon = [System.Drawing.SystemIcons]::Question
$chkForm.Topmost = $true
$chkForm.MaximizeBox = $false
$chkVMBox = new-object System.Windows.Forms.GroupBox
#$chkVMBox.MinimumSize = New-Object System.Drawing.Size(180,100)
$chkVMBox.AutoSize = $true
$chkVMBox.Location = New-Object System.Drawing.Point(10,10)
$chkVMBox.Text = 'VM resources'
#$chkVMBox.Anchor = 'left,top'
#$lastControl = $chkVMBox
if($nrOfDisks -gt 0) {
$chkVMDisks = new-object System.Windows.Forms.GroupBox
#$chkVMDisks.MinimumSize = New-Object System.Drawing.Size(180,20)
$chkVMDisks.Location = New-Object System.Drawing.Point(10,20)
$chkVMDisks.Text = 'DISKs'
#$chkVMDisks.Anchor = 'left,top'
for($disk=0;$disk -lt $nrOfDisks;$disk++) {
$chkbDisk = New-Object System.Windows.Forms.Checkbox
$chkbDisk.Location = New-Object System.Drawing.Point(10, ($vShift+($disk*$vShift)) )
#$chkbDisk.Anchor = 'left,top'
$chkbDisk.AutoSize = $true
$chkbDisk.Text = "disk $disk"
$chkbDisk.TabIndex = $allChkb++
$chkVMDisks.Controls.Add($chkbDisk)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMDisks)
#$lastControl=$chkVMDisks
}
if($nrOfNIC -gt 0) {
#$vLocation = $lastControl.Bottom+$shift
$vLocation = $chkVMDisks.Bottom+$shift
$chkVMNICs = new-object System.Windows.Forms.GroupBox
#$chkVMNICs.MinimumSize = New-Object System.Drawing.Size(180,20)
$chkVMNICs.AutoSize = $true
$chkVMNICs.Location = New-Object System.Drawing.Point(10,$vLocation)
$chkVMNICs.Text = 'NICs'
#$chkVMNICs.Anchor = 'left,top'
for($nic = 0;$nic -lt $nrOfNIC; $nic++) {
$chkbNIC = New-Object System.Windows.Forms.Checkbox
$chkbNIC.Location = New-Object System.Drawing.Point(10, ($vShift+($nic*$vShift)) )
#$chkbNIC.Anchor = 'left,top'
$chkbNIC.AutoSize = $true
$chkbNIC.Text = "nic $nic"
$chkbNIC.TabIndex = $allChkb++
$chkVMNICs.Controls.Add($chkbNIC)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMNICs)
#$lastControl = $chkVMNICs
}
#$vLocation = $lastControl.Bottom+$shift
$vLocation = $chkVMNICs.Bottom+$shift
$chkbVMdiag = New-Object System.Windows.Forms.Checkbox
$chkbVMdiag.Location = New-Object System.Drawing.Point(10,$vLocation)
#$chkbVMdiag.Anchor = 'left,top'
$chkbVMdiag.AutoSize = $true
$chkbVMdiag.Text = "Boot Diagnostics"
$chkbVMdiag.TabIndex = $allChkb++
$chkVMBox.Controls.Add($chkbVMdiag)
$vLocation = $chkVMBox.bottom + 40
$btOK = New-Object System.Windows.Forms.Button
$btOK.Location = New-Object System.Drawing.Size(15,$vLocation)
$btOK.Size = New-Object System.Drawing.Size(70,20)
$btOK.Text = "OK"
$btOK.DialogResult = [System.Windows.Forms.DialogResult]::OK
#$btOK.Anchor = 'left,bottom'
$btCancel = New-Object System.Windows.Forms.Button
$btCancel.Location = New-Object System.Drawing.Size(95,$vLocation)
$btCancel.Size = New-Object System.Drawing.Size(70,20)
$btCancel.Text = "Cancel"
$btCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
#$btCancel.Anchor = 'right,bottom'
$chkForm.AcceptButton = $btOK
$chkForm.CancelButton = $btCancel
$chkForm.Controls.AddRange(#($chkVMBox, $btOK, $btCancel))
[void]$chkForm.ShowDialog()
effect:
effect of code
there is some big padding on the right of the main form
in check box list - there is additional space in first, double the space in second control
there is some padding under last chkbox
chkVMBox (main box) returns 'size = 100' 'bottom = 110'and buttons which uses this value are actually under other controls and invisible.
seems that ALL VALUES returned with 'autosize' are bullsh*t. so how do you create dynamic layout, not knowing sizes/number of elements upfront?
there is some big padding on the right of the main form
in check box list - there is additional space in first, double the space in second control
there is some padding under last chkbox
Set .AutoSizeMode = 'GrowAndShrink' as well as .MinimumSize properties along with the .AutoSize.
buttons … are actually under other controls and invisible
Button positions are derived from $chkVMBox.bottom; use $chkForm.Controls.Add($chkVMBox) before computing vertical location for buttons and remove $chkVMBox from $chkForm.Controls.AddRange(…)
to create dynamic layout, not knowing sizes/number of elements upfront:
I'd try calculating horizontal and vertical positions and (minimal) sizes of controls relative to an invariant; for instance, those are computed from $chkForm.Font in the following code (aspiration apparently inconsistent:), see the $hShift and $vShift variables, their values and usage.
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
Remove-Variable chk* -ErrorAction SilentlyContinue
$nrOfNIC = 2
$nrOfDisks = 3
$nrOfPIP = 1
$allChkb = 1
$chkForm = New-Object System.Windows.Forms.Form
$chkForm.text = "Remove Resources"
$chkForm.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$hShift = $chkForm.Font.SizeInPoints
$vShift = $chkForm.Font.Height * 1.5 ### arbitrary ad hoc coefficient
$chkForm.StartPosition = 'CenterScreen'
$chkForm.FormBorderStyle = 'Fixed3D'
$chkForm.Icon = [System.Drawing.SystemIcons]::Question
$chkForm.Topmost = $true
$chkForm.AutoSize = $true
$chkForm.AutoSizeMode = 'GrowAndShrink'
$chkForm.MinimumSize = [System.Drawing.Size]::new(100,100)
# $chkForm.AutoScaleMode = 'Font'
$chkForm.MaximizeBox = $false
$chkForm.MinimizeBox = $false
$chkVMBox = new-object System.Windows.Forms.GroupBox
$chkVMBox.AutoSizeMode = 'GrowAndShrink'
$chkVMBox.AutoSize = $true
$chkVMBox.Location = New-Object System.Drawing.Point(10,10)
$chkVMBox.Text = 'VM resources'
if($nrOfDisks -gt 0) {
$chkVMDisks = new-object System.Windows.Forms.GroupBox
$chkVMDisks.Location = New-Object System.Drawing.Point($hShift,( $vShift+10))
$chkVMDisks.Text = 'DISKs'
$chkVMDisks.AutoSize = $true
$chkVMDisks.MinimumSize = [System.Drawing.Size]::new(($chkVMBox.Size.Width - $hShift),$vShift)
$chkVMDisks.AutoSizeMode = 'GrowAndShrink'
for($disk=0;$disk -lt $nrOfDisks;$disk++) {
$chkbDisk = New-Object System.Windows.Forms.Checkbox
$chkbDisk.Location = New-Object System.Drawing.Point($hShift, ($vShift*($disk+1)))
$chkbDisk.AutoSize = $true
$chkbDisk.Text = "disk $disk"
$chkbDisk.TabIndex = $allChkb++
$chkVMDisks.Controls.Add($chkbDisk)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMDisks)
#$lastControl=$chkVMDisks
}
if($nrOfNIC -gt 0) {
#$vLocation = $lastControl.Bottom+$hShift
$vLocation = $chkVMDisks.Bottom + $vShift
$chkVMNICs = new-object System.Windows.Forms.GroupBox
$chkVMNICs.AutoSize = $true
$chkVMNICs.MinimumSize = [System.Drawing.Size]::new(($chkVMBox.Size.Width - $hShift),$vShift)
$chkVMNICs.AutoSizeMode = 'GrowAndShrink'
$chkVMNICs.Location = New-Object System.Drawing.Point($hShift,$vLocation)
$chkVMNICs.Text = 'NICs'
for($nic = 0;$nic -lt $nrOfNIC; $nic++) {
$chkbNIC = New-Object System.Windows.Forms.Checkbox
$chkbNIC.Location = New-Object System.Drawing.Point($hShift, ($vShift*($nic+1)) )
# $chkbNIC.Anchor = 'left,top'
$chkbNIC.AutoSize = $true
$chkbNIC.Text = "nic $nic"
$chkbNIC.TabIndex = $allChkb++
$chkVMNICs.Controls.Add($chkbNIC)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMNICs)
#$lastControl = $chkVMNICs
}
#$vLocation = $lastControl.Bottom+$hShift
$vLocation = $chkVMNICs.Bottom + $vShift
$chkbVMdiag = New-Object System.Windows.Forms.Checkbox
$chkbVMdiag.Location = New-Object System.Drawing.Point($hShift,$vLocation)
$chkbVMdiag.AutoSize = $true
$chkbVMdiag.Text = "Boot Diagnostics"
$chkbVMdiag.TabIndex = $allChkb++
$chkVMBox.Controls.Add($chkbVMdiag)
$chkForm.Controls.Add($chkVMBox)
$vLocation = $chkVMBox.bottom + 20
$btOK = New-Object System.Windows.Forms.Button
$btOK.Location = New-Object System.Drawing.Point(15,$vLocation)
# $btOK.Size = New-Object System.Drawing.Size(70,20)
$btOK.AutoSize = $true
$btOK.Text = "OK"
$btOK.DialogResult = [System.Windows.Forms.DialogResult]::OK
$btCancel = New-Object System.Windows.Forms.Button
$btCancel.Location = New-Object System.Drawing.Point(95,$vLocation)
# $btCancel.Size = New-Object System.Drawing.Size(70,20)
$btCancel.AutoSize = $true
$btCancel.Text = "Cancel"
$btCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$chkForm.AcceptButton = $btOK
$chkForm.CancelButton = $btCancel
$chkForm.Controls.AddRange(#($btOK, $btCancel))
[void]$chkForm.ShowDialog()
$chkForm.Dispose()
I have an issue with my PS script including a listview.
I need to check the content of 6 directorys and show the content in 6 different columns :
The issue is that i can't get a correct visual, they are all in the same column
Here is my code :
$Form = New-Object Windows.Forms.Form
$Form.Text = "New Test"
$Form.Width = 1550
$Form.Height = 800
$Listview = New-Object System.Windows.Forms.ListView
$Listview.Location = New-Object System.Drawing.Size(15,10)
$Listview.Size = New-Object System.Drawing.Size(550,10)
$Listview.AutoResizeColumns([System.Windows.Forms.ColumnHeaderAutoResizeStyle]::ColumnContent)
$Listview.View = "Details"
$Listview.FullRowSelect = $true
$Listview.GridLines = $true
$Listview.Height = 650
$Listview.Width =1500
$Listview.AllowColumnReorder = $true
$Listview.Sorting = [System.Windows.Forms.SortOrder]::None
#[void]$Listview.Columns.Add('Null',0)
[void]$Listview.Columns.Add('Scripts',150)
[void]$Listview.Columns.Add('Applications',150)
[void]$Listview.Columns.Add('Systems',500)
[void]$Listview.Columns.Add('Databases',100)
[void]$Listview.Columns.Add('Datas',150)
[void]$Listview.Columns.Add('Backups',500)
$oButton = New-Object Windows.Forms.Button
$oButton.Text = "List"
$oButton.Top = 700
$oButton.Left = 350
$oButton.Width = 150
$oButton.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
$a = (Get-ChildItem "C:\Scripts\").Name
$b = (Get-ChildItem "C:\Applications\").Name
$c = (Get-ChildItem "C:\Systems\").Name
$d = (Get-ChildItem "C:\Databases\").Name
$e = (Get-ChildItem "C:\Datas\").Name
$f = (Get-ChildItem "C:\Backups\").Name
$Entry = New-Object System.Windows.Forms.ListViewItem($_.Null)
foreach($mp in $a){
$Entry1 = New-Object System.Windows.Forms.ListViewItem($_.Scripts)
$Listview.Items.Add($mp)
}
foreach($mp1 in $b){
$Entry2 = New-Object System.Windows.Forms.ListViewItem($_.Applications)
$Listview.Items.Add($Entry2).SubItems.Add($mp1)
}
foreach($mp2 in $c){
$Entry3 = New-Object System.Windows.Forms.ListViewItem($_.Systems)
$Listview.Items.Add($Entry3).SubItems.Add($mp2)
}
foreach($mp3 in $d){
$Entry4 = New-Object System.Windows.Forms.ListViewItem($_.Databases)
$Listview.Items.Add($Entry4).SubItems.Add($mp3)
}
foreach($mp4 in $e){
$Entry5 = New-Object System.Windows.Forms.ListViewItem($_.Datas)
$Listview.Items.Add($Entry5).SubItems.Add($mp4)
}
foreach($mp5 in $f){
$Entry6 = New-Object System.Windows.Forms.ListViewItem($_.Backup)
$Listview.Items.Add($Entry6).SubItems.Add($mp5)
}
$Form.Add_Shown({$Form.Activate()})
$Form.controls.add($oButton)
$Form.controls.add($Listview)
$Form.ShowDialog()
I tried a lot of change but im still blocked
Thanks in advance for you help
Regards
Should be pretty straight forward but cant work out the logic. Below is the code I currently have which sort of works but the only problem is if more than one checkboxes are checked and you uncheck one the button disables, I need the button to remain enabled as long as there is one or more checkbox checked. I've also tried various convoluted If and Elseif statements but nothing I've tried changes this behaviour.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '170,191'
$Form.text = "Scheduler"
$Form.TopMost = $false
$MonCheckBox = New-Object system.Windows.Forms.CheckBox
$MonCheckBox.text = "Monday"
$MonCheckBox.AutoSize = $false
$MonCheckBox.width = 95
$MonCheckBox.height = 20
$MonCheckBox.location = New-Object System.Drawing.Point(30,30)
$MonCheckBox.Font = 'Microsoft Sans Serif,10'
$TueCheckBox = New-Object system.Windows.Forms.CheckBox
$TueCheckBox.text = "Tuesday"
$TueCheckBox.AutoSize = $false
$TueCheckBox.width = 95
$TueCheckBox.height = 20
$TueCheckBox.location = New-Object System.Drawing.Point(30,50)
$TueCheckBox.Font = 'Microsoft Sans Serif,10'
$WedCheckBox = New-Object system.Windows.Forms.CheckBox
$WedCheckBox.text = "Wednesday"
$WedCheckBox.AutoSize = $false
$WedCheckBox.width = 95
$WedCheckBox.height = 20
$WedCheckBox.location = New-Object System.Drawing.Point(30,70)
$WedCheckBox.Font = 'Microsoft Sans Serif,10'
$ThuCheckBox = New-Object system.Windows.Forms.CheckBox
$ThuCheckBox.text = "Thursday"
$ThuCheckBox.AutoSize = $false
$ThuCheckBox.width = 95
$ThuCheckBox.height = 20
$ThuCheckBox.location = New-Object System.Drawing.Point(30,90)
$ThuCheckBox.Font = 'Microsoft Sans Serif,10'
$FriCheckBox = New-Object system.Windows.Forms.CheckBox
$FriCheckBox.text = "Friday"
$FriCheckBox.AutoSize = $false
$FriCheckBox.width = 95
$FriCheckBox.height = 20
$FriCheckBox.location = New-Object System.Drawing.Point(30,110)
$FriCheckBox.Font = 'Microsoft Sans Serif,10'
$SchedButton = New-Object system.Windows.Forms.Button
$SchedButton.text = "Schedule"
$SchedButton.width = 60
$SchedButton.height = 30
$SchedButton.location = New-Object System.Drawing.Point(30,134)
$SchedButton.Font = 'Microsoft Sans Serif,10'
$SchedButton.Autosize = $true
$SchedButton.Enabled = $false
$Form.controls.AddRange(#($MonCheckBox,$TueCheckBox,$WedCheckBox,$ThuCheckBox,$FriCheckBox,$SchedButton))
$MonCheckBox.add_CheckedChanged({$SchedButton.Enabled = $MonCheckBox.Checked})
$TueCheckBox.add_CheckedChanged({$SchedButton.Enabled = $TueCheckBox.Checked})
$WedCheckBox.add_CheckedChanged({$SchedButton.Enabled = $WedCheckBox.Checked})
$ThuCheckBox.add_CheckedChanged({$SchedButton.Enabled = $ThuCheckBox.Checked})
$FriCheckBox.add_CheckedChanged({$SchedButton.Enabled = $FriCheckBox.Checked})
[void]$Form.ShowDialog()
I would do something like this:
$Form.controls.AddRange(#($MonCheckBox, $TueCheckBox, $WedCheckBox, $ThuCheckBox, $FriCheckBox, $SchedButton))
Function Test-AnyButtonChecked {
if (
$MonCheckBox.Checked -or
$TueCheckBox.Checked -or
$WedCheckBox.Checked -or
$ThuCheckBox.Checked -or
$FriCheckBox.Checked
) {
$SchedButton.Enabled = $true
}
else {
$SchedButton.Enabled = $false
}
}
$MonCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$TueCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$WedCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$ThuCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$FriCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
[void]$Form.ShowDialog()
I currently have a script that queries our AD for the users' AD attribute "Department".
Right now, the script will run "successfully" but all output for Textbox2 goes to the console instead of TextBox2.
If I change my function Get-CCUsers to be a query without variables, like Get-ADUser -Filter "Department -eq 19330" (we use numbers for our departments) then the output shows in TextBox2 as I want it to.
How can I get my function to populate TextBox2?
BTW, this script was cobbled together with my limited understanding, and there may well be superfluous or nonsense lines in here.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Howard Center Profile Migration'
$form.Size = New-Object System.Drawing.Size(800,650)
$form.StartPosition = 'CenterScreen'
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the Cost Center # in the space below:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(100,20)
$form.Controls.Add($textBox)
$RunButton = New-Object System.Windows.Forms.Button
$RunButton.Location = New-Object System.Drawing.Point(75,120)
$RunButton.Size = New-Object System.Drawing.Size(75,23)
$RunButton.Text = 'RUN'
#$RunButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
<#$RunButton.Add_Click({
#add here code triggered by the event
$TextBox2.Text = Get-Process | Format-Table -Property ProcessName, Id, CPU -AutoSize | Out-String
})
#>
$form.AcceptButton = $RunButton
$form.Controls.Add($RunButton)
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,70)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'When you are ready click the Run button below'
$form.Controls.Add($label2)
$TextBox2 = New-Object system.windows.Forms.TextBox
$TextBox2.Text = ""
$TextBox2.Multiline = $true
$TextBox2.BackColor = "#013686"
$TextBox2.ScrollBars = "Both"
$TextBox2.Width = 750
$TextBox2.Height = 450
$TextBox2.location = new-object system.drawing.point(10,150)
$TextBox2.Font = "Microsoft Sans Serif,10"
$TextBox2.ForeColor = "#ffffff"
$Form.controls.Add($TextBox2)
$form.Topmost = $true
function Get-CCUsers {
Write-Host "The textbox text is $textbox.Text"
$dept = $textBox.Text
$deptUsers = Get-ADUser -Filter "Department -eq $dept"
ForEach ($user in $deptUsers) {
IF ( ((get-aduser $user).enabled) -eq $True ) {
$UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
$UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
Write-Host "$user, $UHomeDir, $UProfPath"
}
}
}
$RunButton.Add_Click({
$TextBox2.Text = Get-CCUsers
})
$TextBox2.Add_TextChanged({
$TextBox2.SelectionStart = $TextBox2.Text.Length
$TextBox2.ScrollToCaret()
})
$form.Add_Shown({$Form.Update()})
$result = $form.ShowDialog()
$global:x = $textBox.Text
# $x
# if ($result -eq [System.Windows.Forms.DialogResult]::OK)
#{
#}
I have made your script working changing the 'Get-CCUSers' function
function Get-CCUsers {
Write-Host "The textbox text is $textbox.Text"
$dept = $textBox.Text
$deptUsers = Get-ADUser -Filter "Department -eq '$dept'"
$res = #()
ForEach ($user in $deptUsers) {
IF ( ((get-aduser $user).enabled) -eq $True ) {
$UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
$UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
$res += "$user, $UHomeDir, $UProfPath"
}
}
return $res
}
In short:
I removed the Write-Host (the output was actually redirected to stdout)
I added a $res in the function initialized as array
In the ForEach loop, results are added as items in the array
The $res is returned by the function, and then used $RunButton
I have a power-shell form prompting user if they would like to postpone a shutdown time (the time of the shutdown is given by another ps script as an argument which is calculated in the initial script).
I'd like to close the form after 30 minutes of inactivity, how would I go about this?
Powershell Code (Form):
#creating the form object
$form = New-Object System.Windows.Forms.Form
#setting the from title
$form.Text = "System warning"
#setting the form dimesnions and positions
$form.Size = New-Object System.Drawing.Size(300,250)
$form.StartPosition = "CenterScreen"
#claculate time before shut down:
$StartDate=(GET-DATE)
#this ill have to be replaced by a parameter which indicates the shut down date
$EndDate = $EndDate | Get-Date
$timeDifference = NEW-TIMESPAN –Start $StartDate –End $EndDate
$secondsTilShutdown = $timeDifference.TotalSeconds
#time remaining message
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,40)
$label.Text = "This computer is scheduled to shutdown at " + $EndDate
$form.Controls.Add($label)
#second message
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,70)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Postpone the shutdown by : "
$form.Controls.Add($label)
#setting up the drop down box (time in minutes)
$TimePeriods = 60, 120, 180, 240
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(10,90)
$DropDown.Size = new-object System.Drawing.Size(130,30)
ForEach ($time in $TimePeriods) {
[void] $DropDown.Items.Add([string]($time/60) + " hours")
}
#1 hour is the default
$DropDown.SelectedItem = $DropDown.Items[0]
$Form.Controls.Add($DropDown)
#creating the postpone button
$OKButton = New-Object System.Windows.Forms.Button
#position of the button on the form
$OKButton.Location = New-Object System.Drawing.Point(25,130)
#size of the button
$OKButton.Size = New-Object System.Drawing.Size(100,50)
#text of the button
$OKButton.Text = "Postpone"
#the value that the from dialog will return if we click on ok
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
#assigns a key down on the enter key to the button
$form.AcceptButton = $OKButton
#adds the button to the form
$form.Controls.Add($OKButton)
#creating the ignore button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(175,130)
$CancelButton.Size = New-Object System.Drawing.Size(100,50)
$CancelButton.Text = "Continue with scheduled shutdown"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)
$form.Topmost = $True
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
###processing form results :
#if the postpone button button was selected
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
#logic
}
elseif($result -eq [System.Windows.Forms.DialogResult]::Cancel)
{
#we do not push back the shut down time
}
use a timer, i give you a example to timer wich close form
Function ClearAndClose()
{
$Timer.Stop();
$Form.Close();
$Form.Dispose();
$Timer.Dispose();
$Script:CountDown=5
}
Function Button_Click()
{
ClearAndClose
}
Function Timer_Tick()
{
$Label.Text = "Your system will reboot in $Script:CountDown seconds"
--$Script:CountDown
if ($Script:CountDown -lt 0)
{
ClearAndClose
}
}
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Attention redémarrage!!"
$Form.Size = New-Object System.Drawing.Size(250,100)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
$Label = New-Object System.Windows.Forms.Label
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(20,5)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(55,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "STOP"
$Button.DialogResult=[System.Windows.Forms.DialogResult]::OK
$Timer = New-Object System.Windows.Forms.Timer
$Timer.Interval = 1000
$Form.Controls.Add($Label)
$Form.Controls.Add($Button)
$Script:CountDown = 6
$Button.Add_Click({Button_Click})
$Timer.Add_Tick({ Timer_Tick})
$Timer.Start()
$Form.ShowDialog()