guides:com:wscript.shell

WScript.Shell

Original post by sbc on the AutoHotkey Archived Forums


COM Object: WScript.Shell

Purpose: Various Administration Tasks

System Requirements: General, WSH version 5.6 for the Exec() method

Documentation Link: WshShell Object

Other Links: WSH Primer

Code Example:

#Requires AutoHotkey v1.1.0+
 
;Retrieve console's outputs
;http://msdn.microsoft.com/en-us/library/cbxxzwb5%28v=VS.85%29.aspx
;http://technet.microsoft.com/en-us/library/ee156605.aspx
objShell := ComObjCreate("WScript.Shell")
objExec := objShell.Exec("ipconfig.exe")
While !objExec.Status	;wait until ipconfig.exe starts
    Sleep 100
strLine := objExec.StdOut.ReadAll()	;read the output at once
msgbox % strLine   
 
objExec := objShell.Exec("cmd /c ping -n 3 -w 1000 www.google.com")
While !objExec.StdOut.AtEndOfStream ;read the output line by line
	if InStr(objExec.StdOut.ReadLine(), "Reply") {
		Msgbox Reply received.
		Break
	}
 
;Retrieve Special Folder paths
;http://technet.microsoft.com/en-us/library/ee156616.aspx
Msgbox % "Desktop:`n`t" . objShell.SpecialFolders("Desktop") . "`n"
    . "Favorites:`n`t" . objShell.SpecialFolders("Favorites") . "`n"
    . "MyDocuments:`n`t" . objShell.SpecialFolders("MyDocuments") . "`n"
    . "SendTo:`n`t" . objShell.SpecialFolders("SendTo") . "`n"
    . "StartMenu:`n`t" . objShell.SpecialFolders("StartMenu") . "`n"
    . "Startup:`n`t" . objShell.SpecialFolders("Startup")
 
;Add logs to Application Event Log 	
;http://technet.microsoft.com/en-us/library/ee156617.aspx
objShell.LogEvent(0, "Test Writing: Success")
objShell.LogEvent(1, "Test Writing: Error")
objShell.LogEvent(2, "Test Writing: Warning")
objShell.LogEvent(4, "Test Writing: Information")
objShell.LogEvent(8, "Test Writing: Audit Sucess")
objShell.LogEvent(16, "Test Writing: Audit Failure")	
 
;Hello World Script with MessageBox and Notepad
objExec := objShell.Exec("notepad.exe")			;Run, notepad,,, PID
While !objShell.AppActivate(objExec.ProcessID)	;WinWaitActive, ahk_pid %PID%
	sleep 100
objShell.SendKeys("Hello World!{Enter}") ;Send, Hell World{Enter}
iBtn := objShell.Popup("Do you cancel?", 5, "Continue?", 33)	;Msgbox,33, Continue?, Do you cancel?, 5
if iBtn = 1         ;OK is pressed
    objShell.Popup("Done", 5, "You pressed OK", 64)
else if iBtn = 2    ;Cancel is pressed
    objShell.Popup("Canceled", 5, "You pressed Cancel", 64)
else	;Timeout or other reasons
    objShell.Popup("Forced Execution", 5, "None pressed", 64)
objExec.Terminate()								;Process, Close, %PID%

Run a command and retrieve its output: (originally from the AHK Documentation)

#Requires AutoHotkey v1.1.0+
 
MsgBox % RunWaitOne("dir " A_ScriptDir)
 
MsgBox % RunWaitMany("
(
echo Put your commands here,
echo each one will be run,
echo and you'll get the output.
)")
 
RunWaitOne(command) {
    ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99
    shell := ComObjCreate("WScript.Shell")
    ; Execute a single command via cmd.exe
    exec := shell.Exec(ComSpec " /C " command)
    ; Read and return the command's output
    return exec.StdOut.ReadAll()
}
 
RunWaitMany(commands) {
    shell := ComObjCreate("WScript.Shell")
    ; Open cmd.exe with echoing of commands disabled
    exec := shell.Exec(ComSpec " /Q /K echo off")
    ; Send the commands to execute, separated by newline
    exec.StdIn.WriteLine(commands "`nexit")  ; Always exit at the end!
    ; Read and return the output of all commands
    return exec.StdOut.ReadAll()
}

Execute the given code as a new AutoHotkey process: (originally from the AHK Documentation)

#Requires AutoHotkey v1.1.0+
 
ExecScript(Script, Wait:=true)
{
    shell := ComObjCreate("WScript.Shell")
    exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
    exec.StdIn.Write(script)
    exec.StdIn.Close()
    if Wait
        return exec.StdOut.ReadAll()
}
 
; Example:
InputBox expr,, Enter an expression to evaluate as a new script.,,,,,,,, Asc("*")
result := ExecScript("FileAppend % (" expr "), *")
MsgBox % "Result: " result