Orphan Vms

 

function LoadSnapin{
param($PSSnapinName)
if (!(Get-PSSnapin | where {$_.Name -eq $PSSnapinName})){
Add-pssnapin -name $PSSnapinName
}
}
# Load PowerCLI snapin
LoadSnapin -PSSnapinName "VMware.VimAutomation.Core"
# Variables
[string] $vCenter = "vcenter.domain.local" # vCenter FQDN
# Connect to vCenter
Connect-VIServer -Server $vCenter
$report = @()
$arrUsedDisks = Get-View -ViewType VirtualMachine | % {$_.Layout} | % {$_.Disk} | % {$_.DiskFile}
$arrDS = Get-Datastore | Sort-Object -property Name
foreach ($strDatastore in $arrDS) {
Write-Host "Checking" $strDatastore.Name "..."
$ds = Get-Datastore -Name $strDatastore.Name | % {Get-View $_.Id}
$fileQueryFlags = New-Object VMware.Vim.FileQueryFlags
$fileQueryFlags.FileSize = $true
$fileQueryFlags.FileType = $true
$fileQueryFlags.Modification = $true
$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.details = $fileQueryFlags
$searchSpec.matchPattern = "*.vmdk"
$searchSpec.sortFoldersFirst = $true
$dsBrowser = Get-View $ds.browser
$rootPath = "[" + $ds.Name + "]"
$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
foreach ($folder in $searchResult)
{
foreach ($fileResult in $folder.File)
{
if ($fileResult.Path)
{
if (-not ($fileResult.Path.contains("ctk.vmdk"))) #Remove Change Tracking Files
{
if (-not ($arrUsedDisks -contains ($folder.FolderPath.trim('/') + '/' + $fileResult.Path)))
{
$row = "" | Select DS, Path, File, Size, ModDate
$row.DS = $strDatastore.Name
$row.Path = $folder.FolderPath
$row.File = $fileResult.Path
$row.Size = $fileResult.FileSize
$row.ModDate = $fileResult.Modification
$report += $row
}
}
}
}
}
}
# Print report to console
$report

Delete Snaps older than 7 days - Power CLI

Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-7)} | Remove-Snapshot -confirm:$false -RemoveChildren -RunAsync


Delete Snaps by Excluding Clusters:

get-cluster | ?{$_.Name -notmatch "Backup01|Backup02"} | Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-10)} | Remove-Snapshot -confirm:$false -RemoveChildren -RunAsync


List VM's which got rebooted when HA event happened - Power CLI

 

Get-VIEvent -MaxSamples 100000 -Start (Get-Date).AddDays(-1) -Type Warning | Where {$_.FullFormattedMessage -match "restarted"} |select CreatedTime,FullFormattedMessage | sort CreatedTime –ascending

PowerShell to mount NFS datastore to VMware Cluster - Power CLI

 ## This script is intended to request input and mount a datastore for a specific cluster

## Script Developed by Uday Yedluri

## From this line below use everything part of your .ps1 file before execution

## Listed are Input Values like vCenter, Details and creds you should provide post script execution on PS screen


$VCServer = Vcenter1, Vcenter2

$Cluster = Read-Host -Prompt 'VMware Cluster Name'

$NameofDS = Read-Host -Prompt 'Please provide datastore name ---Example: NFSName'

$dspath = Read-host -Prompt 'Please provide datastore path ---- Example: /NFSpath '

$nfshostip = Read-Host -Prompt 'Please provide NFS Host IP --- Example: 10.10.10.5'

$cred = Get-Credential 

Connect-VIServer -Server $VCServer -force -credential $cred -Verbose

$nfsvmhosts = get-cluster $Cluster| Get-VMhost

foreach($nfsvmhost in $nfsvmhosts)

{

   get-vmhost $nfsvmhost | New-Datastore -Nfs -Name $NameofDS -Path $dspath  -NfsHost $nfshostip

 }