Load GUI from separate file - powershell

I have been making an gui for my work, and everything is working perfect with Windows form.
The problem is now that i want to save all the Design and form region into another file. and just load it. Reason: so we can change the design of the GUI if needed and just edit the GUI.ps1 file.
I had the idea that it is just the Main.ps1 that need to be executed. when we need to use the Program.
Any help would be nice.
Main.ps1
|_GUI.ps1
|_Functions-files.ps1
Gui.ps1
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$FMmain = New-Object System.Windows.Forms.Form
$TCmain = New-Object System.Windows.Forms.TabControl
.....
$FMmain.showdialog()

Related

How to send keyboard input only to a specific application

I'm very new to Powershell. I wrote a simple script basically to automate grinding in a video game when I'm away from my computer. I wanted to know if there's a way instead to 'target' the button presses to a particular window/application/etc. while still working on the same machine in other windows. As it stands, I have to have the game window focused for the script to work so it's only really useful if I'm AFK. But if I could have it running in the background while working on other things that would be useful.
$WShell = New-Object -Com Wscript.Shell
while (1) {sleep 1; [System.Windows.Forms.SendKeys]::SendWait("{LEFT}"*80);
[System.Windows.Forms.SendKeys]::SendWait("{RIGHT}"*80);sleep 1;
[System.Windows.Forms.SendKeys]::SendWait("{C}"*20)}
Since I'm new to Powershell I'm not sure if this is doable or not. If not, it's no problem, this is a pretty frivolous use case, I was just curious.
Think about what you're currently doing. . .it's almost the same as tying the shoe laces on a pair of shoes, just to put on different ones;) You're not using the $wShell anywhere in your script.
. . .but, you're almost there already. We can start by loading the required assemblies, and then have it point to the application which you want to interact with:
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
[Microsoft.VisualBasic.Interaction]::AppActivate('Notepad')
Start-Sleep -Milliseconds 1000
[System.Windows.Forms.SendKeys]::SendWait("H")
Start-Sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("E")
Start-Sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("L")
Start-Sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("L")
Start-Sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("O")
Take note of notepad in [Microsoft.VisualBasic.Interaction]::AppActivate('Notepad'), as it will be what ever program you're trying to interact with. In this case, you're just sending "HELLO" to the notepad that's already on started.

Open PowerShell script as ComObject

From a PS1 I am trying to open a new Powershell window running a certain script; I am trying to open it as a COM object so that I can use SendKeys with it.
I tried this,
$newshell = New-Object -ComObject WScript.Shell;
But this doesn't visibly do anything.
I tried making it visible with,
$newshell.Visible = $true, this throws an error that visible is not a property of newshell.
It works with other applications like Excel.
Thank you! I am new to Powershell.

Powershell SendKeys No Input

I'm working on a dual pc stream setup and would like to use my elgato stream deck on my gaming pc for some specific functions. The only problem is, no obs control. I was combing through reddit and came across the suggestion to use SendKeys in powershell scripts to perform hotkey functions. I am able to connect the two computers through PSSession and run my script, but nothing happens. I have the application focused. Even when I just open a text file and run the script, nothing comes up. I don't really think this should be that hard should it? Any help would be appreciated.
$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys(']')
Continuing from my comment...
Sendkeys is a thing but can be really finicky, focus, timing issues, etc. GUI automation is not really PowerShell's strong suit. Custom tools like AutoIT, Selenium, etc., are better options.
With Sendkeys, you often must set delays to ensure focus before calling keystrokes.
YOu can also avoid the use of ...
New-Object -ComObject wscript.shell
... and use this...
Add-Type -AssemblyName System.Windows.Forms
Here are a few examples you can try.
# pops the WinKey Start Menu
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('^{ESC}')
Or this way...
# Initialize a variable with the .Net namespace, then use it.
$SendKeys = [System.Windows.Forms.SendKeys]
$SendKeys::SendWait('^{ESC}')
# SendKeys to hold down keys
$SendKeys::SendWait('q(+%) + (+)q')
# Send commnad results to notepad - Note the sleep to wait for notepad to open, paste the content then select the file menu
Get-NetIPConfiguration | clip | notepad
Sleep -Seconds 1
$SendKeys::SendWait('^V')
Sleep -Seconds 2
$SendKeys::SendWait('%F')
Update as per our comments below:
SendKeys requires a GUI up and running. A GUI requires a logged-on, interactive user.

How to display excel data on powershell console using powershell script

I am trying to display excel data(text format) present across 10 columns in powershell console
But it is not working
$objExcel = New-Object -ComObject Excel.Application
$objexcel.visible=$true
$objexcel.DisplayAlerts=$false
$workbook=$objExcel.Workbooks.open("C:\Users\Desktop\commute.xlsx")
$worksheet=$workbook.sheets.item(1)
$usedrange=$worksheet.UsedRange
#{TIME=$worksheet.Range("A1:A10").text
DEPART=$worksheet.Range("B1:B10").text
REDS=$worksheet.Range("C1:C10").text
TRAINS=$worksheet.Range("D1:D10").text }
why don't you use the cmdlet Import-CSV? It will be much easier.
try the following:
$Worksheet = Import-Csv -Path 'C:\Users\Desktop\commute.xlsx'
It will create a Powershell object you can use and manipulate.
hope it helps you.

How to send number 1 using powershell keystroke.

Would you like to:
[1] Accept the certificate for this session
Please input your selection (The default selection is [1]): 1
I have to enter number one here, how it can be done through power shell script
Read-Host would be the way to do it from the console while using Write-Host/Write-Output to present the option information. You would of course need to check the contents of the variable to see if you got an expected result.
If you're looking for a simple yes/no answer and are OK with popups, use a .NET messagebox:
[System.Windows.Forms.MessageBox]::Show('message', 'titlemessage')
There are tons of overloads for this which include giving you the ability to control the buttons displayed and the icon (warning, error, informational, etc.)
EDIT: This SO post shows how to accomplish SendKeys functionality with both a COM object and a .NET object: How to perform keystroke inside powershell?
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('title of the application window')
Sleep 1
$wshell.SendKeys(1)
and
add-type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait(1)
or
add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms
[Microsoft.VisualBasic.Interaction]::AppActivate(“Calc”)
[System.Windows.Forms.SendKeys]::SendWait(“1{ADD}1=”)
from: https://blogs.technet.microsoft.com/heyscriptingguy/2011/01/10/provide-input-to-applications-with-powershell/