locked
Run powershell from hta RRS feed

  • Question

  • I'm trying to run the following script from a HTA

    Set WshShell = CreateObject("WScript.Shell")
    Connect="powershell -noexit -ExecutionPolicy Bypass -File IISinstall.ps1"
    WshShell.Run Connect, 4, false

    IISinstall.ps1 looks like this:
    import-module servermanager
    add-windowsfeature WEB-Server

    The commands from IISinstall work fine when typed on the command line, but when run it from the HTA I get the following:

    Import-Module : The specified module 'servermanager' was not loaded because no
    valid module file was found in any module directory.
    At D:\Tasks\IISinstall.ps1:5 char:14
    + import-module <<<<  servermanager
        + CategoryInfo          : ResourceUnavailable: (servermanager:String) [Imp
       ort-Module], FileNotFoundException
        + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm
       ands.ImportModuleCommand

    The term 'add-windowsfeature' is not recognized as the name of a cmdlet, functi
    on, script file, or operable program. Check the spelling of the name, or if a p
    ath was included, verify that the path is correct and try again.
    At D:\Tasks\IISinstall.ps1:11 char:19
    + add-windowsfeature <<<<  WEB-Server
        + CategoryInfo          : ObjectNotFound: (add-windowsfeature:String) [],
       CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    I tried a lot of things but can't seem to get it working.

    Does anyone have a solution for this?

    Thursday, April 15, 2010 3:01 PM

Answers

  • Thanks for pointing me in the right direction.

    Even when I used the full path it didn't work. I needed to use %windir%\Sysnative\WindowsPowerShell\v1.0\powershell.exe to fix this.

    Now it works like a charm!

    • Marked as answer by Mervyn Zhang Tuesday, April 20, 2010 9:34 AM
    Friday, April 16, 2010 7:35 AM
  • I think your HTA is invoking the 32-bit version of PowerShell, and it cannot load the 64-bit Server Manager module.

    Try hardcoding your HTA to run the 64-bit version of PowerShell by using the full path ($pshome).

    I tried your code in a VBScript, so even though it is 32-bit, it was still able to call the 64-bit PowerShell.

     

    • Proposed as answer by Marco Shaw Thursday, April 15, 2010 11:08 PM
    • Marked as answer by E76 Friday, April 16, 2010 7:35 AM
    Thursday, April 15, 2010 11:08 PM

All replies

  • I think your HTA is invoking the 32-bit version of PowerShell, and it cannot load the 64-bit Server Manager module.

    Try hardcoding your HTA to run the 64-bit version of PowerShell by using the full path ($pshome).

    I tried your code in a VBScript, so even though it is 32-bit, it was still able to call the 64-bit PowerShell.

     

    • Proposed as answer by Marco Shaw Thursday, April 15, 2010 11:08 PM
    • Marked as answer by E76 Friday, April 16, 2010 7:35 AM
    Thursday, April 15, 2010 11:08 PM
  • Thanks for pointing me in the right direction.

    Even when I used the full path it didn't work. I needed to use %windir%\Sysnative\WindowsPowerShell\v1.0\powershell.exe to fix this.

    Now it works like a charm!

    • Marked as answer by Mervyn Zhang Tuesday, April 20, 2010 9:34 AM
    Friday, April 16, 2010 7:35 AM