locked
Unable to fetch data from azure vm RRS feed

  • Question

  • Hi Support,

    I have 50 azure window and linux vm and i am looking power shell script to fetch data like what service in running inside vm or i want to any particular service in running in vm or not

    Tuesday, June 2, 2020 6:30 PM

Answers

  • Hi There,

    You have a request similar like my request, earlier I worked on a request to fetch all local admins from azure into a csv file, Real challenge. But thanks for the powershell it made it possible to do so.

    You can use invoke-command on Azure VMs, it is just like a normal invoke command. And for this you only need contributor rights on VMs.
    Let me share the command and process,

    $VMS = Get-AzVM | Where {$_.StorageProfile.OsDisk.OsType -eq "Windows"}                                                                               C:\Users\gour6761> foreach($VM in $VMS)
    {
    Invoke-AzVMRunCommand -ResourceGroupName $VM.ResourceGroupName -VMName $VM.Name -CommandId "RunPowerShellScript" -ScriptPath "C:\Temp\azinvoketest.ps1"
    }

    This command will find all Windows machine and store them into a variable. Once this is done then it will invoke the script on all VMs as we have used powershell's foreach here.

    Now the next part is to create a script for your task, like you want to see service status etc.
    Here I have written a test script for you that is checking first 5 service status on server and populating the Name, DisplayName and Status on console. You can change this script as per your requirement, I have placed this in C:\temp and given a name azinvoketest.ps1

    Get-Service | select Name, DisplayName , Status | Select -First 5

    Thats all you need to do, 

    let me share my screen output,

    Hope this helps :)
    Now you have to work on your code to make this output more fancy.


    Cheers, Gourav Please remember to mark the replies as answers if it helped.

    • Marked as answer by PK_TECHNET Tuesday, June 9, 2020 4:26 PM
    Friday, June 5, 2020 9:20 AM

All replies

  • You can search and find many scripts that will do what you ask.

    Please carefully review the following links to set your expectation for posting in technical forums.


    \_(ツ)_/

    Tuesday, June 2, 2020 6:39 PM
  • Thanks.
    Wednesday, June 3, 2020 5:26 AM
  • Hi,

    Maybe you can try this :

    And this is about Azure Powershell how to get running services of a VM via Runbook.

    # Try and enumerate the VM's services 
    foreach ($VM in $VMs) {
            Write-Output "Listing all services..."
            Write-Output ("VM: {0}" -f $VM.Name)
            $ResourceGroupName=$VM.ResourceGroupName
            $Name=$VM.Name
            $status=(Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $Name -Status).Statuses[1].code
            if($status -like "PowerState/deallocated")
            {
                Write-Output "VM is stopped, starting VM"
                if (Start-AzureRmVm -ResourceGroupName $ResourceGroupName -Name $Name)
                {
                    Write-Output "Start VM successfuly"
                }else
                {
                    Write-Output "Start VM failed"
                    break
                }
    
            }
            ##get VM's Public IP
            $nicName = ($VM.NetworkInterfaceIDs[0] -split '/')[-1]
            $pip = (Get-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $ResourceGroupName).IpConfigurations.publicipaddress.id
            $PublicIP=(Get-AzureRmPublicIpAddress -ResourceGroupName shui -Name ($pip -split '/')[-1]).IpAddress
    
            $Uri="http://$($PublicIP):5986"
            Write-Output "Get ConnectionUri $Uri"
    
            ##get Credential from assets
            $shui=Get-AutomationPSCredential -Name 'shui' 
    
            $session=New-PSSession -ConnectionUri $Uri -Credential $shui -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)
    
            $result=Invoke-Command -Session $session -ScriptBlock {ipconfig}
            Write-Output "$result"
            Write-Output "Finished listing..."
    }

    Before you use this script. Firstly,you should open ports 5896 on your VM's firewall and NSG, you could check as the following link. Please ensure you could on your local PC.telnet IP 5986

    1. You should import modules to your Automation Account. More information about how to import Modules please refer to this link.AzureRM.Network

    3.Store your VMs's passord to Runbook, you could refer to this link. When you want to use the credentials, you could use the script below:

    ##get Credential from assets
    $shui=Get-AutomationPSCredential -Name 'shui' 
    
    $session=New-PSSession -ConnectionUri $Uri -Credential $shui -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)

    Really hope that will help you.

    Please let us know if you would like further assistance.

    Best wishes,

    Young Yang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, June 3, 2020 5:46 AM
  • Hi There,

    You have a request similar like my request, earlier I worked on a request to fetch all local admins from azure into a csv file, Real challenge. But thanks for the powershell it made it possible to do so.

    You can use invoke-command on Azure VMs, it is just like a normal invoke command. And for this you only need contributor rights on VMs.
    Let me share the command and process,

    $VMS = Get-AzVM | Where {$_.StorageProfile.OsDisk.OsType -eq "Windows"}                                                                               C:\Users\gour6761> foreach($VM in $VMS)
    {
    Invoke-AzVMRunCommand -ResourceGroupName $VM.ResourceGroupName -VMName $VM.Name -CommandId "RunPowerShellScript" -ScriptPath "C:\Temp\azinvoketest.ps1"
    }

    This command will find all Windows machine and store them into a variable. Once this is done then it will invoke the script on all VMs as we have used powershell's foreach here.

    Now the next part is to create a script for your task, like you want to see service status etc.
    Here I have written a test script for you that is checking first 5 service status on server and populating the Name, DisplayName and Status on console. You can change this script as per your requirement, I have placed this in C:\temp and given a name azinvoketest.ps1

    Get-Service | select Name, DisplayName , Status | Select -First 5

    Thats all you need to do, 

    let me share my screen output,

    Hope this helps :)
    Now you have to work on your code to make this output more fancy.


    Cheers, Gourav Please remember to mark the replies as answers if it helped.

    • Marked as answer by PK_TECHNET Tuesday, June 9, 2020 4:26 PM
    Friday, June 5, 2020 9:20 AM
  • Thanks,
    Tuesday, June 9, 2020 4:26 PM