Author: Rahul Jumde

Azure VM Inventory report

Azure VM Inventory report

Hi, This script will help you to pull Azure VM details. You can use this script for various purpose. This script makes use of AzureRM module, please install below prerequisite modules :-

Install-Module -Name AzureRM.Compute
Install-Module -Name AzureRM.Network

    # Sign into Azure Portal
    login-azurermaccount

    # Fetching subscription list
    $subscription_id = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"      # Enter your subscription details

    # Fetch current working directory 
    $working_directory = "c:\AzureInventory"

    new-item $working_directory -ItemType Directory -Force

function Get-AzureInventory{

# Selecting the subscription
Select-AzureRmSubscription -Subscription $subscription_id

# Fetch the Virtual Machines from the subscription
$azureVMDetails = get-azurermvm

# Fetch the NIC details from the subscription
$azureNICDetails = Get-AzureRmNetworkInterface | ?{ $_.VirtualMachine -NE $null}

# Fetch the Virtual Networks from the subscription
$azureVirtualNetworkDetails = Get-AzureRmVirtualNetwork


#-----------------Fetching Virtual Machine Details-----------------#

    $virtual_machine_object = $null
    $virtual_machine_object = @()


    # Iterating over the NIC Interfaces under the subscription
        
        foreach($azureNICDetail in $azureNICDetails){ 
        $azureVMDetail = $azureVMDetails | ? -Property Id -eq $azureNICDetail.VirtualMachine.id
        $vm_status = get-azurermvm -ResourceGroupName $azureVMDetail.resourcegroupname -name $azureVMDetail.name -Status
        $vm_tags = ($azureVMDetail.Tags.values) -join ';'
        $osversion = $azureVMDetail.StorageProfile.ImageReference.id
        $vmsize = Get-AzureRmVMSize -VMName $azureVMDetail.Name -ResourceGroupName $azureVMDetail.ResourceGroupName | ? {$_.Name -eq $azureVMDetail.HardwareProfile.VmSize}
        
        #Fetching the private IP
        #write-Host $vm.NetworkInterfaceIDs
        $private_ip_address = ($azureNICDetail.IpConfigurations | select-object -ExpandProperty PrivateIpAddress) -Join ';'
        $virturalnetwork = $azureNICDetail.IpConfigurations.subnet.Id.Split("/")[-3]
        $subnet = $azureNICDetail.IpConfigurations.subnet.Id.Split("/")[-1]
            
        #Fetching data disk names
        $data_disks = $azureVMDetail.StorageProfile.DataDisks
        $data_disk_name_list = ''

            foreach ($data_disk in $data_disks) {
            $data_disk_name_list_temp = $data_disk_name_list + "; " +$data_disk.name 
            #Trimming the first three characters which contain --> " ; "
            $data_disk_name_list = $data_disk_name_list_temp.Substring(2)
            #write-host $data_disk_name_list
            }

        #}            

            # Fetching OS Details (Managed / un-managed)

            if($azureVMDetail.StorageProfile.OsDisk.manageddisk -eq $null){
                # This is un-managed disk. It has VHD property

                $os_disk_details_unmanaged = $azureVMDetail.StorageProfile.OsDisk.Vhd.Uri
                $os_disk_details_managed = "This VM has un-managed OS Disk"

            }else{
                
                $os_disk_details_managed = $azureVMDetail.StorageProfile.OsDisk.ManagedDisk.Id
                $os_disk_details_unmanaged = "This VM has Managed OS Disk"
            }

            $virtual_machine_object_temp = new-object PSObject 
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "VMName" -Value $azureVMDetail.Name
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "ResourceGroupName" -Value $azureVMDetail.ResourceGroupName
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "Location" -Value $azureVMDetail.Location
#           $virtual_machine_object_temp | add-member -membertype NoteProperty -name "Zone" -Value $azureVMDetail.Zones
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "Tags" -Value $vm_tags
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "VMStatus" -Value $vm_status.Statuses[1].DisplayStatus
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "VMSize" -Value $azureVMDetail.HardwareProfile.VmSize
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "CPU" -Value $vmsize.NumberOfCores
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "RAM in MB" -Value $vmsize.MemoryInMB
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "OSFamily" -Value $azureVMDetail.StorageProfile.OsDisk.OsType
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "DiskCount" -Value $azureVMDetail.StorageProfile.DataDisks.Count
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "AdminUserName" -Value $azureVMDetail.OSProfile.AdminUsername
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "PrivateIP" -Value $private_ip_address
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "Vnet-Zone" -Value $virturalnetwork
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "Subnet" -Value $subnet
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "OSVersion" -Value $osversion
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "DataDiskNames" -Value $data_disk_name_list
            $virtual_machine_object_temp | add-member -membertype NoteProperty -name "ManagedOSDiskURI" -Value $os_disk_details_managed
                        


            $virtual_machine_object += $virtual_machine_object_temp

            
        }

        $virtual_machine_object | Export-Csv "$working_directory\Virtual_Machine_details_$(get-date -f yyyyMMdd).csv" -NoTypeInformation -Force

}

