update
This commit is contained in:
+1
@@ -0,0 +1 @@
|
||||
# It all starts with a single line of powershell code.
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
A short one-line action-based description, e.g. 'Tests if a function is valid'
|
||||
.DESCRIPTION
|
||||
A longer description of the function, its purpose, common use cases, etc.
|
||||
.NOTES
|
||||
Information or caveats about the function e.g. 'This function is not supported in Linux'
|
||||
.LINK
|
||||
Specify a URI to a help page, this will show when Get-Help -Online is used.
|
||||
.EXAMPLE
|
||||
Test-MyTestFunction -Verbose
|
||||
Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
|
||||
#>
|
||||
|
||||
|
||||
|
||||
<# Scheduled Task metadata
|
||||
General
|
||||
Get IPs for PA
|
||||
run as ndgov\!itdvcenterppa
|
||||
run whether user is logged on or not
|
||||
Triggers
|
||||
Daily, 11am
|
||||
Daily, 11pm
|
||||
Actions
|
||||
old-C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -file "C:\itdscript\GetIPs.ps1"
|
||||
new-"C:\Program Files\PowerShell\7\pwsh.exe" -noninteractive -file "F:\GetVMwareVMGuestIPsForPA\GetVMwareVMGuestIPsForPA.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
|
||||
#>
|
||||
|
||||
$TimeStamp = Get-Date -UFormat "%Y%m%d%H%M%S"
|
||||
|
||||
#Connect
|
||||
Connect-ITDvCenter -Credential $Secret:ndgov_svcitdvmvcauto
|
||||
|
||||
|
||||
##Windows
|
||||
|
||||
|
||||
#Output File
|
||||
$OutFileWin = "c:\inetpub\wwwroot\Win.txt"
|
||||
#$Date = Get-Date -UFormat "%Y%m%d%H%M%S"
|
||||
Get-Item -Path $OutFileWin | Copy-Item -Destination "F:\GetVMwareVMGuestIPsForPA\Backup\Win\$Timestamp-Win.txt"
|
||||
Remove-Item $OutFileWin
|
||||
Start-Sleep -Seconds 5
|
||||
|
||||
#Get Powered On VM's
|
||||
$vmwin = get-VM | Where-Object { $_.PowerState -eq "PoweredOn" `
|
||||
-and ($_.GuestID -eq "windows7Guest" `
|
||||
-or $_.GuestID -eq "windows7_64Guest" `
|
||||
-or $_.GuestID -eq "windows7Server64Guest" `
|
||||
-or $_.GuestID -eq "windows8_64Guest" `
|
||||
-or $_.GuestID -eq "windows8Server64Guest" `
|
||||
-or $_.GuestID -eq "windows9Server64Guest" `
|
||||
-or $_.GuestID -eq "winLonghorn64Guest" `
|
||||
-or $_.GuestID -eq "winLonghornGuest" `
|
||||
-or $_.GuestID -eq "winNetStandardGuest" `
|
||||
-or $_.GuestID -eq "winNetEnterpriseGuest" `
|
||||
-or $_.GuestID -eq "windows9_64Guest" `
|
||||
-or $_.GuestID -eq "windows2019srv_64Guest" `
|
||||
-or $_.GuestID -eq "windows2019srvNext_64Guest") }
|
||||
$vmviewwin = $vmwin | Get-View
|
||||
|
||||
$Outputwin = ""
|
||||
|
||||
#Loop through VM's, NIC's, and IP addresses.
|
||||
Foreach ($v in $vmviewwin) {
|
||||
Foreach ($nic in $v.Guest.Net) {
|
||||
Foreach ($IP in $nic.IPAddress) {
|
||||
If ($IP -notlike "fe80*" -and $IP -notlike "192.168.*" -and $IP -notlike "172.16*") {
|
||||
$OutputWin += $IP + "`n"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#If ($Outputwin -ne "") {$OutputWin | Out-File $OutFileWin -Encoding utf8 -NoNewline}
|
||||
If ($Outputwin -ne "") { $OutputWin | Out-File $OutFileWin -Encoding ASCII -NoNewline }
|
||||
|
||||
|
||||
|
||||
##Linux
|
||||
|
||||
#Output File
|
||||
$OutFileLin = "c:\inetpub\wwwroot\Lin.txt"
|
||||
$Date = Get-Date -UFormat "%Y%m%d%H%M%S"
|
||||
Get-Item -Path $OutFileLin | Copy-Item -Destination "F:\GetVMwareVMGuestIPsForPA\Backup\Lin\$Timestamp-Lin.txt"
|
||||
Remove-Item $OutFileLin
|
||||
Start-Sleep -Seconds 5
|
||||
|
||||
#Get Powered On VM's
|
||||
$vmLin = get-VM | Where-Object { $_.PowerState -eq "PoweredOn" `
|
||||
-and ($_.GuestID -eq "centos6_64Guest" `
|
||||
-or $_.GuestID -eq "centos64Guest" `
|
||||
-or $_.GuestID -eq "centos7_64Guest" `
|
||||
-or $_.GuestID -eq "oracleLinux64Guest" `
|
||||
-or $_.GuestID -eq "oracleLinux7_64Guest" `
|
||||
-or $_.GuestID -eq "rhel4Guest" `
|
||||
-or $_.GuestID -eq "rhel5Guest" `
|
||||
-or $_.GuestID -eq "rhel5_64Guest" `
|
||||
-or $_.GuestID -eq "rhel6Guest" `
|
||||
-or $_.GuestID -eq "rhel6_64Guest" `
|
||||
-or $_.GuestID -eq "rhel7_64Guest" `
|
||||
-or $_.GuestID -eq "rhel8_64Guest" `
|
||||
-or $_.GuestID -eq "rhel9_64Guest" `
|
||||
-or $_.GuestID -eq "sles11_64Guest" `
|
||||
-or $_.GuestID -eq "sles12_64Guest" `
|
||||
-or $_.GuestID -eq "ubuntu64Guest") }
|
||||
$vmviewlin = $vmLin | Get-View
|
||||
|
||||
$OutputLin = ""
|
||||
|
||||
#Loop through VM's, NIC's, and IP addresses.
|
||||
Foreach ($v in $vmviewlin) {
|
||||
Foreach ($nic in $v.Guest.Net) {
|
||||
Foreach ($IP in $nic.IPAddress) {
|
||||
If ($IP -notlike "fe80*" -and $IP -notlike "192.168.*" -and $IP -notlike "172.16*") {
|
||||
$OutputLin += $IP + "`n"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#If ($OutputLin -ne "") {$OutputLin | Out-File $OutFileLin -Encoding utf8 -NoNewline}
|
||||
If ($OutputLin -ne "") { $OutputLin | Out-File $OutFileLin -Encoding ASCII -NoNewline }
|
||||
|
||||
Disconnect-ITDvCenter
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<# This is how VMware data is sent to Billing
|
||||
|
||||
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"
|
||||
newV3 - PSUniversal script and schedule
|
||||
|
||||
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
|
||||
|
||||
#>
|
||||
|
||||
$TimeStamp = Get-Date -UFormat "%Y%m%d%H%M%S"
|
||||
#Start-Transcript F:\SyncVMwareVMsToSql\Logs\SyncVMwareVMsToSql-$Timestamp.log
|
||||
|
||||
Write-Verbose -Message "Connect to vCenter" -Verbose
|
||||
Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Scope Session -Confirm:$false
|
||||
Connect-ITDvCenter -Credential $Secret:ndgov_svcitdvmvcauto
|
||||
|
||||
Write-Verbose -Message "Get current virtual machines, minus filters"
|
||||
$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
|
||||
|
||||
Write-Verbose "Prep SQL connection" -Verbose
|
||||
$SqlServer = "itdsql22p1.nd.gov\SQL22P1"
|
||||
$Database = "ITD-SRS-Billing"
|
||||
$Date = "'" + (Get-Date).ToString('yyyy/MM/dd') + "'"
|
||||
|
||||
Write-Verbose -Message "Remove today's entries from SQL if already there"
|
||||
$SqlQuery = "delete from [VMware_VCenter_VMs] where snapshotdate = $Date;"
|
||||
Invoke-SQLCmd -ServerInstance $SqlServer -Database $Database -Query $SqlQuery
|
||||
|
||||
Write-Verbose -Message "Start loop to create new record for each VM found" -Verbose
|
||||
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
|
||||
Disconnect-ITDvCenter
|
||||
Reference in New Issue
Block a user