Audit TimeStamp for VM Backup with Powershell

โดยปกติการตรวจสอบ DB Backup เมื่อตรวจสอบเสร็จเรียบร้อยจะทำการบันทึก TimeStamp ซึ่งพอเป็น VM Backup โดยใช้ Powershell บน Windows ตอนแรกทำเป็น Excel แล้วมันยุ่งยาก เลยเปลี่ยนมาเป็นใช้ Powershell ทำการบันทึก พร้อมทั้งระบุ Cause ไปด้วยเลย


Backup Policy

เราจะแบ่งการ Audit เป็น 2 แบบ คือ แบบแรกใช้ Task Schedule ในการนับไฟล์ VM Backup ว่าครบตาม Retention หรือไม่ แบบที่สองใช้ Human มาตรวจสอบอีกครั้งหนึ่ง จากไฟล์ที่ได้จากการ Audit ด้วย Task Schedule เมื่อตรวจสอบครบแล้วจึงค่อยทำการบันทึก TimeStamp และหากเกิดปัญหาก็ให้พิมพ์ Note ไปได้เลย

Directory Structure

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

C:\powershell-script
├───Credential/
│   └───vcsa.lab.local.clixml
├───VM-Audit/
│   ├───Daily/
│   ├───Monthly/
│   ├───Weekly/
│   └───CCC.txt
├───VM-Check/
│   ├───Daily/
│   ├───Monthly/
│   └───Weekly/
└───VM-Script/
    ├───Audit-Daily.ps1
    ├───Audit-Monthly.ps1
    ├───Audit-Weekly.ps1
    ├───Configuration.ps1
    ├───Check-Daily.ps1
    ├───Check-Monthly.ps1
    ├───Check-Weekly.ps1
    └───TimeStamp.ps1

Get Started

  • ทำการเพิ่ม Global Variable เพื่อระบุ Path ที่เก็บ Log ในไฟล์ Configuration.ps1
# VM-Check
$Global:path_check_daily = $root + 'VM-Check\Daily\Check-Daily-' + $date + '.txt'
$Global:path_check_weekly = $root + 'VM-Check\Weekly\Check-Weekly-' + $date + '.txt'
$Global:path_check_monthly = $root + 'VM-Check\Monthly\Check-Monthly-' + $date + '.txt'

# VM-Audit
$Global:path_audit_daily = $root + 'VM-Audit\Daily\Audit-Daily-' + $date + '.txt'
$Global:path_audit_weekly = $root + 'VM-Audit\Weekly\Audit-Weekly-' + $date + '.txt'
$Global:path_audit_monthly = $root + 'VM-Audit\Monthly\Audit-Monthly-' + $date + '.txt'

# TimeStamp
$Global:path_audit_ccc = $root + 'VM-Audit\CCC.txt'
  • ทำการสร้างไฟล์ Check-Daily.ps1
. "$PSScriptRoot\Configuration.ps1"
Connect-VIServer -Server $server -Credential $credential

$vm = $vm_daily
$path = $path_check_daily
$condition = $condition_daily

foreach ($line in $vm) {

	#$query = Get-VM -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$query = Get-Template -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$count = $line + ' = ' + @($query).Count
	$query | Out-File $path -Append
	$count | Out-File $path -Append
		
}

Disconnect-VIServer -Server $server -Confirm:$false
  • ทำการสร้างไฟล์ Check-Monthly.ps1
. "$PSScriptRoot\Configuration.ps1"
Connect-VIServer -Server $server -Credential $credential

$vm = $vm_monthly
$path = $path_check_monthly
$condition = $condition_monthly

foreach ($line in $vm) {

	#$query = Get-VM -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$query = Get-Template -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$count = $line + ' = ' + @($query).Count
	$query | Out-File $path -Append
	$count | Out-File $path -Append
		
}

Disconnect-VIServer -Server $server -Confirm:$false
  • ทำการสร้างไฟล์ Check-Weekly.ps1
. "$PSScriptRoot\Configuration.ps1"
Connect-VIServer -Server $server -Credential $credential

$vm = $vm_weekly
$path = $path_check_weekly
$condition = $condition_weekly

foreach ($line in $vm) {

	#$query = Get-VM -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$query = Get-Template -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$count = $line + ' = ' + @($query).Count
	$query | Out-File $path -Append
	$count | Out-File $path -Append
		
}

Disconnect-VIServer -Server $server -Confirm:$false
  • ทำการสร้างไฟล์ Audit-Daily.ps1
. "$PSScriptRoot\Configuration.ps1"
Connect-VIServer -Server $server -Credential $credential

$vm = $vm_daily
$path = $path_audit_daily
$condition = $condition_daily

foreach ($line in $vm) {

	#$query = Get-VM -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$query = Get-Template -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$count = $line + ' = ' + @($query).Count
	$query | Out-File $Path -Append
	$count | Out-File $Path -Append
		
}

Disconnect-VIServer -Server $server -Confirm:$false
  • ทำการสร้างไฟล์ Audit-Monthly.ps1
. "$PSScriptRoot\Configuration.ps1"
Connect-VIServer -Server $server -Credential $credential

$vm = $vm_monthly
$path = $path_audit_monthly
$condition = $condition_monthly

foreach ($line in $vm) {

	#$query = Get-VM -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$query = Get-Template -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$count = $line + ' = ' + @($query).Count
	$query | Out-File $Path -Append
	$count | Out-File $Path -Append
		
}

Disconnect-VIServer -Server $server -Confirm:$false
  • ทำการสร้างไฟล์ Audit-Weekly.ps1
. "$PSScriptRoot\Configuration.ps1"
Connect-VIServer -Server $server -Credential $credential

$vm = $vm_weekly
$path = $path_audit_weekly
$condition = $condition_weekly

foreach ($line in $vm) {

	#$query = Get-VM -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$query = Get-Template -Name '*' | Where-Object { $_.Name -like $condition -and $_.name -like ('*' + $line) } | Sort-Object -Property Name | Select-Object Name
	$count = $line + ' = ' + @($query).Count
	$query | Out-File $Path -Append
	$count | Out-File $Path -Append
		
}

Disconnect-VIServer -Server $server -Confirm:$false
  • ทำการสร้างไฟล์ TimeStamp.ps1
. "$PSScriptRoot\Configuration.ps1"
Connect-VIServer -Server $server -Credential $credential

$note = Read-Host 'Note'

if (!$note -eq '') {

	$timestamp = $date_audit + ' -- ' + $note

} else {

	$timestamp = $date_audit + ' -- CHECK SCRIPT BACKUP OK'

}

$timestamp | Out-File $path_audit_ccc -Append
Disconnect-VIServer -Server $server -Confirm:$false

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

Leave a Reply

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