更改为其他Azure订阅时,Azure PowerShell脚本不起作用 [英] Azure PowerShell script does not work when change to a different Azure Subscription

查看:101
本文介绍了更改为其他Azure订阅时,Azure PowerShell脚本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个非常奇怪的问题.我最近将Azure订阅从free trial切换为pay-as-you-go.我编写的用于创建Azure Resource GroupAzure Data FactoryAzure Active Directory AppAzure SQL ServerAzure SQL DatabasePowerShell脚本不起作用.以下是脚本和错误消息中的示例代码

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   

但是,如果我在Azure Cloud Shell中执行此命令,它将起作用.

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

New-AzResourceGroup Test2ResourceGroupName -location 'westeurope'

我还能够在Azure Portal中创建Resource Group和其他资源.由于公司政策,我们不能使用门户网站,而必须使用Powershell.谁能帮忙为什么PowerShell无法正常工作

I 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
}

推荐答案

您可以使用Clear-AzContext删除所有Azure凭据,帐户和订阅信息.然后使用Connect-AzAccount -Tenant xxxxx -Subscription xxxxx,它应该可以工作.

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.

这篇关于更改为其他Azure订阅时,Azure PowerShell脚本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