Git Product home page Git Product logo

isabella232 / resource-manager-powershell-certificate-authentication Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azure-samples/resource-manager-powershell-certificate-authentication

0.0 0.0 0.0 32 KB

This sample demonstrates how to access Azure resource data by certificate authentication in Powershell

Home Page: https://gallery.technet.microsoft.com/Access-Azure-resource-data-ca9cc9f7

PowerShell 100.00%

resource-manager-powershell-certificate-authentication's Introduction

services platforms author
resource manager
powershell
msonecode

Access Azure resource data by certificate authentication in PowerShell

Introduction

This sample demonstrates how to automatically get a list of all the resources (VMs, Storage Accounts, Databases, App Services) and status via Powershell by certificate authentication.

Scenarios

In some cases, IT admins would like to have the statistics of all azure resources (VMs, Storage Accounts, Databases, App Services) and status information. This Windows PowerShell script will help IT admins to get resources info.

Prerequisite

Install Azure PowerShell according to https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/

Script

  • Save the current Resource Manager authentication information
Login-AzureRmAccount 
Save-AzureRmProfile -Path C:\Work\PS\azureaccount.json
  • Download your subscription certificate for Azure Server Manager from https://manage.windowsazure.com/publishsettings/
  • Open the script file GetAzureResourceList.ps1, edit the variable $RmProfilePath and $PublishSettingsFilePath and then save the file
GetAzureResourceList -RmProfilePath $RmProfilePath -PublishSettingsFilePath $PublishSettingsFilePath
  • Open the powershell and run the script file GetAzureResourceList.ps1
  • After the script has finished its job, you will get a resource list file.
  • Here are some code snippets for your reference.
function GetAzureResourceList() 
{ 
    param 
    ( 
        [string] 
        $RmProfilePath =$(throw "Parameter missing: -RmProfilePath RmProfilePath"),  
        [string] 
        $PublishSettingsFilePath = $(throw "Parameter missing: -PublishSettingsFilePath PublishSettingsFilePath"), 
        [string] 
        $OutFilePath = "c:\AzureResourceList.csv" 
    ) 
      
    Try 
    { 
        #Loads Azure authentication information from a file using Resource Manager.https://portal.azure.com 
        Select-AzureRmProfile –Path $RmProfilePath -ErrorAction Stop 
 
        #Imports a publish-settings file with a certificate to connect to your Windows Azure account using Server Manager.https://manage.windowsazure.com/publishsettings/ 
        Import-AzurePublishSettingsFile -PublishSettingsFile $PublishSettingsFilePath -ErrorAction Stop 
 
        "Resource Type,Resource Name,Resource Status" | out-file $OutFilePath -encoding ascii -append 
        #Get classic VM list 
        ForEach ($classicVM in Get-AzureVM)  
        { 
            "Virtual machines(classic)," + $classicVM.Name + "," + $classicVM.Status | out-file $OutFilePath -encoding ascii -append 
        } 
        #Get resource manager VM  
        ForEach ($rmVM in Get-AzureRmVM)  
        { 
            $statuses = (Get-AzureRmVM -ResourceGroupName $rmVM.ResourceGroupName -Name $rmVM.Name -Status).Statuses  
            "Virtual machines," + $rmVM.Name + "," + $statuses.DisplayStatus | out-file $OutFilePath -encoding ascii -append 
        } 
 
        #Get classic storage accounts list 
        ForEach ($storageAccount in Get-AzureStorageAccount)  
        { 
            "Storage Account(classic)," + $storageAccount.StorageAccountName + ",Available" | out-file $OutFilePath -encoding ascii -append 
        } 
        #Get resource manager storage accounts list 
        ForEach ($rmStorageAccount in Get-AzureRmStorageAccount)  
        { 
            "Storage Account," + $rmStorageAccount.StorageAccountName + "," +  $rmStorageAccount.StatusOfPrimary | out-file $OutFilePath -encoding ascii -append 
        } 
        #Get SQL databases 
        ForEach ($sqlServer in Get-AzureSqlDatabaseServer)  
        { 
            ForEach($sqlDatabase in Get-AzureSqlDatabase -ServerName $sqlServer.ServerName) 
            { 
                If($sqlDatabase.Name -ine "master") 
                { 
                    "SQL databases," + $sqlDatabase.Name + ",Online"  | out-file $OutFilePath -encoding ascii -append 
                } 
            } 
        }     
        #Get-AzureRmResource cmdlet can get all azure resources 
        #Get App Services 
        ForEach ($resource in Get-AzureRmResource)  
        { 
            If($resource.ResourceType -ieq "Microsoft.Web/sites") 
            { 
                $webApp = Get-AzureRmWebApp -Name $resource.ResourceName 
                "App Services," + $resource.ResourceName + "," +  $webApp.State | out-file $OutFilePath -encoding ascii -append 
            } 
        }         
    } 
    Catch 
    {         
          Write-Host -ForegroundColor Red $_.Exception.Message 
    } 
} 
 
GetAzureResourceList -RmProfilePath "C:\Work\PS\azureaccount.json" -PublishSettingsFilePath "C:\Work\AzureCredentials\Visual Studio Ultimate with MSDN-8-2-2016-credentials.publishsettings" 
Write-Host "Done"

Additional Resources

resource-manager-powershell-certificate-authentication's People

Contributors

aliceqin12 avatar msonecode avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.