Knowledgebase |
Jugando con Powershell estos días me ha dado por testear algunas cosillas que os podrán ser de utilidad y que os darán para aquellos que aún no se han "metido" a nivel de desarrollo con PowerShell de lo que se puede llegar a realizar.
Para ello he realizado esta pequeña Tool, a nivel puramente educativo , la cual realiza una serie de acciones sobre un Hypervisor basado en Hyper-V, que espero sea de vuestro agrado y de utilidad o si queréis aumentar las funcionalidades os lo dejo a vuestra elección, adjunto además todo el código fuente, solo hay que grabarlo con la extensión .PS1 y ejecutarlo desde PowerShell.
Código Fuente. (Espero que os pueda ser de utilidad)
function OnApplicationLoad {
return $true
}
function OnApplicationExit {
$script:ExitCode = 0
}
function Call-HyperV-VM_pff {
[void][reflection.assembly]::Load("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[void][reflection.assembly]::Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[System.Windows.Forms.Application]::EnableVisualStyles()
$formHyperVVMC2016Devlope = New-Object 'System.Windows.Forms.Form'
$groupbox1 = New-Object 'System.Windows.Forms.GroupBox'
$labelL = New-Object 'System.Windows.Forms.Label'
$labelControllerType = New-Object 'System.Windows.Forms.Label'
$labelVMPath = New-Object 'System.Windows.Forms.Label'
$labelPath = New-Object 'System.Windows.Forms.Label'
$buttonStopVM = New-Object 'System.Windows.Forms.Button'
$buttonStartVM = New-Object 'System.Windows.Forms.Button'
$buttonClear = New-Object 'System.Windows.Forms.Button'
$panel1 = New-Object 'System.Windows.Forms.Panel'
$labelTiempoDeEjecución = New-Object 'System.Windows.Forms.Label'
$labelVersión = New-Object 'System.Windows.Forms.Label'
$labelEstado = New-Object 'System.Windows.Forms.Label'
$labelNombreVM = New-Object 'System.Windows.Forms.Label'
$label6 = New-Object 'System.Windows.Forms.Label'
$label5 = New-Object 'System.Windows.Forms.Label'
$label4 = New-Object 'System.Windows.Forms.Label'
$label3 = New-Object 'System.Windows.Forms.Label'
$buttonSelectVM = New-Object 'System.Windows.Forms.Button'
$listbox1 = New-Object 'System.Windows.Forms.ListBox'
$labelSeleccioneLaVMDeLaCu = New-Object 'System.Windows.Forms.Label'
$labelActualmenteEstáConec = New-Object 'System.Windows.Forms.Label'
$label2 = New-Object 'System.Windows.Forms.Label'
$label1 = New-Object 'System.Windows.Forms.Label'
$buttonExit = New-Object 'System.Windows.Forms.Button'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
$FormEvent_Load = {
#TODO: Initialize Form Controls here
#Code by Javier Sanchez Alcazar (C) 2016 - CTXDOM.COM Community
#Free Distribution, developed with PowerShell Studio 2012.
$vmHostName = (Get-VMHost).Name
$io = Get-CimInstance -ClassName Win32_OperatingSystem
$label1.Text = "Windows Version$($io.Version) - Windows PowerShell $($PSVersionTable.PSVersion)"
$label2.Text = $vmHostName
Load-ListBox $listBox1 (Get-VM) "Name"
$label3.Text = ""
}
#region Control Helper Functions
function Load-ListBox
{
Param (
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
[System.Windows.Forms.ListBox]$ListBox,
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
$Items,
[Parameter(Mandatory=$false)]
[string]$DisplayMember,
[switch]$Append
)
if(-not $Append)
{
$listBox.Items.Clear()
}
if($Items -is [System.Windows.Forms.ListBox+ObjectCollection])
{
$listBox.Items.AddRange($Items)
}
elseif ($Items -is [Array])
{
$listBox.BeginUpdate()
foreach($obj in $Items)
{
$listBox.Items.Add($obj)
}
$listBox.EndUpdate()
}
else
{
$listBox.Items.Add($Items)
}
$listBox.DisplayMember = $DisplayMember
}
#endregion
$buttonSelectVM_Click = {
# Funcionalidades para el parametro correspondiente.
# Relación de posibles parámetros a utilizar en esta Tool.
# $label3.Text = $($listBox1.SelectedItem).Name
# Uptime
# MemoeryAssignated
# CPUUsage
# Version
$label3.Text = $($listBox1.SelectedItem).Name
$label4.Text = $($listBox1.SelectedItem).State
$label5.Text = $($listBox1.SelectedItem).version
$label6.Text = $($listBox1.SelectedItem).uptime
# Información adicional sobre la VM.
# Get-VMHardDiskDrive
#
# Localización Dónde se encuentran los ficheros PATH.
$labelPath.Text = (Get-VMHardDiskDrive $($listbox1.SelectedItem).Name).Path
$labelL.Text = (Get-VMHardDiskDrive $($listbox1.SelectedItem).Name).ControllerType
}
$buttonClear_Click = {
$label3.Text = ""
$label4.Text = ""
$label5.Text = ""
$label6.Text = ""
$labelL.Text = ""
#COn 1, sale button Aceptar y Cancelar
#1,48 Admiración cuadro amarillo
#::Question sale interrogante, en lugar de Show.
#::Hand sale cruz de cancelar, en lugar de Show.
[System.Windows.Forms.MessageBox]::Show("Información Limpiada","Clear Button",0)
}
$label4_Click = {
}
$buttonStartVM_Click = {
Start-VM $($listBox1.SelectedItem).Name
}
$buttonStopVM_Click = {
Stop-VM $($listBox1.SelectedItem).Name
}
$Form_StateCorrection_Load =
{
$formHyperVVMC2016Devlope.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed =
{
try
{
$buttonStopVM.remove_Click($buttonStopVM_Click)
$buttonStartVM.remove_Click($buttonStartVM_Click)
$buttonClear.remove_Click($buttonClear_Click)
$label4.remove_Click($label4_Click)
$buttonSelectVM.remove_Click($buttonSelectVM_Click)
$formHyperVVMC2016Devlope.remove_Load($FormEvent_Load)
$formHyperVVMC2016Devlope.remove_Load($Form_StateCorrection_Load)
$formHyperVVMC2016Devlope.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
#endregion Generated Events
$formHyperVVMC2016Devlope.Controls.Add($groupbox1)
$formHyperVVMC2016Devlope.Controls.Add($buttonStopVM)
$formHyperVVMC2016Devlope.Controls.Add($buttonStartVM)
$formHyperVVMC2016Devlope.Controls.Add($buttonClear)
$formHyperVVMC2016Devlope.Controls.Add($panel1)
$formHyperVVMC2016Devlope.Controls.Add($buttonSelectVM)
$formHyperVVMC2016Devlope.Controls.Add($listbox1)
$formHyperVVMC2016Devlope.Controls.Add($labelSeleccioneLaVMDeLaCu)
$formHyperVVMC2016Devlope.Controls.Add($labelActualmenteEstáConec)
$formHyperVVMC2016Devlope.Controls.Add($label2)
$formHyperVVMC2016Devlope.Controls.Add($label1)
$formHyperVVMC2016Devlope.Controls.Add($buttonExit)
$formHyperVVMC2016Devlope.AcceptButton = $buttonExit
$formHyperVVMC2016Devlope.ClientSize = '820, 394'
$formHyperVVMC2016Devlope.FormBorderStyle = 'FixedDialog'
$formHyperVVMC2016Devlope.MaximizeBox = $False
$formHyperVVMC2016Devlope.MinimizeBox = $False
$formHyperVVMC2016Devlope.Name = "formHyperVVMC2016Devlope"
$formHyperVVMC2016Devlope.StartPosition = 'CenterScreen'
$formHyperVVMC2016Devlope.Text = "Hyper-V VM (C)2016 Devloped by xavisan (CTXDOM.COM Community)"
$formHyperVVMC2016Devlope.add_Load($FormEvent_Load)
$groupbox1.Controls.Add($labelL)
$groupbox1.Controls.Add($labelControllerType)
$groupbox1.Controls.Add($labelVMPath)
$groupbox1.Controls.Add($labelPath)
$groupbox1.BackColor = 'ControlDark'
$groupbox1.Location = '437, 170'
$groupbox1.Name = "groupbox1"
$groupbox1.Size = '374, 182'
$groupbox1.TabIndex = 11
$groupbox1.TabStop = $False
$groupbox1.Text = "VM Información adicional"
$labelL.Location = '225, 30'
$labelL.Name = "labelL"
$labelL.Size = '100, 23'
$labelL.TabIndex = 3
$labelControllerType.Location = '138, 35'
$labelControllerType.Name = "labelControllerType"
$labelControllerType.Size = '100, 23'
$labelControllerType.TabIndex = 2
$labelControllerType.Text = "Controller Type"
$labelVMPath.Location = '6, 35'
$labelVMPath.Name = "labelVMPath"
$labelVMPath.Size = '100, 23'
$labelVMPath.TabIndex = 1
$labelVMPath.Text = "VM Path"
$labelPath.Location = '6, 66'
$labelPath.Name = "labelPath"
$labelPath.Size = '362, 101'
$labelPath.TabIndex = 0
$buttonStopVM.Location = '518, 358'
$buttonStopVM.Name = "buttonStopVM"
$buttonStopVM.Size = '75, 23'
$buttonStopVM.TabIndex = 10
$buttonStopVM.Text = "Stop VM"
$buttonStopVM.UseVisualStyleBackColor = $True
$buttonStopVM.add_Click($buttonStopVM_Click)
$buttonStartVM.Location = '437, 358'
$buttonStartVM.Name = "buttonStartVM"
$buttonStartVM.Size = '75, 23'
$buttonStartVM.TabIndex = 9
$buttonStartVM.Text = "Start VM"
$buttonStartVM.UseVisualStyleBackColor = $True
$buttonStartVM.add_Click($buttonStartVM_Click)
$buttonClear.Location = '13, 343'
$buttonClear.Name = "buttonClear"
$buttonClear.Size = '75, 23'
$buttonClear.TabIndex = 8
$buttonClear.Text = "Clear"
$buttonClear.UseVisualStyleBackColor = $True
$buttonClear.add_Click($buttonClear_Click)
$panel1.Controls.Add($labelTiempoDeEjecución)
$panel1.Controls.Add($labelVersión)
$panel1.Controls.Add($labelEstado)
$panel1.Controls.Add($labelNombreVM)
$panel1.Controls.Add($label6)
$panel1.Controls.Add($label5)
$panel1.Controls.Add($label4)
$panel1.Controls.Add($label3)
$panel1.BackColor = 'ControlDark'
$panel1.Location = '437, 13'
$panel1.Name = "panel1"
$panel1.Size = '371, 138'
$panel1.TabIndex = 7
$labelTiempoDeEjecución.Location = '4, 98'
$labelTiempoDeEjecución.Name = "labelTiempoDeEjecución"
$labelTiempoDeEjecución.Size = '128, 23'
$labelTiempoDeEjecución.TabIndex = 7
$labelTiempoDeEjecución.Text = "Tiempo de Ejecución :"
$labelVersión.Location = '4, 75'
$labelVersión.Name = "labelVersión"
$labelVersión.Size = '100, 23'
$labelVersión.TabIndex = 6
$labelVersión.Text = "Versión :"
$labelEstado.Location = '4, 52'
$labelEstado.Name = "labelEstado"
$labelEstado.Size = '100, 23'
$labelEstado.TabIndex = 5
$labelEstado.Text = "Estado :"
$labelNombreVM.Location = '4, 30'
$labelNombreVM.Name = "labelNombreVM"
$labelNombreVM.Size = '71, 23'
$labelNombreVM.TabIndex = 4
$labelNombreVM.Text = "Nombre VM :"
$label6.Location = '138, 98'
$label6.Name = "label6"
$label6.Size = '153, 23'
$label6.TabIndex = 3
$label5.Location = '138, 75'
$label5.Name = "label5"
$label5.Size = '153, 23'
$label5.TabIndex = 2
$label4.Location = '138, 52'
$label4.Name = "label4"
$label4.Size = '135, 23'
$label4.TabIndex = 1
$label4.add_Click($label4_Click)
$label3.Location = '81, 30'
$label3.Name = "label3"
$label3.Size = '287, 22'
$label3.TabIndex = 0
$buttonSelectVM.Location = '94, 343'
$buttonSelectVM.Name = "buttonSelectVM"
$buttonSelectVM.Size = '75, 23'
$buttonSelectVM.TabIndex = 6
$buttonSelectVM.Text = "Select VM"
$buttonSelectVM.UseVisualStyleBackColor = $True
$buttonSelectVM.add_Click($buttonSelectVM_Click)
$listbox1.FormattingEnabled = $True
$listbox1.Location = '13, 99'
$listbox1.Name = "listbox1"
$listbox1.Size = '403, 238'
$listbox1.TabIndex = 5
$labelSeleccioneLaVMDeLaCu.Location = '13, 72'
$labelSeleccioneLaVMDeLaCu.Name = "labelSeleccioneLaVMDeLaCu"
$labelSeleccioneLaVMDeLaCu.Size = '294, 23'
$labelSeleccioneLaVMDeLaCu.TabIndex = 4
$labelSeleccioneLaVMDeLaCu.Text = "Seleccione la VM de la cual desea información:"
$labelActualmenteEstáConec.Location = '13, 45'
$labelActualmenteEstáConec.Name = "labelActualmenteEstáConec"
$labelActualmenteEstáConec.Size = '256, 23'
$labelActualmenteEstáConec.TabIndex = 3
$labelActualmenteEstáConec.Text = "Actualmente está conectado al Host de Hyper-V: "
$label2.Location = '275, 45'
$label2.Name = "label2"
$label2.Size = '165, 23'
$label2.TabIndex = 2
$label2.Text = "label2"
$label1.Location = '13, 13'
$label1.Name = "label1"
$label1.Size = '414, 29'
$label1.TabIndex = 1
$label1.Text = "label1"
$buttonExit.Anchor = 'Bottom, Right'
$buttonExit.DialogResult = 'OK'
$buttonExit.Location = '730, 358'
$buttonExit.Name = "buttonExit"
$buttonExit.Size = '75, 23'
$buttonExit.TabIndex = 0
$buttonExit.Text = "Exit"
$buttonExit.UseVisualStyleBackColor = $True
$InitialFormWindowState = $formHyperVVMC2016Devlope.WindowState
#Init the OnLoad event to correct the initial state of the form
$formHyperVVMC2016Devlope.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$formHyperVVMC2016Devlope.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $formHyperVVMC2016Devlope.ShowDialog()
}
if((OnApplicationLoad) -eq $true)
{
Call-HyperV-VM_pff | Out-Null
OnApplicationExit
}
Actualizado (Martes, 29 de Marzo de 2016 10:00)
Últimas noticias
- Hotfix XS82ECU1060 - For Citrix Hypervisor 8.2 Cumulative Update 1
- Citrix Hypervisor Security Bulletin for CVE-2023-46838
- Lo siguiente de Citrix Virtual Apps and Desktop LTSR
- CITRIX USER GROUP ESPAÑA
- Reactivamos
- Vulnerabilidad en la aplicación MOVEit / NetScaler
- Cómo instalar Hyper-V en Windows Home
- Solución: No aparece la opción de “Virus & Threat protection”