Powershell Command to store Azure credentials in Microsoft Graph - azure

I have a graph application and I am trying to install azure active directory for that and I am entering connecting commands every time is there any option where I can store commands so that I can use it in future.

is there any option where I can store commands so that I can use it in future.
Yes there is a command where you can store the credentials and use the credentials to connect to the services.
$AzureAdCred = Get-Credential
Connect-AzureAD -Credential $AzureAdCred
The first command will store the credentials in $azureadcred
The next command will be connecting to the services in advance.
You can go through this Microsoft Document if you want further clarification.

Related

How can i login to Azure with Powershell using 2 factor authentication?

I am logging into Azure from Powershell using the following commands, but since 2 factor authentication was enabled I get the error below:
az login -u testemail#testserver.com -p "<mypassword>"
az account set --subscription "<my_subscription>"
But I get the error:
AADSTS50076: Due to a configuration change made by your administrator, or because you moved to a
new location, you must use multi-factor authentication to access the subscription.
There is 2 factor authentication configured here and if i login with the browser it pops up the windows to enter the security code. How can i automate that within Powershell or is there any other option ?
Reason being I run automated scripts within a CI/CD pipeline and am trying to avoid any user interaction .

Azure Powershell - AzureAdUser V AzAdUser

I have the free developer E5 subscription and have setup a tenancy, created users etc. I have tried creating a second directory that I am planning to use to test the Azure AD Connect tool on a local server. I can switch between the tenants on the Azure Portal with the "Switch tenant" button however can't seem to figure it out with the Powershell cmdlets.
Connect-AzureAD
Set-AzContext -TenantId "My new tenant/directory"
Following these two commands is where the confusion starts. The first commands lists the users from the first directory and the second command shows the users from the directory I have switched too.
Get-AzureAdUser (Shows the first directory, not the one switched too)
Get-AzAdUser (Shows the users of the directory switched too)
Not sure if there is something I am missing here?
Thats because those are commands from 2 different modules:
First one are from AzureAD which is a module designed for tasks within AzureAD
Ths second one are from Az With is designed to handle most, if not all of Azure's resources. The AD functionality is mostly for the module to handle lookups of azure ad objects when checking rbac and assigning access, and not really created to manage AzureAD in any meaningful way (even tho you can do some tasks).
For your task you can use the az module easliy if you just want to look up the users, but if you need to actually administer azure ad i suggest you go for azuread.
To connect to a specified tenant with azuread use connect-azuread -tenantId 'tenant id'. I also think it support that you use domain name aswell

Use a Azure Virtual Machine System-Assigned Managed Identity to access Azure SQL from Powershell

I need to run some bulk operations overnight on an azure sql database.
I have a PowerShell script which uses invoke-sql commands using sql credentials for a service account. Here's an example:
$params = #{
'Database' = 'test-db'
'ServerInstance' = 'my-db.database.windows.net'
'Username' = 'service_account'
'Password' = 'my exposed password'
'OutputSqlErrors' = $true
'Query' = 'SELECT top 10 * FROM dbo.CaseHistory'
}
Invoke-Sqlcmd #params
I want to set this up as a windows scheduled task to run from my VM each night however I don't like having the SQL credentials there in the PowerShell script.
The VM in question has a system-assigned managed identity which I'm already using to access blob storage.
I've already set up a sql user for this identity in azure sql:
CREATE USER [myVmName] FROM EXTERNAL PROVIDER
And I've granted all the required permissions.
How do I use the managed identity from the PowerShell script?
P.S. I'm not precious about sticking with Invoke-Sqlcmd.
Congratulations Twisted have found the solution:
"I found this page with a section right at the bottom on using PowerShell with managed identities: Tutorial: Use a Windows VM system-assigned managed identity to access Azure SQL"
I help him post it as answer and this can be beneficial to other community members.

Purge local user form Azure

Couldn't find exactly what I was looking for in the forum...
I was testing AD Sync with a virtual domain controller. I have since deleted the virtual domain controller and need to remove the users from Azure AD. How is that done? "Delete Users" is not allowable when the user is from a local AD. Also, how do I turn off AD Sync in Azure?
"Delete Users" is not allowable when the user is from a local AD
For the on-premise synced objects, you could not manage or remove from the Azure AD.
If you want to remove the synced users and turn off the AD Sync, you could follow this way:
Install the Azure Active Directory Module for Windows PowerShell.
Connect to Azure AD by using Windows PowerShell.
Disable directory synchronization. To do this, type the cmdlet Set-MsolDirSyncEnabled –EnableDirSync $false, and then press Enter.
Check that directory synchronization was fully disabled by using the Windows PowerShell. To do this, run the cmdlet periodically:(Get-MSOLCompanyInformation).DirectorySynchronizationEnabled.
This cmdlet will return True or False. Continue to run this cmdlet periodically until it returns False, and then go to the next step.
Try to update an object by using Windows PowerShell or by using the cloud service portal. By this step, you could remove the synced users.
For the details, you could read here.
My question was in two parts.
1) Disable directory sync. This is answered by #SunnySun-MSFT above.
2) Remove local AD users from Azure AD. For this MSFT support got back to me with a working solution.
Open powershell as admin
Install-msolservice (provide global admin credentials)
Get-msoluser -all -synchronized (display all local users synched with Azure)
Get-msoluser -all -synchronized | remove-msoluser -force (delete synched users)

Unattended authentication through Azure Powershell for Resource Manager

I have a load of Azure Powershell deployment scripts that are triggered by the build server after the completion of a build which work perfectly - I use the Import-AzurePublishSettingsFile (which is securely stored locally) to log into Azure unattended.
I now need to extend these to include management of Sql Azure Servers, Elastic Pools and Databases.
These, however, are part of the Azure Resource Manager cmdlets which have a different method of authentication.
I've found these articles:
Authenticating a Service Principal with Azure Resource Manager
Using Azure PowerShell with Azure Resource Manager
Using Windows Azure Active Directory to Authenticate the Management Libraries)
but all appear to require you to enter your credentials at some stage at the process. The second link above explicitly states:
The AzureResourceManager module requires Add-AzureAccount. A Publish Settings file is not sufficient.
I've tried (just to see what happens) switching to the Resource Manager with Switch-AzureMode -Name AzureResourceManager and then just executing one of the cmdlets, such as Get-AzureResourceGroup -Name "blah" but I just get an error of
Get-AzureResourceGroup : AuthenticationFailed: Authentication failed. The 'Authorization' header is not present or provided in an invalid format.
I am quite happy to set up an AD Application, manually, once, through the portal and then provide my scripts with the relevant Application ID or whatever is required. I just need it to be unattended so that my build can deploy!
Can anyone tell me how I go about making an unattended script to use these Azure Resource Manager cmdlets through Powershell?
This post gives full details of how to do this. Basically you embed your powershell scripts with the username and password of your AD user.
Unattended authentication to Azure Management APIs with Azure Active Directory
But be aware that if, like me, you wish to combine calls to the AzureResourceManager and AzureServiceManagement modes (using Switch-AzureMode) then you must use Add-AzureAccount -Credential <credentials> and also Remove-AzureAccount -Name <username> otherwise some of the AzureServiceManagement mode cmdlets cease to work.

Resources