VMware Cloud Community
No_Way
Enthusiast
Enthusiast
Jump to solution

Extact Script to a list in a Textbox (or something different)

Hi all,

I am trying to build a small code and dont know realy how to do it

I have this small script

ForEach ($VMHost in Get-VMHost)

    {

            $VMView = $VMHost | Get-View

                  write-host   "Host Hostname: " $VMHost.Name, "Host Processor Type: " $VMHost.ProcessorType, "Host Manufacturer: " $VMHost.Manufacturer, "Host Version: " $VMHost.Version "Build " $VMHost.Build

    }

That I will like to add in my code and present it to a textbox and list all the results on that textbox.

I am using PowerShell Studio(so I will user Powershell and Visual Studio)

Thank You in advance.

NW

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You mean something like this ?

$txt = ''

ForEach ($VMHost in Get-VMHost){

    $txt += "Hostname: $($VMHost.Name) Processor Type: $($VMHost.ProcessorType) Manufacturer: $($VMhost.Manufacturer) Version: $($VMHost.Version) Build: $($VMHost.Build)`r`n"

}

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$Form = New-Object System.Windows.Forms.Form

$Form.Size = New-Object System.Drawing.Size(900,250)

$Form.StartPosition = "CenterScreen"

$TextBox = New-Object System.Windows.Forms.TextBox

$TextBox.Size = New-Object System.Drawing.Size(850,225)

$TextBox.Multiline = $true

$TextBox.Text = $txt

$Form.Controls.Add($TextBox)

$Form.Topmost = $True

[void] $Form.ShowDialog()


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
Wh33ly
Hot Shot
Hot Shot
Jump to solution

What about :

Get-VMhost | select Name,ProcessorType,Manufacturer,Version,Build | Out-GridView

Or to just use Get-VMhost | select Name,ProcessorType,Manufacturer,Version,Build

to retrieve the data you need

No_Way
Enthusiast
Enthusiast
Jump to solution

Hi Wh33ly,

Thank you for your reply.

Yes that is a way to present the data, but I need to send this to a particularly TextBox(and maybe create a array with the data), so using Out-GridView is not an option

Thank you

NW

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean something like this ?

$txt = ''

ForEach ($VMHost in Get-VMHost){

    $txt += "Hostname: $($VMHost.Name) Processor Type: $($VMHost.ProcessorType) Manufacturer: $($VMhost.Manufacturer) Version: $($VMHost.Version) Build: $($VMHost.Build)`r`n"

}

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$Form = New-Object System.Windows.Forms.Form

$Form.Size = New-Object System.Drawing.Size(900,250)

$Form.StartPosition = "CenterScreen"

$TextBox = New-Object System.Windows.Forms.TextBox

$TextBox.Size = New-Object System.Drawing.Size(850,225)

$TextBox.Multiline = $true

$TextBox.Text = $txt

$Form.Controls.Add($TextBox)

$Form.Topmost = $True

[void] $Form.ShowDialog()


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
No_Way
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Yes something like that. Almost there Smiley Happy

So I have my design my form($MainForm) and on the form I have the textbox($textbox4) already in my code(since I am using Powershell Studio we can just drag and drop to create the design forms.

I have created a button to connect to the vCenter(or host depending what IP address is given) and when there is a connection I run the code to show all the hosts details.

So if I want to transport your script/code to my Form code I just need to use this:

$txt = ''

$VMhosts = Get-VMHost -Server $VIServer

Foreach ($VMhost in $VMhosts)

  {

    $txt += "Hostname: $($VMHost.Name) Processor Type: $($VMHost.ProcessorType) Manufacturer: $($VMhost.Manufacturer) Version: $($VMHost.Version) Build: $($VMHost.Build)`r`n"

      }

$TextBox4.Multiline = $true

$TextBox4.Text = $txt

Is working, just trying to auto extend the textbox while the list grows.

But LucD, thank you this will do.

NW

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If the content of the listbox becomes too big, you should make sure that the scrollbars are enabled.

Something like:

$Textbox4.ScrollBars = "Vertical"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

No_Way
Enthusiast
Enthusiast
Jump to solution

Hi,

Yes did that part already.

Because this vCenter that I am testing has more than 100 hosts. So too many to fit in just one small textbox.

NW

0 Kudos