استخدام PowerShell لإدارة موارد ناقل خدمة Microsoft Azure

Microsoft Azure PowerShell هي بيئة برمجة نصية يمكنك استخدامها للتحكم في نشر وإدارة خدمات Azure والتنفيذ التلقائي لها. توضح هذه المقالة كيفية استخدام الوحدة النمطية PowerShell إدارة موارد ناقل خدمة Microsoft Azure لتوفير وإدارة كيانات ناقل خدمة Microsoft Azure (مساحات الأسماء وقوائم الانتظار والموضوعات والاشتراكات) باستخدام وحدة تحكم Azure PowerShell أو برنامج نصي محلي.

يمكنك أيضاً إدارة كيانات ناقل خدمة Microsoft Azure باستخدام قوالب Azure Resource Manager. لمزيد من المعلومات، راجع المقالة إنشاء موارد ناقل الخدمة باستخدام قوالب Azure Resource Manager.

ملاحظة

نوصي باستخدام وحدة Azure Az PowerShell للتفاعل مع Azure. راجع تثبيت Azure PowerShell للبدء. لمعرفة كيفية الترحيل إلى الوحدة النمطية Az PowerShell، راجع ترحيل Azure PowerShell من AzureRM إلى Az.

المتطلبات الأساسية

قبل أن تبدأ، ستحتاج إلى المتطلبات الأساسية التالية:

الشروع في العمل

تتمثل الخطوة الأولى في استخدام PowerShell لتسجيل الدخول إلى حساب Azure واشتراك Azure. اتبع الإرشادات الواردة في بدء استخدام Azure PowerShell cmdlets لتسجيل الدخول إلى حساب Azure الخاص بك، واسترداد الموارد والوصول إليها في اشتراك Azure الخاص بك.

توفير مساحة اسم ناقل خدمة Microsoft Azure

عند العمل مع مساحات أسماء ناقل خدمة Microsoft Azure، يمكنك استخدام Get-AzServiceBusNamespaceو New-AzServiceBusNamespaceو Remove-AzServiceBusNamespaceو Set-AzServiceBusNamespace أوامر cmdlets.

ينشئ هذا المثال بعض المتغيرات المحلية في البرنامج النصي؛ $Namespace و$Location.

  • $Namespace هو اسم مساحة اسم ناقل خدمة Microsoft Azure التي نريد العمل بها.
  • $Location يحدد مركز البيانات الذي نوفر فيه مساحة الاسم.
  • $CurrentNamespace يخزن مساحة اسم المرجع التي نسترجعها (أو ننشئها).

في البرنامج النصي الفعلي، يمكن تمرير $Namespace و$Location كمعلمات.

يقوم هذا الجزء من البرنامج النصي بما يلي:

  1. يحاول استرداد مساحة اسم ناقل خدمة Microsoft Azure بالاسم المحدد.

  2. إذا تم العثور على مساحة الاسم، فإنها تبلغ عما تم العثور عليه.

  3. إذا لم يتم العثور على مساحة الاسم، فإنها تنشئ مساحة الاسم ثم تسترجع مساحة الاسم المنشأة حديثاً.

     # Query to see if the namespace currently exists
    $CurrentNamespace = Get-AzServiceBusNamespace -ResourceGroup $ResGrpName -NamespaceName $Namespace
    
    # Check if the namespace already exists or needs to be created
    if ($CurrentNamespace)
    {
        Write-Host "The namespace $Namespace already exists in the $Location region:"
     	# Report what was found
     	Get-AzServiceBusNamespace -ResourceGroup $ResGrpName -NamespaceName $Namespace
    }
    else
    {
        Write-Host "The $Namespace namespace does not exist."
        Write-Host "Creating the $Namespace namespace in the $Location region..."
        New-AzServiceBusNamespace -ResourceGroup $ResGrpName -NamespaceName $Namespace -Location $Location
        $CurrentNamespace = Get-AzServiceBusNamespace -ResourceGroup $ResGrpName -NamespaceName $Namespace
        Write-Host "The $Namespace namespace in Resource Group $ResGrpName in the $Location region has been successfully created."
    
    }
    

