Trouble finding the GAC file needed to run an assembly in powershell - azure

I am using PowerShell for some work in Azure. I am trying to run this code, but I get an error saying:
'Unable to find type [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider].'
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
I tried everything to fix it I searched for the DLL file I need, but to no avail. I tried to add the type using this command:
add-type -assemblyname Microsoft.Azure.Common
But that also gives an error. I am also using the latest version of PowerShell:
Name
Value
PSVersion
5.1.18362.1171
PSEdition
Desktop
PSCompatibleVersions
{1.0, 2.0, 3.0, 4.0...}
BuildVersion
10.0.18362.1171
CLRVersion
4.0.30319.42000
WSManStackVersion
3.0
PSRemotingProtocolVersion
2.3
SerializationVersion
1.1.0.1
Please help!

The error was caused because it was using Azure CLI. If Azure powershell is used it causes no error. They are different.
I am not sure if you have solved this issue, but you should have some misunderstandings about them, if you run this line in the PowerShell locally, this issue has nothing to do with Azure CLI, Azure CLI is another type of command written by python.
To solve the issue, you just need to import the module Az.Accounts via Import-Module -Name Az.Accounts -Force, make sure you have installed Az powershell module first, then login with Connect-AzAccount, normally it will import the Az.Accounts module automatically meanwhile for you, no need to do it manually. So if you find it works fine now without import anything, actually it imports it for you automatically.
Sample:
Connect-AzAccount
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
Manual way:

Related

"Connect-MsGraph' is not recognized as the name of a cmdlet

I have a couple of PowerShell scripts that can run using Windows PowerShell with no issue.
As soon as I run them in VS Code on the same computer, I get "Connect-MsGraph' is not recognized as the name of a cmdlet,
I have installed the same Microsoft.Graph.Intune and other relevant AAD modules for both Windows PowerShell and VS Code.
Any idea why I can't run them in the VS Code?
Thanks
Connect-MsGraph' is not recognized as the name of a cmdlet,
The issue happens because of the VS code is not able to access the PSModulePath in the system or it happens if you are not using the latest Microsoft.Graph SDK.
Try to install the module in a default path
Install-Module -Name Microsoft.Graph -RequiredVersion 1.2.0
Refer here if in your system default path contains OneDrive.
After adding the required modules in a default path try to restart the VS Code and System.
References
Connect MS Graph in VS Code

[error]Could not find the modules: 'Az.Accounts' with Version: '3.1.0'

I have a release Pipeline in which I use Azure PowerShell to deploy artifacts into AppServices in Azure.
It was working fine around two weeks ago but now failing.
I stayed nights troubleshooting and searching around for solution, but no success :(
In the Preferred Azure Powershell Version I have this tip: "
In case of hosted agents, the supported Azure PowerShell Version is:
1.0.0, 1.6.0, 2.3.2, 2.6.0, 3.1.0 (Hosted VS2017 Queue). To pick the latest version available on the agent, select "Latest installed
version".
"
I installed Az version 3.1.0 with the below command from normal Powershell using Pipeline on the target agent:
Install-Module -Name Az -Scope CurrentUser -RequiredVersion 3.1.0 -AllowClobber -Repository PSGallery -Force -SkipPublisherCheck
I can see Az module 3.1.0 installed successfully when I run Get-InstalledModule.
I tried both Tasks version 4 and 5 that supports Az and tried different versions from supported versions and all are failing with the error "Az.Accounts module not found".
I also tried installing latest version of Az but also failed with the same error.
We are using Hosted agents.
I dont know what is wrong and why VS2017 is showing lower supported versions?
Please help!
I can provide more details if needed.

Get-AzRecoveryServicesVault throws "Could not load file or assembly Microsoft.Azure.PowerShell.Authentication" in the devops release pipeline

The Az.RecoveryServices powershell command Get-AzRecoveryServicesVault throws "Could not load file or assembly Microsoft.Azure.PowerShell.Authentication or one of its dependencies. The system cannot find the file specified" in the devops release pipeline. The command is executed in the Azure powershell task version 4.* in the pipeline. The difference I see with the last successful run is the version of Az.Accounts.
Import-Module -Name C:\Modules\az_3.8.0\Az.Accounts\1.8.1\Az.Accounts.psd1 -Global
During the successful run the Azure powershell task imported version 1.8.0.
Import-Module -Name C:\Modules\az_3.8.0\Az.Accounts\1.8.0\Az.Accounts.psd1 -Global
Well, I can reproduce your issue on my side, looks it is a bug when using the Az.Accounts 1.8.1 along with the Az.RecoveryServices 2.8.0.
Per my test, to solve the issue, just use the Az.RecoveryServices 2.9.1, in the azure powershell task, there is no Az.RecoveryServices 2.9.1 installed by default currently, so we need to install and import it manually.
Try the commands below, it works fine on my side.
Install-Module -Name Az.RecoveryServices -RequiredVersion 2.9.1 -Force
Import-Module -Name Az.RecoveryServices -RequiredVersion 2.9.1
Get-AzRecoveryServicesVault -ResourceGroupName <group-name>
Make sure to use Az.Accounts 1.9.0, in my case I had to remove higher ver and re-run the commands what Joy Wang mentioned above...

Cloudn't register-PSRepository in my Windows10 for installing Azure PowerShell

I want to install Azure PowerShell in my windows 10, but I met an error message, I tried to find the reason for the error and found there was no package when I ran Get-PSRepository.
Background:
Install azure with code as below:
Install-Module -Name Az -AllowClobber -Scope AllUsers
Install-Module -Name Az -Repository PSGallery -Force
Error message:
Can't find the package at C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1748
Register-PSRepository -Default
Get-PSRepository shows can't find the package.
net winhttp show proxy shows without proxy server
what should I do to install the azure PowerShell?
Based on your error message, it seems that you have connection issue to powershell gallery
So, my first suggestion is to check your network environment work. Also remember to check your system proxy settings.
If your network has no problem, you may download package manually and then install modules from the downloaded nuget package
Another choice is to use Azure Cloud Shell
Hope the above could be helpful to you!

Add-AzureKeyVaultManagedStorageAccount cmdlet not found in AzureRM.KeyVault v. 3.1.0

According to Powershell Gallery the 3.1.0 version of AzureRM.KeyVault contains cmdlets supporting Azure Storage accounts whose keys are managed by KeyVault. I installed and Get-Module shows the 3.1.0 version, but I am not seeing (in ExportedCommands) the various commands to support storage.
Here are the steps I would do.
Shutdown all powershell processes or reboot
Go to c:\program files\windowspowershell\modules and delete the folder with the AzureRM.keyvault in name
Launch powershell and install the module Install-Module -Name AzureRM.KeyVault

Resources