Hi Poshoholic,
Here is my first script:
Although I intend to connect to vCenter using a master script. The master script will also connect to Database, retrieve vCenter login details (there can be multiple vcenters) i.e IP/hostname, user ID and password. Then run through the table which will have a list of powercli scripts and iteratively connect to each vcenter server and run all the scripts.
# load the VMware module
$VimAutoCore = “VMware.VimAutomation.Core”
if ( (Get-PSSnapin -Name $VimAutoCore -ErrorAction SilentlyContinue) -eq $null ) {
Write-Host “loading $VimAutoCore powershell module”
Add-PsSnapin $VimAutoCore
}
#This variable connects to SQL Server Database
$SQLConnectionString = “Server=hostname;Database=dbname;User Id=sa;Password=password;”
#Variable to retrieve vCenter IP, Username and password
$vCenterIP = Get-MOLDatabaseData -connectionString $SQLConnectionString -isSQLServer -query “SELECT vCenterIP FROM server_table”
$vCenterUser = Get-MOLDatabaseData -connectionString $SQLConnectionString -isSQLServer -query “SELECT UserID FROM server_table”
$vCenterPassword = Get-MOLDatabaseData -connectionString $SQLConnectionString -isSQLServer -query “SELECT Password FROM server_table”
#Connect to vCenter Server
Connect-VIServer -Server $vCenterIP -User $vCenterUser -Password $vCenterPassword -ErrorAction Stop -Protocol https
# List any Snapshot older than $Age days and bigger than $SnapSize MB.
function Get-ActiveSnapshot {
BEGIN{}
PROCESS{
$Age = 1
$Snapshot = Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-$Age)}
foreach ($sn in $Snapshot){
$created = $sn.Created.ToLocalTime()
$size = “{0:N2}” -f $sn.SizeGB
$props =@{
‘Virtual Machine’=$sn.VM;
‘Category’ = “Snapshot”;
‘Cluster’ = $sn.VM.Host.Parent.Name;
‘Results’ = “This Snapshot was created on $Created which is $size GB in Size”}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
}
}
END{}
}
Get-ActiveSnapshot
Although the above code doesn’t connect to vcenter server, but it can retrieve information from SQL table though.