إنشاء قاعدة ترخيص مساحة الاسم

يوضح المثال التالي كيفية إدارة قواعد ترخيص مساحة الاسم باستخدام الأوامر New-AzServiceBusAuthorizationRule وGet-AzServiceBusAuthorizationRule وSet-AzServiceBusAuthorizationRule وRemove-AzServiceBusAuthorizationRule.

# Query to see if rule exists
$CurrentRule = Get-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthorizationRuleName $AuthRule

# Check if the rule already exists or needs to be created
if ($CurrentRule)
{
    Write-Host "The $AuthRule rule already exists for the namespace $Namespace."
}
else
{
    Write-Host "The $AuthRule rule does not exist."
    Write-Host "Creating the $AuthRule rule for the $Namespace namespace..."
    New-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthorizationRuleName $AuthRule -Rights @("Listen","Send")
    $CurrentRule = Get-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthorizationRuleName $AuthRule
    Write-Host "The $AuthRule rule for the $Namespace namespace has been successfully created."

    Write-Host "Setting rights on the namespace"
    $authRuleObj = Get-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthorizationRuleName $AuthRule

    Write-Host "Remove Send rights"
    $authRuleObj.Rights.Remove("Send")
    Set-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthRuleObj $authRuleObj

    Write-Host "Add Send and Manage rights to the namespace"
    $authRuleObj.Rights.Add("Send")
    Set-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthRuleObj $authRuleObj
    $authRuleObj.Rights.Add("Manage")
    Set-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthRuleObj $authRuleObj

    Write-Host "Show value of primary key"
    $CurrentKey = Get-AzServiceBusKey -ResourceGroup $ResGrpName -NamespaceName $Namespace -Name $AuthRule
        
    Write-Host "Remove this authorization rule"
    Remove-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -Name $AuthRule
}

قم بإنشاء قائمة انتظار

لإنشاء قائمة انتظار أو موضوع، قم بإجراء فحص مساحة الاسم باستخدام البرنامج النصي في القسم السابق. ثم قم بإنشاء قائمة الانتظار:

# Check if queue already exists
$CurrentQ = Get-AzServiceBusQueue -ResourceGroup $ResGrpName -NamespaceName $Namespace -QueueName $QueueName

if($CurrentQ)
{
    Write-Host "The queue $QueueName already exists in the $Location region:"
}
else
{
    Write-Host "The $QueueName queue does not exist."
    Write-Host "Creating the $QueueName queue in the $Location region..."
    New-AzServiceBusQueue -ResourceGroup $ResGrpName -NamespaceName $Namespace -QueueName $QueueName
    $CurrentQ = Get-AzServiceBusQueue -ResourceGroup $ResGrpName -NamespaceName $Namespace -QueueName $QueueName
    Write-Host "The $QueueName queue in Resource Group $ResGrpName in the $Location region has been successfully created."
}

تعديل خصائص قائمة الانتظار

بعد تنفيذ البرنامج النصي في القسم السابق، يمكنك استخدام الأمر Set-AzServiceBusQueue لتحديث خصائص قائمة الانتظار، كما في المثال التالي:

$CurrentQ.DeadLetteringOnMessageExpiration = $True
$CurrentQ.MaxDeliveryCount = 7
$CurrentQ.MaxSizeInMegabytes = 2048
$CurrentQ.EnableExpress = $True

Set-AzServiceBusQueue -ResourceGroup $ResGrpName -NamespaceName $Namespace -QueueName $QueueName -QueueObj $CurrentQ

توفير كيانات أخرى لناقل خدمة Microsoft Azure

يمكنك استخدام وحدة ناقل خدمة Microsoft Azure PowerShell النمطية لتوفير الكيانات الأخرى، مثل الموضوعات والاشتراكات. تتشابه أوامر cmdlets هذه من الناحية التركيبية مع أوامر cmdlets الخاصة بإنشاء قائمة الانتظار الموضحة في القسم السابق.

الخطوات التالية

هناك بعض الطرق البديلة لإدارة كيانات ناقل خدمة Microsoft Azure، كما هو موضح في منشورات المدونة هذه: