1
votes

I am experiencing a very strange problem. I recently switched Azure subscription from free trial to pay-as-you-go. The PowerShell script i wrote to create Azure Resource Group, Azure Data Factory, Azure Active Directory App Azure SQL Server, Azure SQL Database does not work. below is the sample code from script and error messages

New-AzResourceGroup Test2ResourceGroupName2 -location 'westeurope'

$AzADAppName = "TestADApp1"
$AzADAppUri = "https://test.com/active-directory-app"
$AzADAppSecret = "TestSecret"
$AzADApp = Get-AzADApplication -DisplayName $AzADAppName

if (-not $AzADApp) {
    if ($AzADApp.IdentifierUris -ne $AzADAppUri) {

        $AzADApp = New-AzADApplication -DisplayName $AzADAppName -HomePage $AzADAppUri -IdentifierUris $AzADAppUri -Password $(ConvertTo-SecureString -String $AzADAppSecret -AsPlainText -Force)
        }
 }


New-AzResourceGroup : Your Azure credentials have not been set up or have expired, please run Connect-AzAccount to set up your Azure credentials.
At line:1 char:1
+ New-AzResourceGroup Test2ResourceGroupName2 -location 'westeurope'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzResourceGroup], ArgumentException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupCmdlet

Get-AzADApplication : User was not found.
At line:6 char:12
+ $AzADApp = Get-AzADApplication -DisplayName $AzADAppName
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-AzADApplication], Exception
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ActiveDirectory.GetAzureADApplicationCommand

New-AzADApplication : User was not found.
At line:11 char:20
+ ...  $AzADApp = New-AzADApplication -DisplayName $AzADAppName -HomePage $ ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-AzADApplication], Exception
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ActiveDirectory.NewAzureADApplicationCommand   

However if i execute this command in Azure Cloud Shell it works.

New-AzResourceGroup Test2ResourceGroupName -location 'westeurope'

enter image description hereI am also able to create Resource Group and other resources in Azure Portal. We cannot use portal and we have to use powershell due to company policy. could anyone help why PowerShell is not working

Here is the full script as requested in comments

Connect-AzAccount -TenantID xxxxx-xxx-xxx-xxxxx-xxxxx

# Creating Azure Active Directory App

$AzADAppName = "xxxxx-active-directory-app"
$AzADAppUri = "https://xxxxx.com/xxxxx-app"
$AzADAppSecret = "xxxxx"
$AzADApp = Get-AzADApplication -DisplayName $AzADAppName

if (-not $AzADApp) {
    if ($AzADApp.IdentifierUris -ne $AzADAppUri) {

        $AzADApp = New-AzADApplication -DisplayName $AzADAppName -HomePage $AzADAppUri -IdentifierUris $AzADAppUri -Password $(ConvertTo-SecureString -String $AzADAppSecret -AsPlainText -Force)
        $AzADServicePrincipal = New-AzADServicePrincipal -ApplicationId $AzADApp.ApplicationId

        # Assign the Contributor RBAC role to the service principal
        # If you get a PrincipalNotFound error: wait 15 seconds, then rerun the following until successful

        $Retries = 0; While ($NewRole -eq $null -and $Retries -le 6) {
            # Sleep here for a few seconds to allow the service principal application to become active (usually, it will take only a couple of seconds)
            Sleep 15
            New-AzRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $AzADApp.ApplicationId -ErrorAction SilentlyContinue
            $NewRole = Get-AzRoleAssignment -ServicePrincipalName $AzADServicePrincipal.ApplicationId -ErrorAction SilentlyContinue
            $Retries++;
        }

        "Application {0} Created Successfully" -f $AzADApp.DisplayName


        # Display the values for your application 
        "Save these values for using them in your application"
        "Subscription ID: {0}" -f (Get-AzContext).Subscription.SubscriptionId
        "Tenant ID:{0}" -f (Get-AzContext).Tenant.TenantId
        "Application ID:{0}" -f $AzADApp.ApplicationId
        "Application AzADAppSecret :{0}" -f $AzADAppSecret
    }
}
else {
    "Application{0} Already Exists" -f $AzADApp.DisplayName
}


# Creating Azure Resource Group

$DataFactoryName = "xxxxx-DataFactory"
$ResourceGroupName = "xxxxx-ResourceGroup"
$ResourceGroup = Get-AzResourceGroup -Name $ResourceGroupName
$Location = 'westeurope'

