Hello there, You should install the AzureAD module first. You can follow the below steps: Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 -Force Import-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 Install-Module AzureAD -Force -Force suppresses the user input prompts and allows the script to run in the background. Hope this resolves your Query !! --If the reply is helpful, please Upvote and Accept it as an answer--
Connect to AAD using automation account Runbook
Hector Huerta Tricolor
106
Reputation points
Im trying to connect to AAD and get AAzure application details using runnbook and manage identity on automation account but , im receiving the below error:
Connect-AzureAD: Line | 23 | Connect-AzureAD -AccountId $azureContext.account.id -TenantId $azureC … | ~~~~~~~~~~~~~~~ | The term 'Connect-AzureAD' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Get-AzureADApplication: Line | 26 | $Applications = Get-AzureADApplication -all $true | ~~~~~~~~~~~~~~~~~~~~~~ | The term 'Get-AzureADApplication' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Powershell Runbook:
#$connectionName="AzureRunAsConnection"
#$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
# Now you can login to Azure PowerShell with your Service Principal and Certificate
#Connect-AzureAD -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationID -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
#Connect-AzAccount -Identity
#Connect-AzureAD
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process
# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context
# Set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
#$azureContext = Connect-AzAccount -Identity
#$azureContext = Set-AzContext -SubscriptionName $azureContext.context.Subscription -DefaultProfile $azureContext.context
$graphToken = Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com/"
$aadToken = Get-AzAccessToken -ResourceUrl "https://graph.windows.net"
Connect-AzureAD -AccountId $AzureContext.account.id -TenantId $AzureContext.tenant.id -AadAccessToken $aadToken.token -MsAccessToken $graphToken.token
$Applications = Get-AzureADApplication -all $true
$Logs = @()
Write-host "I would like to see the Applications with the Secrets and Certificates that expire in the next X amount of Days? <
4 answers
Sort by: Most helpful
-
-
Daniel Pavez Sandoval 0 Reputation points
2023-05-23T21:46:44.1633333+00:00 I have a similar problem. I created an Automation Account with a runbook with the following code:
try { Connect-AzAccount -Identity | Out-Null } catch { Write-Error -Message $_.Exception throw $_.Exception } $Context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext $GraphToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($Context.Account,$Context.Environment,$Context.Tenant.Id.ToString(),$null,[Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never,$null,"https://graph.microsoft.com").AccessToken $AADToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($Context.Account,$Context.Environment,$Context.Tenant.Id.ToString(),$null,[Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never,$null,"https://graph.windows.net").AccessToken try { Connect-AzureAD -AadAccessToken $AADToken -AccountId $Context.Account.Id -TenantId $Context.Tenant.Id | Out-Null } catch{ Write-Error -Message $_.Exception throw $_.Exception }
But I get the following error when I run that on my Automation Account:
Failed The term 'Connect-AzureAD' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Write-Error: System.Management.Automation.CommandNotFoundException: The term 'Connect-AzureAD' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception) at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
My Automation Account has the
AzureAD
module for the runtime versions5.1
and7.2
-
Mwa1234 1 Reputation point
2024-06-03T11:39:45.1133333+00:00 I Have the same issue.
Directory: C:\usr\src\PSModules ModuleType Version PreRelease Name PSEdition ExportedCommands ---------- ------- ---------- ---- --------- ---------------- Binary 1.0 Automation.Sandbox.AssetManagement… Desk {Get-AutomationVariable, Set-AutomationV… Directory: C:\AzAt\Modules\11.2.0 ModuleType Version PreRelease Name PSEdition ExportedCommands ---------- ------- ---------- ---- --------- ---------------- Script 11.2.0 Az Core,Desk Script 3.0.0 Az.Accounts Core,Desk {Disable-AzDataCollection, Disable-AzCon… Script 2.0.0 Az.Advisor Core,Desk {Disable-AzAdvisorRecommendation, Enable… Script 6.0.0 Az.Aks Core,Desk {Get-AzAksCluster, New-AzAksCluster, Rem… Script 1.1.4 Az.AnalysisServices Core,Desk {Resume-AzAnalysisServicesServer, Suspen… Script 4.0.2 Az.ApiManagement Core,Desk {Add-AzApiManagementApiToGateway, Add-Az… Script 1.0.0 Az.App Core,Desk {Disable-AzContainerAppRevision, Enable-… Script 1.3.0 Az.AppConfiguration Core,Desk {Get-AzAppConfigurationStore, Get-AzAppC… Script 2.2.3 Az.ApplicationInsights Core,Desk {Get-AzApplicationInsights, Get-AzApplic… Script 1.0.0 Az.ArcResourceBridge Core,Desk {Get-AzArcResourceBridge, Get-AzArcResou… Script 2.0.0 Az.Attestation Core,Desk {Get-AzAttestationPolicy, Set-AzAttestat… Script 1.0.0 Az.Automanage Core,Desk {Get-AzAutomanageBestPractice, Get-AzAut… Script 1.10.0 Az.Automation Core,Desk {Export-AzAutomationDscConfiguration, Ex… Script 3.5.0 Az.Batch Core,Desk {Remove-AzBatchAccount, Get-AzBatchAccou… Script 2.0.3 Az.Billing Core,Desk {Get-AzBillingInvoice, Get-AzBillingPeri… Script 3.1.1 Az.Cdn Core,Desk {Clear-AzCdnEndpointContent, Clear-AzFro… Script 2.0.0 Az.CloudService Core,Desk {Get-AzCloudService, Get-AzCloudServiceI… Script 1.14.1 Az.CognitiveServices Core,Desk {Get-AzCognitiveServicesAccount, Get-AzC… Script 7.1.1 Az.Compute Core,Desk {Add-AzImageDataDisk, Add-AzVhd, Add-AzV… Script 1.0.0 Az.ConfidentialLedger Core,Desk {Get-AzConfidentialLedger, New-AzConfide… Script 4.0.0 Az.ContainerInstance Core,Desk {Add-AzContainerInstanceOutput, Get-AzCo… Script 4.1.3 Az.ContainerRegistry Core,Desk {Connect-AzContainerRegistry, Get-AzCont… Script 1.14.0 Az.CosmosDB Core,Desk {Get-AzCosmosDBAccount, Get-AzCosmosDBAc… Script 1.1.0 Az.DataBoxEdge Core,Desk {Get-AzDataBoxEdgeJob, Get-AzDataBoxEdge… Script 1.7.1 Az.Databricks Core,Desk {Get-AzDatabricksAccessConnector, Get-Az… Script 1.18.0 Az.DataFactory Core,Desk {Set-AzDataFactoryV2, Update-AzDataFacto… Script 1.0.3 Az.DataLakeAnalytics Core,Desk {Get-AzDataLakeAnalyticsDataSource, New-… Script 1.3.0 Az.DataLakeStore Core,Desk {Get-AzDataLakeStoreTrustedIdProvider, R… Script 2.2.0 Az.DataProtection Core,Desk {Backup-AzDataProtectionBackupInstanceAd… Script 1.0.1 Az.DataShare Core,Desk {New-AzDataShareAccount, Get-AzDataShare… Script 1.1.0 Az.DeploymentManager Core,Desk {Get-AzDeploymentManagerArtifactSource, … Script 4.3.0 Az.DesktopVirtualization Core,Desk {Disconnect-AzWvdUserSession, Expand-AzW… Script 1.1.0 Az.DevCenter Core,Desk {Deploy-AzDevCenterUserEnvironment, Get-… Script 1.0.2 Az.DevTestLabs Core,Desk {Get-AzDtlAllowedVMSizesPolicy, Get-AzDt… Script 1.2.0 Az.Dns Core,Desk {Get-AzDnsRecordSet, New-AzDnsRecordConf… Script 1.6.0 Az.EventGrid Core,Desk {New-AzEventGridTopic, Get-AzEventGridTo… Script 4.2.0 Az.EventHub Core,Desk {New-AzEventHubAuthorizationRuleSASToken… Script 1.10.0 Az.FrontDoor Core,Desk {New-AzFrontDoor, Get-AzFrontDoor, Set-A… Script 4.0.7 Az.Functions Core,Desk {Get-AzFunctionApp, Get-AzFunctionAppAva… Script 6.1.0 Az.HDInsight Core,Desk {Add-AzHDInsightClusterIdentity, Add-AzH… Script 2.0.0 Az.HealthcareApis Core,Desk {Get-AzHealthcareApisService, Get-AzHeal… Script 2.7.5 Az.IotHub Core,Desk {Add-AzIotHubKey, Get-AzIotHubEventHubCo… Script 5.1.0 Az.KeyVault Core,Desk {Add-AzKeyVaultCertificate, Add-AzKeyVau… Script 2.3.0 Az.Kusto Core,Desk {Add-AzKustoClusterLanguageExtension, Ad… Script 1.0.0 Az.LoadTesting Core,Desk {Get-AzLoad, New-AzLoad, Remove-AzLoad, … Script 1.5.0 Az.LogicApp Core,Desk {Get-AzIntegrationAccountAgreement, Get-… Script 1.1.3 Az.MachineLearning Core,Desk {Move-AzMlCommitmentAssociation, Get-AzM… Script 1.0.0 Az.MachineLearningServices Core,Desk {Get-AzMLServiceQuota, Get-AzMLServiceUs… Script 1.4.1 Az.Maintenance Core,Desk {Get-AzApplyUpdate, Get-AzConfigurationA… Script 1.2.0 Az.ManagedServiceIdentity Core,Desk {Get-AzSystemAssignedIdentity, Get-AzUse… Script 3.0.0 Az.ManagedServices Core,Desk {Get-AzManagedServicesAssignment, Get-Az… Script 2.0.0 Az.MarketplaceOrdering Core,Desk {Get-AzMarketplaceTerms, Invoke-AzMarket… Script 1.1.2 Az.Media Core,Desk {Sync-AzMediaServiceStorageKey, Set-AzMe… Script 2.2.0 Az.Migrate Core,Desk {Get-AzMigrateDiscoveredServer, Get-AzMi… Script 5.0.0 Az.Monitor Core,Desk {Get-AzMetricDefinition, Get-AzMetric, R… Script 1.1.1 Az.MySql Core,Desk {Get-AzMySqlConfiguration, Get-AzMySqlCo… Script 7.3.0 Az.Network Core,Desk {Add-AzApplicationGatewayAuthenticationC… Script 1.0.0 Az.NetworkCloud Core,Desk {Deploy-AzNetworkCloudCluster, Disable-A… Script 1.1.2 Az.NotificationHubs Core,Desk {Get-AzNotificationHub, Get-AzNotificati… Script 3.2.0 Az.OperationalInsights Core,Desk {New-AzOperationalInsightsAzureActivityL… Script 1.6.4 Az.PolicyInsights Core,Desk {Get-AzPolicyEvent, Get-AzPolicyState, G… Script 1.1.0 Az.PostgreSql Core,Desk {Get-AzPostgreSqlConfiguration, Get-AzPo… Script 2.0.0 Az.PowerBIEmbedded Core,Desk {Resume-AzPowerBIEmbeddedCapacity, Suspe… Script 1.0.4 Az.PrivateDns Core,Desk {Get-AzPrivateDnsZone, Remove-AzPrivateD… Script 6.7.0 Az.RecoveryServices Core,Desk {Add-AzRecoveryServicesAsrReplicationPro… Script 1.8.1 Az.RedisCache Core,Desk {Remove-AzRedisCachePatchSchedule, New-A… Script 1.2.0 Az.RedisEnterpriseCache Core,Desk {Export-AzRedisEnterpriseCache, Get-AzRe… Script 2.0.0 Az.Relay Core,Desk {Get-AzRelayAuthorizationRule, Get-AzRel… Script 1.2.0 Az.ResourceMover Core,Desk {Add-AzResourceMoverMoveResource, Get-Az… Script 6.13.0 Az.Resources Core,Desk {Export-AzResourceGroup, Export-AzTempla… Script 1.5.1 Az.Security Core,Desk {Get-AzSecuritySolution, Get-AzSecurityS… Script 3.1.1 Az.SecurityInsights Core,Desk {Get-AzSentinelAlertRule, Get-AzSentinel… Script 3.0.0 Az.ServiceBus Core,Desk {New-AzServiceBusAuthorizationRuleSASTok… Script 3.3.2 Az.ServiceFabric Core,Desk {Add-AzServiceFabricClientCertificate, A… Script 2.0.0 Az.SignalR Core,Desk {New-AzSignalR, Get-AzSignalR, Get-AzSig… Script 4.13.0 Az.Sql Core,Desk {Add-AzSqlDatabaseToFailoverGroup, Add-A… Script 2.1.0 Az.SqlVirtualMachine Core,Desk {Get-AzAvailabilityGroupListener, Get-Az… Script 2.2.3 Az.StackHCI Core,Desk {Add-AzStackHCIVMAttestation, Disable-Az… Script 6.1.0 Az.Storage Core,Desk {Add-AzRmStorageContainerLegalHold, Add-… Script 1.3.0 Az.StorageMover Core,Desk {Get-AzStorageMover, Get-AzStorageMoverA… Script 2.1.1 Az.StorageSync Core,Desk {Get-AzStorageSyncCloudEndpoint, Get-AzS… Script 2.0.0 Az.StreamAnalytics Core,Desk {Get-AzStreamAnalyticsCluster, Get-AzStr… Script 1.0.0 Az.Support Core,Desk {Get-AzSupportService, Get-AzSupportProb… Script 3.0.5 Az.Synapse Core,Desk {Add-AzSynapseDataFlowDebugSessionPackag… Script 1.2.2 Az.TrafficManager Core,Desk {Add-AzTrafficManagerCustomHeaderToEndpo… Script 3.1.2 Az.Websites Core,Desk {Get-AzAppServicePlan, Set-AzAppServiceP… Directory: C:\app\runtimes\win\lib\net6.0\Modules ModuleType Version PreRelease Name PSEdition ExportedCommands ---------- ------- ---------- ---- --------- ---------------- Manifest 7.0.0.0 CimCmdlets Core {Get-CimAssociatedInstance, Get-CimClass… Manifest 7.0.0.0 Microsoft.PowerShell.Diagnostics Core {Get-WinEvent, New-WinEvent, Get-Counter} Manifest 7.0.0.0 Microsoft.PowerShell.Host Core {Start-Transcript, Stop-Transcript} Manifest 7.0.0.0 Microsoft.PowerShell.Management Core {Add-Content, Clear-Content, Get-Clipboa… Manifest 7.0.0.0 Microsoft.PowerShell.Security Core {Get-Acl, Set-Acl, Get-PfxCertificate, G… Manifest 7.0.0.0 Microsoft.PowerShell.Utility Core {Export-Alias, Get-Alias, Import-Alias, … Manifest 7.0.0.0 Microsoft.WSMan.Management Core {Disable-WSManCredSSP, Enable-WSManCredS… Script 7.0.0.0 PSDiagnostics Core {Disable-PSTrace, Disable-PSWSManCombine… Directory: C:\Program Files\WindowsPowerShell\Modules ModuleType Version PreRelease Name PSEdition ExportedCommands ---------- ------- ---------- ---- --------- ---------------- Script 1.0.1 Microsoft.PowerShell.Operation.Val… Desk {Get-OperationValidation, Invoke-Operati… Binary 1.0.0.1 PackageManagement Desk {Find-Package, Get-Package, Get-PackageP… Script 3.4.0 Pester Desk {Describe, Context, It, Should…} Script 1.0.0.1 PowerShellGet Desk {Install-Module, Find-Module, Save-Modul… Script 2.0.0 beta2 PSReadline Desk {Get-PSReadLineKeyHandler, Set-PSReadLin… Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules ModuleType Version PreRelease Name PSEdition ExportedCommands ---------- ------- ---------- ---- --------- ---------------- Manifest 2.0.1.0 Appx Core,Desk {Add-AppxPackage, Get-AppxPackage, Get-A… Manifest 1.0.0.0 BranchCache Core,Desk {Add-BCDataCacheExtension, Clear-BCCache… Manifest 1.0.0.0 DirectAccessClientComponents Core,Desk {Disable-DAManualEntryPointSelection, En… Manifest 1.0.0.0 DnsClient Core,Desk {Resolve-DnsName, Clear-DnsClientCache, … Manifest 1.0.0.0 EventTracingManagement Core,Desk {Start-EtwTraceSession, New-EtwTraceSess… Script 1.0.0.0 LAPS Core,Desk {Find-LapsADExtendedRights, Get-LapsADPa… Manifest 3.0.0.0 Microsoft.PowerShell.Diagnostics Core,Desk {Get-WinEvent, Get-Counter, Import-Count… Manifest 1.0.0.0 Microsoft.PowerShell.LocalAccounts Core,Desk {Add-LocalGroupMember, Disable-LocalUser… Manifest 3.1.0.0 Microsoft.PowerShell.Management Core,Desk {Add-Content, Clear-Content, Clear-ItemP… Manifest 1.0 MMAgent Core,Desk {Disable-MMAgent, Enable-MMAgent, Set-MM… Manifest 2.0.0.0 NetAdapter Core,Desk {Disable-NetAdapter, Disable-NetAdapterB… Manifest 1.0.0.0 NetConnection Core,Desk {Get-NetConnectionProfile, Set-NetConnec… Manifest 1.0.0.0 NetEventPacketCapture Core,Desk {New-NetEventSession, Remove-NetEventSes… Manifest 2.0.0.0 NetLbfo Core,Desk {Add-NetLbfoTeamMember, Add-NetLbfoTeamN… Manifest 1.0.0.0 NetNat Core,Desk {Get-NetNat, Get-NetNatExternalAddress, … Manifest 2.0.0.0 NetQos Core,Desk {Get-NetQosPolicy, Set-NetQosPolicy, Rem… Manifest 2.0.0.0 NetSecurity Core,Desk {Get-DAPolicyChange, New-NetIPsecAuthPro… Manifest 1.0.0.0 NetSwitchTeam Core,Desk {New-NetSwitchTeam, Remove-NetSwitchTeam… Manifest 1.0.0.0 NetTCPIP Core,Desk {Get-NetIPAddress, Get-NetIPInterface, G… Manifest 1.0.0.0 NetworkConnectivityStatus Core,Desk {Get-DAConnectionStatus, Get-NCSIPolicyC… Manifest 1.0.0.0 NetworkTransition Core,Desk {Add-NetIPHttpsCertBinding, Disable-NetD… Manifest 1.0 NFS Core,Desk {Get-NfsMappedIdentity, Get-NfsNetgroup,… Manifest 1.0.0.0 PcsvDevice Core,Desk {Get-PcsvDevice, Start-PcsvDevice, Stop-… Manifest 1.0.0.0 PnpDevice Core,Desk {Get-PnpDevice, Get-PnpDeviceProperty, E… Manifest 1.0.0.0 ScheduledTasks Core,Desk {Get-ScheduledTask, Set-ScheduledTask, R… Manifest 2.0.0.0 SecureBoot Core,Desk {Confirm-SecureBootUEFI, Set-SecureBootU… Manifest 2.0.0.0 SmbShare Core,Desk {Get-SmbShare, Remove-SmbShare, Set-SmbS… Manifest 2.0.0.0 SmbWitness Core,Desk {Get-SmbWitnessClient, Move-SmbWitnessCl… Manifest 2.0.0.0 SoftwareInventoryLogging Core,Desk {Get-SilComputer, Get-SilComputerIdentit… Manifest 2.0.0.0 Storage Core,Desk {Add-InitiatorIdToMaskingSet, Add-Partit… Manifest 1.0.0.0 Wdac Core,Desk {Get-OdbcDriver, Set-OdbcDriver, Get-Odb… Script 1.0 WindowsErrorReporting Core,Desk {Enable-WindowsErrorReporting, Disable-W… Manifest 1.0.0.0 WindowsUpdate Core,Desk Get-WindowsUpdateLog Manifest 1.0.0.2 WindowsUpdateProvider Core,Desk {Get-WUAVersion, Get-WULastInstallationD…
-
Mwa1234 1 Reputation point
2024-06-03T12:28:11.5066667+00:00 Now it was installed but still getting the error:
System.Management.Automation.CommandNotFoundException: The term 'Connect-AzureAD' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception) at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)