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?
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
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.
User contributions licensed under CC BY-SA 3.0