Get-AzureInventory

Template and Powered OFF VM’s Report from vCenter server

Template and Powered OFF VM’s Report from vCenter server

This powershell script will help you to pull report of powered off VM’s and the old templates. You can run this script from your desktop directly or from your vCenter server. You need to provide vCenter server list as input. Script will ask for credentials during runtime and output will be stored in csv file.

$VCServers = Get-Content "C:\vclist.txt"

$vcUSERNAME = Read-Host 'Enter user name'
$vcPassword = Read-Host 'Enter password' -AsSecureString
$logincred = New-Object System.Management.Automation.PSCredential ($vcusername, $vcPassword)

$date = Get-Date -Format "yyyy_MM_dd_hh_mm"

#---------------------------- VC Report generation-------------------------------------------#

foreach ($VCServer in $VCServers) {

Write-Host -ForegroundColor DarkYellow "Working on $VCServer"
Connect-VIServer -Server $VCServer -Credential $logincred # -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null

#----------------- Template Report -----------------#

$csvfile_template = "C:\VC_reports\$($VCServer + "_" +$date + "_" + "template.csv")"
Write-Host -ForegroundColor Yellow "File name - $csvfile_template"

Get-Template -Name spwdfvm* | Sort-Object |
Select Name,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}} |
Export-Csv $csvfile_template -NoTypeInformation -UseCulture

#------------------- OLD VM Report -------------------#

$csvfile_vm = "C:\VC_reports\$($VCServer + "_" +$date + "_" + "OLD_VM.csv")"
Write-Host -ForegroundColor Yellow "File name - $csvfile_vm"

Get-VMHost | % { Get-VM -Location $_.Name } | where {$_.PowerState -eq "PoweredOff"}  |
Select Name, PowerState,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N="UsedSpaceGB";E={[math]::Round($_.UsedSpaceGB,1)}},
@{N="ProvisionedSpaceGB";E={[math]::Round($_.ProvisionedSpaceGB,1)}},
@{N="Folder";E={$_.Folder.Name}} |
Export-Csv $csvfile_vm -NoTypeInformation -UseCulture

disconnect-VIserver -Server $VCServer -confirm:$false

}
PowerShell and .Net version report on remote computers

PowerShell and .Net version report on remote computers

Hi Friends,

Here is powershell script to pull out report of PowerShell and .Net version on remote computer.

$computers = Get-Content -Path “C:\serverlist.txt”

ForEach($computer in $computers){
If(!(Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
Write-Host -ForegroundColor Red “$computer is offline. Proceeding with next computer”
continue
} else {
Invoke-Command -ComputerName $computer -ScriptBlock {
$Lookup = @{
378389 = [version]’4.5′
378675 = [version]’4.5.1′
378758 = [version]’4.5.1′
379893 = [version]’4.5.2′
393295 = [version]’4.6′
393297 = [version]’4.6′
394254 = [version]’4.6.1′
394271 = [version]’4.6.1′
394802 = [version]’4.6.2′
394806 = [version]’4.6.2′
460798 = [version]’4.7′
460805 = [version]’4.7′
461308 = [version]’4.7.1′
461310 = [version]’4.7.1′
461808 = [version]’4.7.2′
461814 = [version]’4.7.2′
528040 = [version]’4.8′
528049 = [version]’4.8′
}

$computerOS = Get-WmiObject -Class Win32_OperatingSystem | Select-Object CSName,Caption
$OS = $computerOS.Caption
$ComputerN = $computerOS.CSName
$info = Get-ChildItem ‘HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP’ -Recurse -ErrorAction SilentlyContinue |
Get-ItemProperty -name Version, Release -EA 0 |
Where-Object { $_.PSChildName -eq “Full”} | Select-Object PSComputerName, Version, Release
$netversion = $info.Version
$netrelease = $info.Release
$com = $info.PSComputerName
$PSver = $PSVersionTable.PSVersion.ToString()

New-Object -TypeName PSObject -Property @{
“ComputerName” = $ComputerN
“OperatingSystem” = $OS
“DotNetFramework” = $netversion
“DotNetRelease” = $netrelease
“PowerShellVersion” = $PSver
} | Select-Object ComputerName, OperatingSystem, DotNetFramework, DotNetRelease, PowerShellVersion | FT
}
}
}