0

I'm attempting to use PowerShell to install a Windows Feature (don't ask) as part of a project. In the PowerShell console I can run the command:

Get-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerRole"

and it completes successfully. When I then try to run this from the PM> prompt in Visual Studio 2015 (running as administrator) it then gives me the following error:

get-windowsoptionalfeature : An attempt was made to load a program with an incorrect format.

At line:1 char:1
+ get-windowsoptionalfeature -online -featurename "IIS-WebServerRole"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-WindowsOptionalFeature], COMException
    + FullyQualifiedErrorId : Microsoft.Dism.Commands.GetWindowsOptionalFeatureCommand

Here is my current $PSVersionTable from wihtin Package Manager.

Name                           Value
----                           -----
PSVersion                      5.1.14393.206
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.206 
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0   
PSRemotingProtocolVersion      2.3 
SerializationVersion           1.1.0.1

I can successfully run get-help get-windowsoptionalfeature and it will show syntax for the command. I also ran the command in a try catch block to get the exception, here is what it returned:

System.Runtime.InteropServices.COMException (0x8007000B): An attempt was made to load a program with an incorrect format.

   at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)

What, if anything, can I do to fix not being able to use Get-WindowsOptionalFeature from the Package Manager console?

Iron Ninja
  • 434
  • 1
  • 3
  • 13

2 Answers2

1

So, Here is the thing.. The package manager console is basically running as a 32 bit process and your OS appears to be 64. A similar error is here

Try executing the command from a 32 bit powershell (the one probably under %windir%\System32\WindowsPowerShell\v1.0), you will get the same error you got in visual studio. As user @Wendy mentioned the package manager is intented for working with your nuget packages and not as a powershell executor. I am curious to, why would you like to do it.

If, executing powershell commands from visual studio is your need, you can add powershell script to your solution and execute with some additional steps as mentioned here or just simply invoke it as a process as mentioned here In the other hand, If you would like to do it as part of a nuget package, try writing your script logic in your install script as mentioned here

Community
  • 1
  • 1
Prageeth Saravanan
  • 1,053
  • 1
  • 8
  • 24
  • I am indeed packaging the PowerShell script in the Nuget package as described in your second link, and then installing the package, which runs the script, and subsequently breaks due to the errors listed above. I'll peek at the other link and see where that leads. – Iron Ninja Dec 09 '16 at 16:08
  • 1
    Ended up using something I found here: http://www.madwithpowershell.com/2015/06/64-bit-vs-32-bit-powershell.html and invoking the 64 bit powershell from the 'sysnative' directory passing in the script. Thanks for pointing me in the direction of the 64 vs 32 bit issue! – Iron Ninja Dec 12 '16 at 14:16
-1

According to the introduce for Package Manager Console: https://nuget.codeplex.com/wikipage?title=Package%20Manager%20Console%20Command%20Reference%20(v1.3), the Package Manager Console lets you run PowerShell scripts from within Visual Studio and is the host for the PowerShell-based NuGet commands.

The Package Manager Console just provide the help documentation and descriptions for PowerShell command, so you could not run PowerShell command in Package Manager Console.

Weiwei
  • 3,674
  • 1
  • 10
  • 13
  • I think that is an old reference, https://docs.nuget.org/ndocs/tools/powershell-reference "The Package Manager Console provides a PowerShell interface within Visual Studio to interact with NuGet." – Iron Ninja Dec 09 '16 at 16:20
  • According to the define of Package Manager Console: https://docs.nuget.org/ndocs/tools/package-manager-console “The Package Manager Console in Visual Studio lets you use NuGet PowerShell commands to find, install, uninstall, and update NuGet packages”. The Package Manager Console only support NuGet PowerShell commands not full PowerShell scripts in the NuGet official documentation. – Weiwei Dec 12 '16 at 09:23