Contenidos
- Crear el formulario en PowerShell
- Abrir Microsoft Spy++
- Localizar la clase mediante la opción «Buscar ventana» en Microsoft Spy++
- En el caso concreto del formulario que hemos creado la ventana tiene los siguiente datos
- Código en PowerShell que sirve para escribir un texto en la caja de texto del formulario creado en PowerShell de forma automática
Crear el formulario en PowerShell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#Crear un formulario, añadir una etiqueta, dos botones y una caja de texto #Funcionalidad para el formulario: #Pulsar la tecla Enter almacena en una variable el contenido de la caja de texto y se muestra #Pulsar la tecla Escape sale del formulario #Formulario $Form = New-Object System.Windows.Forms.Form $Form.Text="Formulario" $Form.Size=New-Object System.Drawing.Size(500,500) $Form.StartPosition="CenterScreen" #Etiqueta $Label=New-Object System.Windows.Forms.Label $Label.Text="Etiqueta de ejemplo" $Label.AutoSize=$True $Label.Location=New-Object System.Drawing.Size(160,160) #Caja de texto $TextBox = New-Object System.Windows.Forms.TextBox $TextBox.Location = New-Object System.Drawing.Size(100,180) $TextBox.Size = New-Object System.Drawing.Size(260,20) #Botón1 $Button1=New-Object System.Windows.Forms.Button $Button1.Size=New-Object System.Drawing.Size(75,23) $Button1.Text="Botón OK" $Button1.Location=New-Object System.Drawing.Size(120,220) #Botón2 $Button2=New-Object System.Windows.Forms.Button $Button2.Size=New-Object System.Drawing.Size(75,23) $Button2.Text="Botón Cancelar" $Button2.Location=New-Object System.Drawing.Size(220,220) #Funcionalidad para el formulario: #Pulsar la tecla Enter almacena en una variable el contenido de la caja de texto y se muestra #Pulsar la tecla Escape sale del formulario $Form.KeyPreview = $True $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$Var=$TextBox.Text;Write-Host $Var;$Form.Close()}}) $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$Form.Close()}}) #Funcionalidad para el botón: #Pulsar Enter almacena en una variable el contenido de la caja de texto y se muestra #Pulsar Escape sale del formulario $Button1.Add_Click({$Var=$TextBox.Text;Write-Host $Var}) $Button2.Add_Click({$Form.Close()}) #Añadir etiqueta $Form.Controls.Add($Label) #Añadir botones $Form.Controls.Add($Button1) $Form.Controls.Add($Button2) #Añadir caja de texto $Form.Controls.Add($TextBox) $Form.ShowDialog() |

Abrir Microsoft Spy++

Localizar la clase mediante la opción «Buscar ventana» en Microsoft Spy++

En el caso concreto del formulario que hemos creado la ventana tiene los siguiente datos

Código en PowerShell que sirve para escribir un texto en la caja de texto del formulario creado en PowerShell de forma automática
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$codigo=' [DllImport("user32.dll", EntryPoint = "FindWindowEx")]public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("User32.dll")]public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam); ' # Localizar el proceso de PowerShell_ISE que tiene abierto el formulario, en el siguiente caso hay que utilizar el primer PowerShell_ISE abiero, es decir $proceso[0] $proceso = Get-Process powershell_ise $acciones = Add-Type -MemberDefinition $codigo -Name TextoNotepad -PassThru # FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow) # SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam) # Tenemos que localizar la clase mediante Microsoft Spy++ $clase = "WindowsForms10.EDIT.app.0.141b42a_r6_ad1" $acciones::SendMessage([IntPtr]$acciones::FindWindowEx($proceso[0].MainWindowHandle, [IntPtr]::Zero, $clase, $null), 0x000C, 0, "Texto escribir") |
