PowerShell script n/w in Administrator level

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ahkLily16
Posts: 3
Joined: 18 Jun 2017, 22:28

PowerShell script n/w in Administrator level

18 Jun 2017, 23:06

Hi all
Can't for the life of me get my script to work in a PowerShell session that has been run as Administrator.
Yet it works like a dream in a PS session that's opened normally with non-Admin privileges.

Code: Select all

SendMode Input
#Warn


#IfWinActive Windows PowerShell
F1::
   Send {Escape}Get-Help{Space}
Return							; Press F1 to insert Get-Help

F2::
   Send {Escape}Get-Command{Space} 
Return							; Press F2 to insert Get-Command

F3::
   Send {Escape}Get-Alias{Space} 
Return							; Press F3 to insert Get-Alias

F4::
   Send {Escape}Get-ChildItem{Enter} 
Return							; Press F4 to Get-ChildItem

F5::
   Send {Escape}Clear-Host{Enter} 
Return							; Press F5 to Clear-Host

F6::
   Send {Escape}Get-History{Enter}
   Send Invoke-History{Space}	
Return							; Press F6 to Invoke-History
My system is Windows 10 x64 v1703, PS Version 5.1, AHK v1.1.25.01

I've assumed that it is some kind of Run as Administrator issue. I've tried:
-RightClick > Run as Adminitrator when launching my .ahk file
-RightClick > Compile Script, then (.exe file) RightClick > Properties > Compatibility > Run this program as Administrator
-Tried the same Compatibility > Run this program as Administrator setting the .exe files in C:\Program Files\AutoHotKey
-Inserted code found at https://autohotkey.com/docs/commands/Run.htm#RunAs which perhaps seems to do the tasks I've tried beforehand
-Tested operation in fresh Windows 10 VM, tried again in Windows 8.1 PowerShell v4.0 VM

In all instances, I'm logged in using a Local Account of the Administrator type.

What have I missed? :?:
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: PowerShell script n/w in Administrator level

19 Jun 2017, 12:41

EDIT: You're welcome, ahkLily16 :-)

Hi,

ahkLily16 wrote:What have I missed? :?:
When you right-click PowerShell from the start menu and choose to run it as Administrator, the window's title becomes Administrator: Windows PowerShell.

You can use #IfWinActive ahk_exe powershell.exe instead or look at SetTitleMatchMode to make title matching work on substrings or, if you want to, regex it up.

Here's my modification to your script that tries to enable your PS hotkeys even if you start the command prompt first and then start PowerShell in it:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode, Input  ; Recommended for new scripts due to its superior speed and reliability.
SetBatchLines, -1
;ListLines, Off
;#KeyHistory 0
;SetKeyDelay, -1, -1
#Warn

#If (WinActive("ahk_exe powershell.exe") || powershellIsPrimaryProcess(WinActive("ahk_exe cmd.exe")))
F1::
   Send {Escape}Get-Help{Space}
Return							; Press F1 to insert Get-Help

F2::
   Send {Escape}Get-Command{Space} 
Return							; Press F2 to insert Get-Command

F3::
   Send {Escape}Get-Alias{Space} 
Return							; Press F3 to insert Get-Alias

F4::
   Send {Escape}Get-ChildItem{Enter} 
Return							; Press F4 to Get-ChildItem

F5::
   Send {Escape}Clear-Host{Enter} 
Return							; Press F5 to Clear-Host

F6::
   Send {Escape}Get-History{Enter}
   Send Invoke-History{Space}	
Return							; Press F6 to Invoke-History
#If

powershellIsPrimaryProcess(cmdHwnd)
{
	static scriptPID := DllCall("GetCurrentProcessId", "UInt")
          ,processCount := 10
          ,processList := 0
          ,basename := ""
          ,AttachConsole := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "AttachConsole", "Ptr")
          ,GetConsoleProcessList := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "GetConsoleProcessList", "Ptr")
          ,FreeConsole := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "FreeConsole", "Ptr")
	ret := False

	if (!cmdHwnd)
		return False

	WinGet targetCmdPID, PID, ahk_id %cmdHwnd%
	if (targetCmdPID && DllCall(AttachConsole, "UInt", targetCmdPID)) {
		if (!VarSetCapacity(processList))
			VarSetCapacity(processList, processCount * 4, 0)
			,VarSetCapacity(basename, 522)
		Loop {
			actualCapacity := DllCall(GetConsoleProcessList, "Ptr", &processList, "UInt", processCount, "UInt")
			if (!actualCapacity) {
				break
			} else if (actualCapacity > processCount) {
				processCount := actualCapacity
				VarSetCapacity(processList, processCount * 4, 0)
				continue
			}
			Loop %actualCapacity% {
				dwPID := NumGet(processList, (A_Index - 1) * 4, "UInt")
				if (dwPID == scriptPID || dwPID == targetCmdPID)
					continue
				if ((fullPath := QueryFullProcessImageName(dwPID))) {
					SplitPath, fullPath, basename
					if (basename == "powershell.exe") ; use single = for case-insensitive string compare
						ret := True
				}
				break
			}
			break
		}
		DllCall(FreeConsole)
	}
	return ret
}

QueryFullProcessImageName(PID)
{
	static imageName, PROCESS_QUERY_LIMITED_INFORMATION := 0x1000
		  ,OpenProcess := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "OpenProcess", "Ptr")
		  ,QueryFullProcessImageNameW := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "QueryFullProcessImageNameW", "Ptr")
		  ,CloseHandle := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "CloseHandle", "Ptr")
	if (!PID)
		return ""

	size := 260
	if (!VarSetCapacity(imageName))
		VarSetCapacity(imageName, (size + 1) * 2)

	if (!(hProcess := DllCall(OpenProcess, "UInt", PROCESS_QUERY_LIMITED_INFORMATION, "Int", False, "UInt", PID, "Ptr")))
		return ""
	
	pathObtained := DllCall(QueryFullProcessImageNameW, "Ptr", hProcess, "UInt", 0, "WStr", imageName, "UInt*", size)
	,DllCall(CloseHandle, "Ptr", hProcess)
	return pathObtained ? imageName : ""
}
Last edited by qwerty12 on 20 Jun 2017, 04:39, edited 1 time in total.
ahkLily16
Posts: 3
Joined: 18 Jun 2017, 22:28

Re: PowerShell script n/w in Administrator level

19 Jun 2017, 16:33

Yep, all those options worked perfectly. 10/10 mate :clap:
Swear I tried that #IfWinActive ahk_exe powershell.exe at the start of my troubleshooting! Only breaks when someone else isn't looking, right? /s
Thanks for this, much appreciated
ahkLily16
Posts: 3
Joined: 18 Jun 2017, 22:28

Re: PowerShell script n/w in Administrator level

19 Jun 2017, 16:36

Explains why my script would only work for a split second after opening the session, as it's window title takes a split second to rename itself to Administrator: Win...etc. :idea:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Frogrammer, Google [Bot], jameswrightesq and 276 guests