I can run any PowerShell command inside AHK file. For example, this command below:
Run, PowerShell.exe -noexit -Command "Start notepad; start calc.exe",, hide
I have Powershell command that I can run it from PowerShell fine and its working great which will add user as local admin on remote machine:
([ADSI]"WinNT://serverabc123/Administrators,Group").Add("WinNT://HMM.org.br/Paul1")
However, when I use the above code on AHK like it's not working!
Run, PowerShell.exe -noexit -Command "([ADSI]"WinNT://serverabc123/Administrators,Group").Add("WinNT://HMM.org.br/Paul1")",, hide
I keep getting this error:
At line:1 char:8<>
+ ([ADSI]WinNT://serverabc123/Administrators
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'WinNT://serverabc123/Administrators' in expression or statement.
At line:1 char:8
+ ([ADSI]WinNT://serverabc123/Administrators
+ ~
Missing closing ')' in expression.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Can anyone advice on what to do? I could write external file PS1 and call it from the AHK then delete it, but I want to have it running from my single AHK file.
Try the following:
Run, powershell.exe -noexit -command ([ADSI]\"WinNT://serverabc123/Administrators`,Group\").Add(\"WinNT://HMM.org.br/Paul1\")
That is:
Don't use outer quoting.
\-escape " characters (required by PowerShell)
`-escape , characters (required by AutoHotkey)
Related
I wish to run the following command from within Powershell 5 but make it execute with Powershell 6:
$PSVersionTable.PSVersion.Major
How can this be done?
The expected return value from the command should be '6'.
I am getting this:
pwsh -command "$PSVersionTable.PSVersion.Major"
System.Collections.Hashtable.PSVersion.Major : The term 'System.Collections.Hashtable.PSVersion.Major' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ System.Collections.Hashtable.PSVersion.Major
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (System.Collections.\u2026ble.PSVersion.Major:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The problem is the double-quotes. PowerShell 5 is using string interpolation on the string before passing it to pwsh. So, what you see as:
pwsh -command "$PSVersionTable.PSVersion.Major"
Is actually passed as:
pwsh -command "System.Collections.Hashtable.PSVersion.Major.PSVersion.Major"
The solution is to use single quotes (which 'switches off' interpolation):
pwsh -command '$PSVersionTable.PSVersion.Major'
I have the need to run PowerShell scripts using the command prompt (calling powershell.exe with the -c parameter). I have run these for years but have never tried it with Invoke-Command in order to do remoting and when the script I want to execute has a switch I have to pass.
This piece of code works great from PowerShell, a simple script that has the LogRun switch:
icm -cn MYCOMPUTER02 {C:\Temp\Write-to-File.ps1 -LogRun $Using:LogRun}
However, when I run it via the Command Prompt with powershell.exe fails:
powershell -c "icm -cn MYCOMPUTER02 {C:\Temp\Write-to-File.ps1 -LogRun $Using:LogRun}"
I have tried many variations of this and nothing works, I am sure I missing some syntax detail or some quotes somewhere.
Please let me know if you can help me figure this out.
This is the error I get:
icm : The value of the using variable '$using:LogRun' cannot be retrieved
because it has not been set in the local session.
At line:1 char:1
+ icm -cn MYCOMPUTER02{D:\PatV2DU\PatV2DUService20180411120032\Sc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Command], RuntimeException
+ FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.InvokeCommandCommand
I tried the solution given from: Specify the size of command prompt when executing a batch file
I ran:
powershell -command "&{set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize}"
But I get these errors, any ideas?
Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. At line:1 char:22 + &{set-executionpolicy <<<< remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize} + CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Import-Module : The specified module 'SetConsoleFont' was not loaded because no valid module file was found in any module directory . At line:1 char:50 + &{set-executionpolicy remotesigned; Import-Module <<<< SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize} + CategoryInfo : ResourceUnavailable: (SetConsoleFont:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
The term 'Get-ConsoleFontInfo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spe lling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:86 + &{set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo <<<< | Format-Table -AutoSize} + CategoryInfo : ObjectNotFound: (Get-ConsoleFontInfo:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
I have put the file SetConsoleFont.psm1 in
C:\Users\Adrian\Documents\WindowsPowerShell\Modules\SetConsoleFont
You say "You're not allowed to set the execution policy" well maybe I'm not, but it's my machine so why shouldn't I? I don't want to execute these commands as Administrator, just as a user, me (Adrian)
Another comment was to try set-executionpolicy bypass process
so I tried:
powershell -command "&{set-executionpolicy bypass process; set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize}"
But got even more red errors.
I have no idea what powershell is or how to use it, I just want to change the font from a batch file without hassle!
Try set-executionpolicy bypass process instead.
Also make sure you have put the module in a module path folder such as:
[yourprofile]\Documents\WindowsPowershell\Modules
I managed to get it working but only in a PowerShell console, and I had to run it as Administrator. However this is not practical for me for the following reasons:
I wish to change the font of new window seamlessly from a batch file, which will be run by users of the software. They may not have Administrator access and so cannot execute "set-executionpolicy remotesigned" which I needed to do to get it working.
Also this has to be done in a DOS batch file, so opening up a powershell window is not an option. It only works in a PowerShell window and not with the DOS "powershell -command" option.
So a partial answer.
If you want to change Execution Policy, it should be done in an elevated prompt.
And loading the module can be done by giving absolute path. Example is below.
Import-Module c:\users\testuser\desktop\SetConsoleFont.psm1 -Verbose
and we can bypass execution policy like below.
powershell.exe -executionpolicy bypass -command "${<your code>}"
Edit: The imported module will be available only in the scope of the script block.
here it is with in {}. So whatever cmdlets and functions in side the module should be executed in sided the scriptblock.
Regards,
Kvprasoon
I try to start powershell as an external tool.
I want it to be run in the console windows of eclipse.
But it starts with empty console, and when I type and press return, I get:
Missing expression after unary opearator ‘-’.
At line:1 char:2
+ – <<<< Command Set-Location C:\Users\XXX
+ CategoryInfo : ParserError: (-:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterOperator
You got misplaced quotes in the Arguments section.
-NoExit -"Command Set-Location ${project_loc}"
Try moving the quote after Command switch like so,
-NoExit -Command "Set-Location ${project_loc}"
I tried running one of the .bat files with CMD.
The first line it works
powershell -Command "Set-ExecutionPolicy Unrestricted"
powershell -Command "& {.\SetupLabxx.ps1}" -NoExit
pause
The second line gives me this exception
The term '.\SetupLabxx.ps1' is not recognized as the name of a cmdlet,
function , script file, or operable program. Check the spelling of the
name, or if a pat h was included, verify that the path is correct and
try again. At line:1 char:20 + & {.\SetupLabxx.ps1 <<<< } -NoExit +
CategoryInfo : ObjectNotFound: (.\SetupLabxx.ps1:String) [], Co
mmandNotFoundException + FullyQualifiedErrorId :
CommandNotFoundException
You can combine the commands (use full path to script):
powershell.exe -NoExit -ExecutionPolicy Unrestricted -File c:\SetupLabxx.ps1