I'm new to this whole PS thingy, but have come up with a few exmples that may help others along. Please feel free to pick faut, make suggestions, and please talk to me like a 10 year old so I understand 🐵
TG
Purpose : List all Disks that exist in a specific datastore by VM
#Main
$strVC = ""
Connect-VIServer $strVC
$objUsedDisks = Get-VM | Get-HardDisk | where-object {$_.filename -like "SAN_vmdata"}
write-host "----
"
Foreach ($strDisk in $objUsedDisks )
{
write-host $Strdisk.filename " " $strdisk.capacityKB " " $strdisk.name
}
Disconnect-VIServer -Confirm:$False
Purpose : List Additional Disks (non OS) on all Datastores
#Main
$strVC = ""
Connect-VIServer $strVC
create an array holding all the datastore objects.
$arrDS = Get-Datastore
#iterate through the avaiable datastores looking for files.
Foreach ($strDatastore in $arrDS)
{
#Create a string with the Datastore Name and print it out.
$strDatastoreName = $strDatastore.name
Write-Host $strDatastoreName
create a Datastore 'View' object for the DS beging queried.
$ds = Get-Datastore -Name $strDatastoreName | %{Get-View $_.Id}
#Create a fileQuery object to define what sort of query is required.
$fileQueryFlags = New-Object VMware.Vim.FileQueryFlags
$fileQueryFlags.FileSize = $true
$fileQueryFlags.FileType = $true
$fileQueryFlags.Modification = $true
create a new search object to browse the datastore, pass it the query flag and the datastore view.
$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.details = $fileQueryFlags
$searchSpec.sortFoldersFirst = $true
$dsBrowser = Get-View $ds.browser
$rootPath = "[http://"$ds.summary.Name"|http://"$ds.summary.Name"]"
read the results from the query into a search object
$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
#$myCol = @()
from the search object - itterate through the folders
foreach ($folder in $searchResult)
{
for each folder - itterate through the files.
foreach ($fileResult in $folder.File)
{
#create an object called file with Name and Full Path properties
$file = "" | select Name, FullPath
#Populate the Name and Full Path propeties with vaules from the files.
$file.Name = $fileResult.Path
$file.fullPath = $folder.folderpath
if the file Name property has .vmdk in it - then it's a disk so....
IF ($file.name -like "*_?.vmdk")
{
combine the Name and Full Path properties into a string and print.
$combinedpath = $file.fullpath + $file.name
write-host "Combined Path = : $combinedpath"
}
}
}
}
Disconnect-VIServer -Confirm:$False