【问题标题】:Azure runbook powershell script to copy all webapp settings用于复制所有 webapp 设置的 Azure Runbook powershell 脚本
【发布时间】:2016-05-30 22:24:27
【问题描述】:

我正在尝试将以下脚本作为运行手册运行,以将所有设置从一个 webapp 复制到另一个 webapp,但出现以下错误。

try
{   
    $acct = Get-AzureRmSubscription
}
catch
{
    Login-AzureRmAccount
}

$fromResourceGroup = 'resourceG1'
$fromSite = 'website1'
$toResourceGroup = 'resourceG2'
$toSite = 'website2'

$props = (Invoke-AzureRmResourceAction -ResourceGroupName $fromResourceGroup -ResourceType Microsoft.Web/sites/Config -Name $fromSite/appsettings -Action list -ApiVersion 2015-08-01 -Force).Properties

$hash = @{}
$props | Get-Member -MemberType NoteProperty | % { $hash[$_.Name] = $props.($_.Name) }

Set-AzureRMWebApp -ResourceGroupName $toResourceGroup
        -Name $toSite -AppSettings $hash

例外:

Get-Member : You must specify an object for the Get-Member cmdlet.
At line:18 char:10
+ $props | Get-Member -MemberType NoteProperty | % { $hash[$_.Name] = $ ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationException
    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

Set-AzureRMWebApp : The term 'Set-AzureRMWebApp' 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.
At line:20 char:1
+ Set-AzureRMWebApp -ResourceGroupName $toResourceGroup
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Set-AzureRMWebApp:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

-Name : The term '-Name' 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.
At line:21 char:9
+         -Name $toSite -AppSettings $hash
+         ~~~~~
    + CategoryInfo          : ObjectNotFound: (-Name:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

【问题讨论】:

    标签: powershell azure azure-automation


    【解决方案1】:

    Automation Runbook 使用不同的登录策略,因此您不应只是将 PowerShell 脚本复制到 Runbook 并期望它以与本地运行完全相同的方式运行。

    您会看到命令Login-AzureRmAccount 将弹出一个窗口,询问用户名和密码。但是,在自动化运行手册中,它不能。因此,您需要执行其他操作才能正确登录。

    1. 为您的自动化创建一个新的 Active Direcotry 用户。

      一个。登录Azure Classic Portal

      b.选择 Active Directory,然后点击您的默认 Active Directory。

      c。点击用户,然后点击添加用户。对于用户类型,选择您组织中的新用户。它不能是拥有现有 Microsoft 帐户的用户,因为它在尝试登录 Runbook 时会失败。

      d。在用户配置文件中,对于角色,服务管理员就足够了,但如果需要,您可以选择全局管理员。不要启用多重身份验证。如果您再次这样做,尝试登录 Runbook 时会失败。

      e。记下用户的全名和临时密码。

      f。返回经典门户,点击设置 > 管理员 > 添加。输入您在上面获得的用户名,然后选择您的订阅。

      g.注销 Azure,然后使用刚刚创建的帐户重新登录。系统将提示您更改用户密码。

      注意:如果您已有“非 Microsoft”且已禁用 MFA 的用户帐户,则可以跳过此步骤。欲了解更多信息,请参阅Configuring Azure Automation

    2. 为您的 Runbook 创建一个 PS 凭证资产。

      一个。登录Azure Portal,然后选择您的自动化。

      b.在您的自动化帐户设置刀片中,点击Assets > Credential

      c。点击添加凭据,输入名称、用户名和密码(您在上一步中创建的用户名和密码),然后点击创建

    3. 不要简单地使用Login-AzureRmAccount,您应该使用以下方式登录。

      $cred = Get-AutomationPSCredential -Name "<your credential name>"
      Login-AzureRmAccount -Credential $cred
      

    【讨论】:

      【解决方案2】:

      这个错误:

      Get-Member : You must specify an object for the Get-Member cmdlet.

      表示 $props 为空,因为您将它传递给 Get-Member。所以

      $props = (Invoke-AzureRmResourceAction -ResourceGroupName $fromResourceGroup -ResourceType Microsoft.Web/sites/Config -Name $fromSite/appsettings -Action list -ApiVersion 2015-08-01 -Force).Properties

      由于某种原因正在评估为 null。

      这可能是因为您没有正确地对 Azure 进行身份验证。请参阅https://azure.microsoft.com/en-us/blog/azure-automation-authenticating-to-azure-using-azure-active-directory/https://azure.microsoft.com/en-us/blog/announcing-azure-resource-manager-support-azure-automation-runbooks/ 了解更多信息。

      【讨论】:

        猜你喜欢
        • 2019-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-15
        • 1970-01-01
        相关资源
        最近更新 更多