Powershell 6.2.3 unable to import AzureAd module - import

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

Related

Ubuntu 20 cannot Connect to MS Online Service via Powershell

I am using Ubuntu 20 and I have installed powershell to manage Office 365 accounts.
The powershell version is as follows:
Name Value
PSVersion 7.3.1
PSEdition Core
GitCommitId 7.3.1
OS Linux 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
When I run the command line with the correct credentials:
Connect-MsolService -Credential $Credentials
I get this error message:
Connect-MsolService: Could not load file or assembly 'System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
Connect-MsolService: Could not load file or assembly 'System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
Does any one know why?
Any help would be greatly appreciated...

System.Net.WebClient error can't download picture from the intranet

I have a problem with powershell.
I am trying to download a gif from the intranet of our company with a BAT data, but it always say the following error:
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://domain.name/file.name','C:\file.name')"
can anyone help me?
edit: Here is the version of powershell:
sure here is the table:
Name Value
---- -----
PSVersion 5.0.10586.117
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.117
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
The windows version is Windows 2008 R2
Best regards,
Xplosio

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.

Nuget Package Manager Fails When Calling Get-WindowsOptionalFeature

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.

PowerShell $PSVersionTable

I have PowerShell 3.0 installed, and I wish to emulate v1.0
When I launch PowerShell 3.0 with -Version 1 $PSVersionTable reports:
Name Value
---- -----
CLRVersion 2.0.50727.5456
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
If I use the -Version 2 parameter I get the same result
If I use the -Version 3 parameter I get version 3.
Question, why can't I emulate version 1?
Looking at this answer, I don't think you can run 1 & 2 side-by-side without some surgery on the GAC (so it would follow that 1, 2 & 3 don't all co-exist either - only 2 & 3, because 2 & 3 were designed to run side by side). Version 2 was meant to completely replace version 1, AFAICT.
You can not run V1 using the side by side stuff. That was never supported. You CAN run V2 from V3, but not V1.