Report VM with Powershell

หลังจากที่วุ่นวายกับการทำ Backup กันไปแล้ว ก็มาถึงความวุ่นวายของการทำ Report ซึ่งเราจะ Export ออกมาเป็น CSV โดยใช้ Powershell ซึ่งเดี๋ยวเราจะนำไปต่อกับ Power BI เพื่อแสดงแบบ Data Visualization สำหรับผู้บริหาร


Design Report

สิ่งที่เราต้องการก็คือ รายงานของ VM ทั้งหมด ซึ่งจะบอกถึงรายละเอียดตั้งแต่ Name, Status ( PowerOn or PowerOff ), CPU, Memory, HDD, Datastore, Resource Pool, IP Address, OS และ Used Space ( Thin or Thick )

Directory Structure

เราจะใช้ Directory Structure เดิมที่เคยสร้างไว้ โดยจะประกอบไปด้วยโครงสร้างดังนี้

C:\powershell-script
├───Credential/
│   └───vcsa.lab.local.clixml
├───VM-Report/
│   └───VM/
└───VM-Script/
    ├───Configuration.ps1
    └───Report-VM.ps1

Get Started

  • ทำการเพิ่ม Global Variable ขึ้นในไฟล์ Configuration.ps1
# VM-Report
$Global:report_vm = $root + 'VM-Report\VM\Report-VM-' + $date + '.csv'

# PowerBI
$Global:powerbi_vm = $root + 'PowerBI\Dataset\Dataset-VM.csv'
  • ทำการสร้างไฟล์ Report-VM.ps1
. "$PSScriptRoot\Configuration.ps1"
Connect-VIServer -Server $server -Credential $credential

$report = $report_vm
$powrebi = $powerbi_vm

$query = Get-VM -Name '*' | Sort-Object Name | Select Name, PowerState, NumCpu, MemoryMB,
	@{N="UsedSpaceGB"; E={$_.UsedSpaceGB -as [int]}},
	@{N="HardDiskSizeGB"; E={((Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum) -as [int]}},
	@{N="ResourcePool";E={[string]::Join(',',(Get-ResourcePool -Id $_.ResourcePoolId | Select -ExpandProperty Name))}},
	@{N="IPaddress";E={$_.Guest.IpAddress[0]}},
 	@{N="GuestOS";E={$_.Guest.OSFullName}},
	@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}}

$query | Export-Csv -Path $report -NoTypeInformation
$query | Export-Csv -Path $powrebi -NoTypeInformation

Disconnect-VIServer -Server $server -Confirm:$false

อ่านเพิ่มเติม : https://bit.ly/2TC5Isg

Leave a Reply

Your email address will not be published. Required fields are marked *