if (-not $ResourceGroup) {
    $ResourceGroup = New-AzResourceGroup $ResourceGroupName -location 'westeurope'
    if ($ResourceGroup) {
        "Resource Group {0} Created Successfully" -f $ResourceGroup.ResourceGroupName
    }
    else {
        "ERROR: Resource Group Creation UNSUCCESSFUL"
    }
}
else {

    "Resource Group {0} Exists" -f $ResourceGroup.ResourceGroupName 
}

# Creating Azure Data Factory

$DataFactory = Get-AzDataFactoryV2 -Name $DataFactoryName -ResourceGroupName $ResourceGroup.ResourceGroupName

if (-not $DataFactory) {
    $DataFactory = Set-AzDataFactoryV2 -ResourceGroupName $ResourceGroup.ResourceGroupName -Location $ResourceGroup.Location -Name $DataFactoryName
    if ($DataFactory) {
        "Data Factory {0} Created Successfully" -f $DataFactory.DataFactoryName
    }
    else {
        "ERROR: Data Factory Creation UNSUCCESSFUL"
    }
}
else {
    "Data Factory {0} Already Exists" -f $DataFactory.DataFactoryName 
}


# Creating Azure SQL Server and  Database
$ServerName = "xxxxx"
$DatabaseName = "xxxxx"
$AzSQLServer = Get-AzSqlServer -ServerName $ServerName

$Subscription = Get-AzSubscription

"Subscription Data" -f $Subscription.Id

if (-not $AzSQLServer) {
    "Creating New Azure SQL Server"

    $AdminSqlLogin = "xxxxx"
    $Password = "xxxxx"
    $StartIp = "xxxxx.xxxxx.xxxxx.xxxxx"
    $EndIp = "xxxxx.xxxxx.xxxxx.xxxxx"

    $AzSQLServer = New-AzSqlServer -ResourceGroupName $ResourceGroupName `
        -ServerName $ServerName `
        -Location $Location `
        -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminSqlLogin, $(ConvertTo-SecureString -String $Password -AsPlainText -Force))
    if ($AzSQLServer) {  
        $FireWallRule = New-AzSqlServerFirewallRule -ResourceGroupName $ResourceGroupName `
            -ServerName $ServerName `
            -FirewallRuleName "AllowedIPs" -StartIpAddress $StartIp -EndIpAddress $EndIp
        if ($FireWallRule) {
            "Server Created Successfully {0} with firewall Rule Setup" -f $AzSQLServer.ServerName
        }
        else {
            "Server Created Successfully {0} No FireWall Setup" -f $AzSQLServer.ServerName
        }
    }
    else {
        "ERROR: Server Creation UNSUCCESSFUL"
    }

}
else {
    "Server Exists {0}" -f $AzSQLServer.ServerName
}

$AzSQLDatabase = Get-AzSqlDatabase -DatabaseName $DatabaseName -ServerName $ServerName -ResourceGroupName $ResourceGroup.ResourceGroupName

if (-not $AzSQLDatabase) {
    "Creating New Azure SQL Database" 
    $Parameters = @{
        ResourceGroupName             = $ResourceGroupName
        ServerName                    = $ServerName
        DatabaseName                  = $DatabaseName
        RequestedServiceObjectiveName = 'S0'
    }
    $AzSQLDatabase = New-AzSqlDatabase @Parameters
    if ($AzSQLDatabase) {
        "Azure SQL Database {0} Created Successfully " -f $AzSQLDatabase.DatabaseName
    }
    else {
        "ERROR: Azure SQL Database Creation UNSUCCESSFUL"
    }
}
else {
    "Database {0} Exists " -f $AzSQLDatabase.DatabaseName
}
1
Could you share the full script please? How are you authenticating to Azure? Are you using Select-AzContext to set the account and subscription you're operating on?mehmetseckin
@MehmetSeckin Thanks I have shared the script3355307
If you change the subscription and the tenant, then you need to connect to Azure with the new credentials which you can log in Azure in the portal for the new subscription and tenant. For the cloud shell, it means you already logged in.Charles Xu
@3355307 Can this issue be related to this stackoverflow.com/questions/56691220/…?AmanGarg-MSFT
@AmanGarg-MSFT yes and i have asked that user to put the same answer for this question as both are related issues3355307

1 Answers

2
votes

You could use Clear-AzContext to remove all Azure credentials, account, and subscription information. Then use Connect-AzAccount -Tenant xxxxx -Subscription xxxxx, it should work.