'Powershell script not working outside of ISE
My friends and I like to mess with each other by shutting down each other's computers down during design class. I wrote a little script to automate the action by rearranging code I found online, but it doesn't run outside the ISE (It works as intended inside the ISE, when I try to start it outside the ISE, I get error messages for almost all the code). Keep in mind that I'm completely new to PowerShell & most other coding languages & do not know how I'm supposed to fix it.
#start cmd and execute shutdown /i command
Set-Variable -Name "ip" -Value "87"
start cmd.exe
sleep 0.55
[System.Windows.Forms.SendKeys]::SendWait("shutdown /i")
sleep 0.55
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
#open ip window
sleep 0.1
[Windows.Forms.Cursor]::Position = "$(800),$(385)"
sleep 0.55
Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W;
[W.U32]::mouse_event(6,0,0,0,0);
sleep 0.55
#enter ip
[System.Windows.Forms.SendKeys]::SendWait("ip 192.168.100.$ip")
sleep 0.1
[Windows.Forms.Cursor]::Position = "$(720),$(660)"
sleep 0.55
Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W;
[W.U32]::mouse_event(6,0,0,0,0);
sleep 0.55
#set mode to shutdown
[Windows.Forms.Cursor]::Position = "$(720),$(485)"
sleep 0.1
Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W;
[W.U32]::mouse_event(6,0,0,0,0);
sleep 0.55
[System.Windows.Forms.SendKeys]::SendWait("{UP}")
sleep 0.3
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
#disable warning
sleep 0.1
[Windows.Forms.Cursor]::Position = "$(485),$(515)"
sleep 0.55
Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W;
[W.U32]::mouse_event(6,0,0,0,0);
sleep 0.1
#set reason
[Windows.Forms.Cursor]::Position = "$(650),$(640)"
sleep 0.55
Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W;
[W.U32]::mouse_event(6,0,0,0,0);
sleep 0.55
[System.Windows.Forms.SendKeys]::SendWait("{DOWN}")
sleep 0.3
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
sleep 0.1
#execute
[Windows.Forms.Cursor]::Position = "$(650),$(770)"
sleep 0.55
Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W;
[W.U32]::mouse_event(6,0,0,0,0);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
