Nuget Package Manager Fails When Calling Get-WindowsOptionalFeature - powershell

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.

Related

Powershell Publish-Module to Powershell Gallery fails after creating Nuget Package

I am trying to publish a new version of a Powershell module to the Powershell Gallery by using
Publish-Module -Name "Nuttercode-PRTG" -NuGetApiKey $apiKey -Verbose -RequiredVersion 1.4
The module .psm1 and .psd1 files can be found here.
After dotnet.exe created the package in {myUserDir}\AppData\Local\Temp\{random numbers}\Nuttercode-PRTG it reports:
VERBOSE: finished running C:\Program Files\dotnet\dotnet.exe with exit code 0
VERBOSE: Created Nuget Package
VERBOSE: Successfully created nuget package at
Write-Error: Failed to publish module 'Nuttercode-PRTG': 'Cannot bind argument to parameter 'NupkgPath' because it is an empty string.'.
Afterwards the temp-folder gets deleted. To me this sounds like an internal problem of Publish-Module or dotnet.exe, but I have no idea what is going on or if I am doing something very wrong myself. I also created a new api key just in case but it did not affect the result.
I am using Windows 10 20H2 (Build 19042.1469), Powershell 7.2.2, PowerShellGet 2.2.5, and dotnet.exe 6.0.201. I am able to replicate the problem on another computer using the same command and arguments using Windows 11 21H2 (Build 22000.556), Powershell 7.2.1, PowershellGet 2.2.5, and dotnet.exe 5.0.102.

Powershell 6.2.3 unable to import AzureAd module

I am getting the below error when importing the "AzureAD" powershell module. I have installed the module, unable to import it. I have checked to see if there are any prerequisites required, nothing mentioned in GitHub.
Earlier, I was able to install and import "AZ" module.
UPDATE : Resolved one dependency by copying the system.windows.forms.dll, but more runtime errors...
Name Value
---- -----
PSVersion 6.2.3
PSEdition Core
GitCommitId 6.2.3
OS Microsoft Windows 10.0.17134
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
VERBOSE: Loading module from path 'C:\Program
Files\PowerShell\Modules\AzureAd\2.0.2.76\AzureAd.psd1'. VERBOSE:
Loading 'FormatsToProcess' from path 'C:\Program
Files\PowerShell\Modules\AzureAd\2.0.2.76\AzureAD.Format.ps1xml'.
VERBOSE: Populating RepositorySourceLocation property for module
AzureAd. VERBOSE: Loading module from path 'C:\Program
Files\PowerShell\Modules\AzureAd\2.0.2.76\Microsoft.Open.Azure.AD.CommonLibrary.dll'.
Import-Module : Could not load file or assembly 'System.Windows.Forms,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
The system cannot find the file specified. At line:1 char:1
+ Import-Module -Name AzureAd -Scope Global -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand
Which .NET version are you using? You may need to update it as this was an issue with older versions. If that doesn't work you can remove the dependency on System.Runtime.InteropServices.RuntimeInformation to get rid of the error in your screenshot.
It seems that the AzureAD Module is not compatible with PowerShell Core due to the WinForms dependency. I have tried both 7.0.0 and 6.2.4 which did not work
However you can install it using the Desktop version of PowerShell. This version works for me.
PS C:\Windows\system32> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.18362.752
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.752
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Run elevated
Install-Module AzureAD
And then move on...
Import-Module -Name AzureAD
$creds = Get-Credential -Message "Credentials to connect to Azure AD"
Connect-AzureAD -Credential $creds
Get-AzureADCurrentSessionInfo

PowerShell Script With Classes won't execute on Fresh Windows 10 Installation

I am attempting to execute a PowerShell script that works fine on a windows 10 PC that has visual studio and all its dependencies installed, but it will not execute on a VM with a fresh installation of windows 10 pro. I've chiseled the script down and determined that the thing it does not like is the definition of a class in the script.
Class Computer {
[String]$Name
[String]$Description
[String]$Type
[String]$Owner
}
The error I get when running the script with nothing but this class definition is:
An error occurred while creating the pipeline.
+CategoryInfo: NotSpecified: (:) [], ParentContainsErrorRecordException
+FullyQualifiedErrorId: RuntimeException
Running $PSVersionTable on the fresh VM:
Name Value
---- -----
PSVersion 5.0.10240.16384
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 10.0.10240.16384
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
Doesn't PowerShell 5.0 have support for classes? Am I missing a .NET framework dependency that isn't installed or enabled by default with a fresh Windows 10 installation?
To get the WMF 5.0 functionaly, please update to the November Update (10.0.10586.0) per The WMF 5.0 release blog.
Note: If you are running Windows 10, you can get the same set of functionality available in WMF 5.0 RTM by updating to the November update of Windows 10 (Version 1511). If you have not already updated your Windows 10 system, select the Start button, then select Settings > Update & security > Windows Update > Check for updates.

How do you get prerelease packages using powershell's Find-Package?

I am using Find-Package from Powershell 5.0. I need to query for pre-release packages. Is this possible?
I am aware that I can fall back to using nuget.exe list -prerelease if necessary.
It appears that the -AllVersions parameter will include them.
According to the nuget powershell documentation there is the -IncludePreRelease option:
Find-Package [Id] [-Source ] [-First ] [-Skip ] [-AllVersions] [-IncludePrerelease] [-ExactMatch]
Test:
PM> Find-Package Microsoft.AspNet.Identity.EntityFramework -Exact
Microsoft.AspNet.Identity.Entity... {2.2.1}
PM> Find-Package Microsoft.AspNet.Identity.EntityFramework -Exact -IncludePreRelease
Microsoft.AspNet.Identity.Entity... {3.0.0-rc1-final}
In Powershell you have to use the command "-AllowPrereleaseVersions" as documented here:
https://learn.microsoft.com/de-de/powershell/module/packagemanagement/find-package?view=powershell-7.1
Here is the hint on the differences betweeen PackageManager in Visual Studio and in Powershell:
https://learn.microsoft.com/de-de/nuget/reference/ps-reference/ps-ref-find-package
Version 3.0+; this topic describes the command within the Package Manager Console in Visual Studio on Windows. For the generic PowerShell Find-Package command, see the PowerShell PackageManagement reference.

Powershell with Sharepoint module - contradictory Error-Messages

I am trying to run the command "Get-SPWeb" from the SharePoint-Snapin for Powershell.
If I start PS normal (.net Ver.4), this is the error:
"Get-SPWeb: Microsoft SharePoint is not supported with Version 4.0.3.... of the Microsoft .Net Runtime."
Alright, so simply put -Version 2 behind the PS-Shortcut so it starts with Version 2? See what happens:
Error: Import-Module: [...] The Module 'SharePoint' requires a minimum version of '4.0' to execute
If I wouldn´t be mad as hell, I would just laugh about it.
Do you have any idea how to fix this issue?
This is the code by the way (important parts):
Add-PSSnapin Microsoft.SharePoint.PowerShell
$web = Get-SPWeb "http://myaddress.com/myreport"
Edit: Version-Info´s:
The Version-check provides the following:
PSVersion: 2.0
CLRVersion: 2.0. ....
Build: 6.1. ...
And with the normal PS:
PSVersion: 3.0
CLRVersion: 4.0
Build: 6.2
Also, I always start with the x64-version as administrator
Edit2: This is how the errors look: