Azure Runbook PowerShell脚本可复制所有WebApp设置 [英] Azure runbook powershell script to copy all webapp settings

查看:63
本文介绍了Azure Runbook PowerShell脚本可复制所有WebApp设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将以下脚本作为Runbook运行,以将所有设置从一个Web应用程序复制到另一个Web应用程序,但是出现以下错误.

  try{$ acct = Get-AzureRmSubscription}抓住{Login-AzureRmAccount}$ fromResourceGroup ='resourceG1'$ fromSite ='website1'$ toResourceGroup ='resourceG2'$ toSite ='website2'$ props =(Invoke-AzureRmResourceAction -ResourceGroupName $ fromResourceGroup -ResourceType Microsoft.Web/sites/Config -Name $ fromSite/appsettings-操作列表-ApiVersion 2015-08-01 -Force).属性$ hash = @ {}$ props |Get-Member -MemberType注意属性|%{$ hash [$ _.Name] = $ props.($ _.Name)}Set-AzureRMWebApp -ResourceGroupName $ toResourceGroup-名称$ toSite -AppSettings $ hash 

例外:

  Get-Member:您必须为Get-Member cmdlet指定一个对象.在线:18字符:10+ $ props |Get-Member -MemberType注意属性|%{$ hash [$ _.Name] = $ ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo:CloseError:(:) [Get-Member],InvalidOperationException+ FullyQualifiedErrorId:NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommandSet-AzureRMWebApp:术语"Set-AzureRMWebApp"未被识别为cmdlet,函数,脚本文件或可操作的程序.检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后尝试再次.在第20行:char:1+ Set-AzureRMWebApp -ResourceGroupName $ toResourceGroup+ ~~~~~~~~~~~~~~~~~+ CategoryInfo:ObjectNotFound:(Set-AzureRMWebApp:String)[],CommandNotFoundException+ FullyQualifiedErrorId:CommandNotFoundException-Name:术语"-Name"不被识别为cmdlet,函数,脚本文件或可运行程序的名称.查看名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试.在线:21字符:9+-名称$ toSite -AppSettings $ hash+ ~~~~~+ CategoryInfo:ObjectNotFound:(-Name:String)[],CommandNotFoundException+ FullyQualifiedErrorId:CommandNotFoundException 

解决方案

自动化Runbook使用不同的登录策略,因此,您不应仅将PowerShell脚本复制到Runbook并期望它的运行方式与运行时完全相同本地.

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

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

    a.登录

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

  2. 您不仅应使用 Login-AzureRmAccount ,还应使用以下内容进行登录.

      $ cred = Get-AutomationPSCredential-名称<您的凭证名称>"Login-AzureRmAccount-凭据$ cred 

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

解决方案

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
    

这篇关于Azure Runbook PowerShell脚本可复制所有WebApp设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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