How to install and configure Azure PowerShell

https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/

You can use Windows PowerShell to perform a variety of tasks in Azure, either interactively at a command prompt or automatically through scripts. Azure PowerShell is a module that provides cmdlets to manage Azure through Windows PowerShell. You can use the cmdlets to create, test, deploy, and manage solutions and services delivered through the Azure platform. In most cases, you can use the cmdlets to perform the same tasks that you can perform through the Azure Management Portal. For example, you can create and configure cloud services, virtual machines, virtual networks, and web apps.

The module is distributed as a downloadable file and the source code is managed through a publicly available repository. A link to the downloadable files is provided in the installation instructions later in this topic. For information about the source code, see Azure PowerShell code repository.

This guide provides basic information about installing and setting up Azure PowerShell to manage the Azure platform.

Prerequisites for using Azure PowerShell

Azure is a subscription-based platform. This means that a subscription is required to use the platform. In most cases, it also means that the cmdlets require subscription information to perform the tasks with your subscription. (Some of the storage-related cmdlets can be used without this information.) You provide this by configuring your computer to connect to your subscription. Instructions are provided in this article, under "How to: Connect to your subscription."

When you install the module, the installer checks your system for the required software and installs all dependencies, such as the correct version of Windows PowerShell and .NET Framework.

How to: Install Azure PowerShell

You can download and install the Azure PowerShell modules by running the Microsoft Web Platform Installer. When prompted, click Run. The Web Platform Installer installs the Azure PowerShell modules and all dependencies. Follow the prompts to complete the installation.

For more information about the command-line tools available for Azure, see Command-line tools.

Installing the module also installs a customized console for Azure PowerShell. You can run the cmdlets from either the standard Windows PowerShell console or the Azure PowerShell console.

The method you use to open either console depends on the version of Windows you're running:

  • On a computer running at least Windows 8 or Windows Server 2012, you can use the built-in Search. From the Start screen, begin typing power. This returns a scoped list of apps that includes Windows PowerShell and Azure PowerShell. To open the console, click either app. (To pin the app to the Start screen, right-click the icon.)

  • On a computer running a version earlier than Windows 8 or Windows Server 2012, use the Start menu. From the Start menu, click All Programs, click Azure, and then click Azure PowerShell.

How to: Connect to your subscription

Use of Azure requires a subscription. If you don't have a subscription, see Get Started with Azure.

The cmdlets need your subscription so they can manage your services. There are two ways to provide your subscription information to Windows PowerShell. You can use a management certificate that contains the information or you can sign in to Azure using your Microsoft account or a work or school account. When you sign in, Azure Active Directory (Azure AD) authenticates the credentials and returns an access token that lets Azure PowerShell manage your account.

To help you choose the authentication method that's appropriate for your needs, consider the following:

  • Azure AD is the recommended authentication method since it makes it easier to manage access to a subscription. With the update in version 0.8.6, it enables an automation scenario with Azure AD authentication as well if a work or school account is used. It works with Azure Resource Manager API as well.
  • When you use the certificate method, the subscription information is available as long as the subscription and the certificate are valid. However, this method makes it harder to manage access to a shared subscription, such as when more than one person is authorized to access the account. Also, Azure Resource Manager API doesn't accept certificate authentication.

For more information about authentication and subscription management in Azure, see Manage Accounts, Subscriptions, and Administrative Roles.

Use the Azure AD method

  1. Open the Azure PowerShell console, as instructed in How to: Install Azure PowerShell.

  2. Type the following command:

    Copy
    Add-AzureAccount
  3. In the window, type the email address and password associated with your account.

  4. Azure authenticates and saves the credential information, and then closes the window.

  5. Starting from 0.8.6, if you sign in using a work or school account, you can type the following command to bypass the pop up window. This will pop up the standard Windows PowerShell credential window for you to enter your work or school account user name and password.

    Copy
    $cred = Get-Credential
    Add-AzureAccount -Credential $cred

Use the certificate method

