6

I want to be able to run a following PowerShell command through AutoHotKey script:

new-item -path c:\ -name logfiles -itemtype directory

I am unable to find a way to achieve this task. Please help.

0

5 Answers 5

11

If you want to run a PowerShell script from ahk and it contains several lines and you don't want to use an external file, this is an example how do it:

AutoHotkey code:

psScript =
(
    param($param1, $param2)

    new-item -path $param1 -name logfiles -itemtype directory
    new-item -path $param2 -name logfiles -itemtype directory
    remove-item -path 'c:\temp'
    # etc, write any code, use this quotes for strings: '
    # if you need ", then write: \":
    $import = '[DllImport(\"ntdll.dll\")] public static extern int RtlAdjustPrivilege(ulong a, bool b, bool c, ref bool d);'
)

param1 = C:\temp
param2 = D:\temp

RunWait PowerShell.exe -Command &{%psScript%} '%param1%' '%param2%',, hide

; use this call if you want to see powershell output
Run PowerShell.exe -NoExit -Command &{%psScript%} '%param1%' '%param2%'   
Sign up to request clarification or add additional context in comments.

1 Comment

Use get-help about_PowerShell_exe | more from PowerShell prompt to get an explanation for why &{...} call operator is used.
2

Try:

Run, PowerShell "new-item -path c:\ -name logfiles -itemtype"

Seemed to work for me.

Edit based on newly provided information:

Command found @ http://exchangeserverpro.com/install-exchange-2013-pre-requisites-windows-server-2012/

Try:

Run, PowerShell "Install-WindowsFeature AS-HTTP-Activation
                , Desktop-Experience
                , NET-Framework-45-Features
                , RPC-over-HTTP-proxy
                , RSAT-Clustering
                , Web-Mgmt-Console
                , WAS-Process-Model
                , Web-Asp-Net45
                , Web-Basic-Auth
                , Web-Client-Auth
                , Web-Digest-Auth
                , Web-Dir-Browsing
                , Web-Dyn-Compression
                , Web-Http-Errors
                , Web-Http-Logging
                , Web-Http-Redirect
                , Web-Http-Tracing
                , Web-ISAPI-Ext
                , Web-ISAPI-Filter
                , Web-Lgcy-Mgmt-Console
                , Web-Metabase
                , Web-Mgmt-Console
                , Web-Mgmt-Service
                , Web-Net-Ext45
                , Web-Request-Monitor
                , Web-Server
                , Web-Stat-Compression
                , Web-Static-Content
                , Web-Windows-Auth
                , Web-WMI
                , Windows-Identity-Foundation"

3 Comments

The one you provided works for me too. But when I use 'Add-Windowsfeature' command instead of that in the example, I get an error saying VARIABLE NAME TOO LONG. Any suggestions please.
I edited the code to include Add-Windowsfeature code into it, but you haven't reported back if it is working or not?
I am getting the same error after running this. Any other way you could think of? Thank you so much.
0

PowerShell isn't necessary to solve this particular problem.

AutoHotKey has built-in functionality for creating directories: FileCreateDir

For example:

FileCreateDir, C:\logfiles

1 Comment

@Chad .. The command I provided was just an example. Actually I wanted to run the 'Add-Windowsfeature' command from the following article: exchangeserverpro.com/… and I am unable to run it because of an error that says VARIABLE NAME TOO LONG. Please suggest.
0

For PowerShell 7

Run pwsh.exe -NoExit -Command &{new-item -path c:\ -name logfiles -itemtype directory}

For Windows PowerShell

Run PowerShell.exe -NoExit -Command &{new-item -path c:\ -name logfiles -itemtype directory}

-NoExit to keep the terminal open other you may omit that part.

I personally used this method to ssh into a linux machine since none of the above worked for me (maybe I'm stupid) but here's my script.

#+t::
Run pwsh.exe -NoExit -Command &{ssh -p <Enter Port Number> <Username>@<IP Address>}
return

Win + Shift + T to launch

Comments

0

Thank you all for helping me finally figuring out my AutoHotkey publishing issues.

I'm using AHK v.2 to control a MQTTX CLI client and this is what I had to do to make it work:

  • Put the MQTTX client in the same folder as the .ahk script (for easier times 😊)

  • Rename it mqttx.exe if not already named that.

  • On Windows 11 and using PowerShell initialize the client with .\mqttx init (get the correct input from the broker).

  • Now this is the PowerShell commands I got working in AHK:

Shift & m::
{
    ; # PowerShell #
    Run("PowerShell" " .\mqttx" ' pub -t "my_topic" -m "Hello PowerShell"')
    Run("pwsh.exe" ' -NoExit -Command .\mqttx pub -t "my_topic" -m "Hi PS"')
}

For the AHK nerds out there it turns out we don't actually need PowerShell for this:

; Optional: Make a constant variable for the MQTTX client
MQTT_EXE := A_WorkingDir . "\mqttx"

Shift & m::
{
    ; # AutoHotkey #
    Run(MQTT_EXE ' pub -t "my_topic" -m "Hello from AHK" ' ) 
    Run(A_WorkingDir . "\mqttx" ' pub -t "my_topic" -m "AHK is back!"')
}

Hopes this helps somebody!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.