62 lines
2.6 KiB
PowerShell
62 lines
2.6 KiB
PowerShell
<# Scheduled Task metadata
|
|
General
|
|
Old-VMware Billing
|
|
run as ndgov\!itdvcenterscript (required for SQL Database access)
|
|
run whether user is logged on or not
|
|
Triggers
|
|
Daily, 5am
|
|
Actions
|
|
old-C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -file "C:\itdscript\vmconfig.ps1"
|
|
new-"C:\Program Files\PowerShell\7\pwsh.exe" -noninteractive -file "F:\SyncVMwareVMsToSql\SyncVMwareVMsToSql.ps1"
|
|
"C:\Program Files\PowerShell\7\pwsh.exe" -noninteractive -file "F:\SyncVMwareVMsToSql\SyncVMwareVMsToSql.ps1"
|
|
|
|
Settings
|
|
allow task to be run on demand
|
|
stop the task if it runs longer than 1 hour -eq $true
|
|
if the running task does not end when requested, force it to stop
|
|
|
|
SQL Query to check for most recent 2500 records
|
|
SELECT TOP (2500) [ServerName]
|
|
,[SnapshotDate]
|
|
,[VMName]
|
|
,[Memory_MB]
|
|
,[Num_VCPU]
|
|
,[Disk_MB]
|
|
,[ESXHostName]
|
|
FROM [ITD-SRS-Billing].[dbo].[VMWare_VCenter_VMs]
|
|
ORDER BY SnapshotDate DESC, VMName ASC
|
|
|
|
#>
|
|
$Secret:!itdvcenterscript
|
|
$TimeStamp = Get-Date -UFormat "%Y%m%d%H%M%S"
|
|
Start-Transcript F:\SyncVMwareVMsToSql\Logs\SyncVMwareVMsToSql-$Timestamp.log
|
|
|
|
Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Scope Session -Confirm:$false
|
|
Connect-ITDvCenter
|
|
|
|
$Datacenters = Get-Datacenter | Where-Object {$_.Name -notlike "*Normandy*" -and $_.Name -notlike "*Vantis*"}
|
|
$VMs = $Datacenters | Get-VM | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" -and $_.Name -notlike "itdzmtest*"} | Select Name, NumCPU, @{label="MemoryMB"; expression={$_.MemoryGB * 1024}}, @{label="HardDiskSizeGB"; expression={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum * 1024}}, VMHost | Sort-Object Name
|
|
|
|
|
|
$SqlServer = "itdsql22p1.nd.gov\SQL22P1"
|
|
$Database = "ITD-SRS-Billing"
|
|
$Date = "'" + (Get-Date).ToString('yyyy/MM/dd') + "'"
|
|
|
|
# remove today's entries if already there
|
|
Write-Verbose -Message "remove today's entries if already there"
|
|
$SqlQuery = "delete from [VMware_VCenter_VMs] where snapshotdate = $Date;"
|
|
Invoke-SQLCmd -ServerInstance $SqlServer -Database $Database -Query $SqlQuery
|
|
|
|
foreach($VM in $VMs) {
|
|
Write-Verbose -Message ("Begin " + $VM.Name)
|
|
$VMName = "'" + $VM.Name + "'"
|
|
$VMMemoryMB = $VM.MemoryMB
|
|
$VMNumCPU = $VM.NumCPU
|
|
$VMHardDiskSizeGB = $VM.HardDiskSizeGB
|
|
$VMHost = "'" + $VM.VMHost + "'"
|
|
$SqlQuery ="INSERT INTO [VMware_VCenter_VMs] (ServerName, SnapshotDate, VMName, Memory_MB, Num_VCPU, Disk_MB, ESXHostName) Values ('None', $Date, $VMName, $VMMemoryMB, $VMNumCPU, $VMHardDiskSizeGB, $VMHost);"
|
|
Invoke-SQLCmd -ServerInstance $SqlServer -Database $Database -Query $SqlQuery
|
|
|
|
}
|
|
|
|
Stop-Transcript |