Azure runbook powershell script to copy all webapp settings

℡╲_俬逩灬. 提交于 2020-01-06 22:03:30

问题


I am trying to run the following script as a runbook to copy all settings from one webapp to another but I get the following error.

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

exception:

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

回答1:


An Automation Runbook use a different strategy for login, so you should not just copy a PowerShell script to a Runbook and expect it run exactly the same way as you run locally.

You see that the command Login-AzureRmAccount will popup a window asking for user name and password. But, in an Automation Runbook, it can't. Hence, you need to do something else in order to properly login.

  1. Create a new Active Direcotry User for your automation.

    a. Log into the Azure Classic Portal.

    b. Select Active Directory, and click your default Active Directory.

    c. Click User, and click Add User. For Type of User, choose New user in your organization. It cannot be User with an existing Microsoft account, because it will fail when trying to login in a Runbook.

    d. On the User Profile, for Roles, a Service administrator is good enough, but if you want, you can choose Global administrator. Do not Enable Multi-Factor Authentication. If you do, again, it will fail when trying to login in a Runbook.

    e. Note the user’s full name and temporary password.

    f. Back to the Classic Portal, Click Settings > Administrators > Add. Type in the user name you got above, and select your subscription.

    g. Log out of Azure and then log back in with the account you just created. You will be prompted to change the user’s password.

    Note: If you already have a "non-Microsoft" and MFA-disabled user account, you can skip this step. For more information, see Configuring Azure Automation

  2. Create a PS credential asset for your Runbook.

    a. Log into the Azure Portal, and choose your automation.

    b. In your automation account setting blade, click Assets > Credential.

    c. Click Add a credential, enter Name, User name, and Password (the user name and password you create in the previous step), and click Create.

  3. Instead of simply using Login-AzureRmAccount, you should us the following to login.

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



回答2:


This error:

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

means that $props is null, since you are passing it to Get-Member. So

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

is evaluating to null for some reason.

This is probably because you are not authenticating to Azure correctly. Please see https://azure.microsoft.com/en-us/blog/azure-automation-authenticating-to-azure-using-azure-active-directory/ and https://azure.microsoft.com/en-us/blog/announcing-azure-resource-manager-support-azure-automation-runbooks/ for more info.



来源:https://stackoverflow.com/questions/35486796/azure-runbook-powershell-script-to-copy-all-webapp-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!