I need to connect to AD in azure function app using powershell script. (as it is in function i need to do it without prompt)
I am trying this:
# Import AzureAD
Import-Module "D:\home\site\modules\AzureAD.psd1" -ErrorAction SilentlyContinue
$appId = "myAppId"
$thumb = "certThumb"
$tenantId = "myTenantId"
Connect-AzureAD -TenantId $tenantId -ApplicationId $appId -CertificateThumbprint $thumb
Unfortunately i am getting following error:
The term 'Connect-AzureAD' is not recognized as the name of a cmdlet, function, script file, or operable program.
I copied azureAd modules to fs of the function app, but it still looks like importing it doesn't give any result.
Do you know a way to solve it?
Using the AzureAD module in Azure Functions requires a workaround now: https://github.com/Azure/azure-functions-powershell-worker/issues/232#issuecomment-536744760
This will probably be fixed soon.
By the way, instead of copying the module into your app, consider using the Managed Dependencies feature
Related
I want to use the following code in a Azure Function powershell app:
Add-AzTableRow `
-table outputTable`
-partitionKey $partitionKey `
-rowKey ($record.id) -property #{"userId" = "001";}
I'm using this documentation as a guide. However, this guide uses Install-Module AzTable. Since I am using a Function App to run this code on a timer, I can't install the module on run time. I've followed this question/answer. I've added this to `requirements.psd1':
#{
# For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'.
# To use the Az module in your function app, please uncomment the line below.
#'Az' = '8.*'
AzTable = '2.*'
}
When I run the code I get the following error:
[Error] ERROR: The 'Add-AzTableRow' command was found in the module 'AzTable', but the module could not be loaded. For more information, run 'Import-Module AzTable'.
Could someone please give me some insight on what I'm doing wrong? I want to be able to update and query the table from the Function App without any user input.
Edit:
I have added 'Az' = '8.*' and 'AzTable' = '2.*'. I let the function install the resource by running and waiting. I'm now getting the error:
[Error] ERROR: The term 'Add-AzTableRow' 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.
I'm not sure why I'm getting this error because Add-AzTableRow is apart if the AzTable module.
The AzTable module requires the Az Module:
It requires latest PowerShell Az module installed
https://www.powershellgallery.com/packages/AzTable/2.0.1
But currently the install of the Az module is disabled in your requirements.psd1:
Remove # from #'Az' = '8.*' = 'Az' = '8.*'
After that the machine behind the function will install the required module and the code will be able to load it/acces the functions.
Note if you "activate" a module the first time, run your code and maybe you still get the error messge -> script got started before module install completed... so simply wait some minutes and retry.
Run VS Code as an Administrator. Open the Azure PowerShell Functions Project in the VS Code.
In the VS Code Terminal of project Workspace/Path, run the below cmdlets one by one:
Install-Module Az
Import-Module Az
Install-Module AzTable -Force
Import-Module AzTable
Created Azure Functions PowerShell HTTP Trigger Function and written the code with the reference of this MS Doc:
run.ps1
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."
# Interact with query parameters or the body of the request.
Connect-AzAccount -Tenant '<Tenant-Id>' -SubscriptionId '<Subscription-Id>'
Set-AzContext -Subscription "<Subscription-Id>"
$resourceGroup = "HariTestRG"
$storageAccountName ="store365rvi7b3lmoq"
$storageAccount=Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$ctx = $storageAccount.Context
Write-Host $ctx.ConnectionString
$tableName = "pshkrishtesttable"
$cloudTable = (Get-AzStorageTable –Name $tableName –Context $ctx).CloudTable
Write-Host $cloudTable.Name
$partitionKey1 = "partition2"
Write-Host "Partition Key"
# add a row
Add-AzTableRow `
-table $cloudTable `
-partitionKey $partitionKey1 `
-rowKey ("India") -property #{"username"="Jashu";"userid"=598}
Write-Host "Table Row Added"
$TableRows = Get-AzTableRow -table $cloudTable
Write-Host $TableRows | Format-Table
$body = "Hello Krishna, This HTTP triggered function executed successfully."
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]#{
StatusCode = [HttpStatusCode]::OK
Body = $body
})
requirements.psd1:
#{
'Az' = '8.*'
'Az.Storage' = '4.10.0'
'AzTable' = '2.1.0'
}
Result:
Note: If you are running the PowerShell Function with these modules for the first time, it will take some time during the runtime/execution.
I have been banging my head against the wall trying to get this to work.
Just to be clear, I am not running this locally. This runs fine when I run it locally in powershell cli.
I am running this as an Azure Function App. The weird thing is that a few lines above this I have similar code to connect to PnPOnline - that works fine.
Goal: I need to be able to pass an email address to AD and retrieve the ObjectID of that user. Again, works fine locally.
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName,
$(convertto-securestring $Password -asplaintext -force)
Import-Module AzureAD
Connect-AzureAD -Credentials $cred
The error:
The term 'Connect-AzureAD' is not recognized as the name of a cmdlet, function, script file, or operable program.
Any help would be GREATLY appreciated.
The term 'Connect-AzureAD' is not recognized as the name of a cmdlet,
function, script file, or operable program.
The error occurs because AzureAD module is not installed.
You can install AzureAD module using the below command:
Install-Module -Name AzureAD
Once the AzureAD module is installed, you can import the AzureAD module in your powershell script using below command:
Import-Module AzureAD
Now you can connect to the Azure AD and get the user object id using the below script:
Import-Module AzureAD
$secpasswd = $Password | ConvertTo-SecureString -AsPlainText -Force;$cred = New-Object Management.Automation.PSCredential ($UserName, $secpasswd);
Connect-AzureAD -Credential $cred;
$ADUser = Get-AzureADUser -Filter "EmailAddress -eq 'someEmail#something.com'"
I have created a Runbook and added below cmdlet
Get-AzFunctionApp | Stop-AzFunctionApp
I get below error
The term 'Get-AzFunctionApp' is not recognized as the name of a cmdlet,
I have imported all the below modules but no luck
After adding 'Az.Functions' I get below error
Failed
Cannot validate argument on parameter 'SubscriptionId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. (The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.)
The cmdlet Get-AzFunctionApp / Stop-AzFunctionApp are included in this module: Az.Functions.
Please import this module and then the functions related operation will work.
Here is an example(please note that add the -Force parameter for Stop-AzFunctionApp), it works as per my testing:
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
Get-AzFunctionApp -ResourceGroupName xxx | Stop-AzFunctionApp -Force
From the screenshot you posted, it looks like you are missing the Az.Functions module that contains both the Get-AzFunctionApp and Stop-AzFunctionApp cmdlets.
To import Az.Functions module into your Automation account, do the following:
From your Automation account, under Shared Resources, select Modules.
Select Browse Gallery.
In the search bar, enter the module name (Az.Functions).
On the PowerShell Module page, select Import to import the module into your Automation account.
You can also import the module through the PowerShell Gallery. Choose the Azure Automation tab and select Deploy to Azure Automation from here: Az.Functions.
I have an Azure Automation Run As account. When I run the following code (from step 5 of Azure online tutorial) on the runbook in Azure Portal, I get the error shown below. Question: What I may be missing here, and how can we resolve the issue?
runbook code:
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave –Scope Process
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
$AzureContext = Select-AzSubscription -SubscriptionId $Conn.SubscriptionID
Error:
Failed At line:4 char:1
+ Disable-AzContextAutosave –Scope Process
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cannot find the 'Disable-AzContextAutosave' command. If this command is defined as a workflow, ensure it is defined before the workflow that calls it. If it is a command intended to run directly within Windows PowerShell (or is not available on this system), place it in an InlineScript: 'InlineScript { Disable-AzContextAutosave }'
I'm assuming you havn't imported the Az.Accounts module into your automation account. Disable-AzContextAutosave, Connect-AzAccount and Select-AzSubscription are from this module.
Follow this guide to Import Az modules.
I am deploying an ARM template from azure DevOps using Azure PowerShell as shown below.
This is subscription level deployment. I am getting below error.
The term 'Get-AzSubscription' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is
correct and try again.
Install-Module -Name Az -AllowClobber -Scope CurrentUser
Import-Module Az
$context = Get-AzSubscription -SubscriptionId xxxxxxxx
Set-AzContext $context
New-azdeployment -Name "SKL" -Location westeurope -TemplateFile .\delegatedResourceManagement.json -TemplateParameterFile .\delegatedResourceManagement.parameters.json
Logs:
##[section]Starting: Azure PowerShell script: InlineScript
==============================================================================
Task : Azure PowerShell
Description : Run a PowerShell script within an Azure environment
Version : 3.153.0
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/deploy/azure-powershell
==============================================================================
##[command]Import-Module -Name C:\Modules\azurerm_6.7.0\AzureRM\6.7.0\AzureRM.psd1 -Global
##[command]Clear-AzureRmContext -Scope Process
##[command]Disable-AzureRmContextAutosave -ErrorAction Stop
##[command]Add-AzureRMAccount -ServicePrincipal -Tenant *** -Credential System.Management.Automation.PSCredential -Environment AzureCloud #processScope
##[command] Select-AzureRMSubscription -SubscriptionId xxxxxxx -TenantId ***
##[command]& 'd:\a\_temp\xxxxxxd.ps1'
##[warning]User declined to install module (Az).
##[error]The specified module 'Az' was not loaded because no valid module file was found in any module directory.
##[command]Disconnect-AzureRmAccount -Scope Process -ErrorAction Stop
##[command]Clear-AzureRmContext -Scope Process -ErrorAction Stop
##[error]The term 'Get-AzSubscription' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
##[section]Finishing: Azure PowerShell script: InlineScript
You need to specify the task version: 4.* (Preview) or higher to use the Az powershell module:
Also there is an Azure resource group deployment to deploy ARM template easily:
looks like too much confusion among all az modules, MSFT have messed up az new/old modules and are conflicting with each other. az account show will list all modules with AZ CLI
az account show
Search apps and Features - Uninstall the old April 2018 Azure
Follow something like this link https://blog.atwork.at/post/The-new-Azure-PowerShell-Az-module
Then you won't have issue with
Get-AzSubscription ( after using Connect)