The Azure module includes cmdlets that help you download and import the certificate.

  • The Get-AzurePublishSettingsFile cmdlet opens a web page on the Azure Management Portal, from which you can download the subscription information. The information is contained in a .publishsettings file.

  • The Import-AzurePublishSettingsFile imports the .publishsettings file for use by the module. This file includes a management certificate that has security credentials.

  1. Sign in to the Azure Management Portal using the credentials for your Azure account.

  2. Open the Azure PowerShell console, as instructed in How to: Install Azure PowerShell.

  3. Type the following command:

    Copy
    Get-AzurePublishSettingsFile
  4. When prompted, download and save the publishing profile and note the path and name of the .publishsettings file. This information is required when you run the Import-AzurePublishSettingsFile cmdlet to import the settings. The default location and file name format is:

    Copy
        C:\Users<UserProfile>\Download\[*MySubscription*-...]-*downloadDate*-credentials.publishsettings
  5. Type a command similar to the following, substituting your Windows account name and the path and file name for the placeholders:

    Copy
    Import-AzurePublishSettingsFile C:Users<UserProfile>Downloads<SubscriptionName>-credentials.publishsettings

View account and subscription details

You can have multiple accounts and subscriptions available for use by Azure PowerShell. You can add multiple accounts by running Add-AzureAccount more than once.

To get the available Azure accounts, type:

Copy
Get-AzureAccount

To get your Azure subscriptions, type:

Copy
Get-AzureSubscription

How to use the cmdlets: An example

After you've installed the module and configured your computer to connect to your subscription, you can create an Azure web app. This example will get you started using the Azure cmdlets.

  1. Start the Azure PowerShell console.

  2. Choose a name for your web app. Pick a name that conforms to DNS naming conventions. Valid names can contain only letters 'a' through 'z', numbers '0' through '9', and a hyphen ('-').

    The web app name must be unique in Azure. We'll use "mySite" in this example, but be sure to choose a different name, such as your account name followed by a number.

    After you pick a name, type a command similar to the following. Substitute your web app name for "mySite".

    Copy
    New-AzureWebsite mySite

    The cmdlet creates the web app and returns an object that represents the new web app. The object properties include useful information about the web app.

  3. To get information about the web app, type this command. It returns a bit of information about all web apps in the subscription, including the one that you just created.

    Copy
    Get-AzureWebsite
  4. To get more information about your web app, include the web app name in the command. Be sure to substitute the name of your web app for "mySite".

    Copy
    Get-AzureWebsite -Name mySite
  5. Web apps are started after they are created. To stop the web app, type this command, including the name of your web app.

    Copy
    Stop-AzureWebsite -Name mySite
  6. To verify that the site's state is 'stopped', run the Get-AzureWebsite command again.

    Copy
    Get-AzureWebsite
  7. To complete this test, delete the web app. Type:

    Copy
    Remove-AzureWebsite -Name mySite
  8. To complete the task, confirm that the web app is deleted.

    Copy
    Get-AzureWebsite -Name mySite

Getting Help

These resources provide help for specific cmdlets:

  • From within the console, you can use the built-in Help system. The Get-Help cmdlet provides access to this system. The following table provides some examples of commands you can use to get Help. You can get more information from within the console by typing help.

    Command Result
    Get-Help Describes how to use the Help system.

    Note: The description includes some information about Help files that does not apply to the Azure module. Specifically, Help files are installed when the module is installed. They are not available for download separately.

    Get-Help Azure Gets all cmdlets in the Azure module.
    Get-Help <language>-dev Gets cmdlets for developing and managing applications in a specific language. For example, help node-dev, help php-dev, or help python-dev.
    Get-Help <cmdlet> Gets help about a Windows PowerShell cmdlet. Replace with the cmdlet name.
    Get-Help <cmdlet> -Parameter * Gets descriptions of the cmdlet parameters. The asterisk (*) means "all".
    Get-Help <cmdlet> -Examples Gets the syntax and examples of using the cmdlet.
    Get-Help <cmdlet> -Full Gets all help for a cmdlet, including technical details.
  • Reference information about the cmdlets in the Azure PowerShell modules is also available in the Azure library. For information, see Azure Cmdlet Reference.

For help from the community, try these popular forums:

Additional Resources

These are some of the resources available that you can use to learn to use Azure and Windows PowerShell.

原文地址:https://www.cnblogs.com/hellohongfu/p/4823503.html