The vcserver is on the production network and the script work on there OK but does not get and of the server in the DMZ.
I can not run it from the DMZ as does not have access to the vcserver.
Is the only way to open a port?
#Get VMware Disk Usage
Created by Hugo Peeters
VARIABLES
$Decimals = 1
$VCServer = “xxxxxxxx”
SCRIPT
Connect to VC
Write-Progress “Gathering Information” “Connecting to Virtual Center” -Id 0
$VC = Connect-VIServer $VCServer
Create Output Collection
$myCol = @()
List Datastores (Datastore Name)
Write-Progress “Gathering Information” “Listing Datastores” -Id 0
$Datastores = Get-Datastore | Sort Name
List vms
Write-Progress “Gathering Information” “Listing VMs and Disk Files” -Id 0
$VMSummaries = @()
ForEach ($vm in (Get-VM))
{
$VMView = $VM | Get-View
ForEach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | Where {$_.DeviceInfo.Label -match “SCSI Controller”}))
{
ForEach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | Where {$_.ControllerKey -eq $VirtualSCSIController.Key}))
{
$VMSummary = “” | Select VM, HostName, PowerState, DiskFile, DiskName, DiskSize, SCSIController, SCSITarget
$VMSummary.VM = $VM.Name
$VMSummary.HostName = $VMView.Guest.HostName
$VMSummary.PowerState = $VM.PowerState
$VMSummary.DiskFile = $VirtualDiskDevice.Backing.FileName
$VMSummary.DiskName = $VirtualDiskDevice.DeviceInfo.Label
$VMSummary.DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB
$VMSummary.SCSIController = $VirtualSCSIController.BusNumber
$VMSummary.SCSITarget = $VirtualDiskDevice.UnitNumber
$VMSummaries += $VMSummary
}
}
Clear-Variable VMView -ErrorAction SilentlyContinue
}
Loop through Datastores
ForEach ($Datastore in $Datastores)
{
List vmdk files in datastore (vmdk Name)
Write-Progress “Gathering Information” (”Processing Datastore {0}” -f $Datastore.Name) -Id 0
$DSView = $Datastore | Get-View
$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.sortFoldersFirst = $true
$dsBrowser = Get-View $DSView.browser
$rootPath = ““
$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
ForEach ($result in $searchResult)
{
ForEach ($vmdk in ($result.File | ?{$_.Path -like “*.vmdk”} | Sort Path))
{
Write-Progress “Gathering Information” (”Processing VMDK {0}” -f $vmdk.Path) -Id 1
Write-Host “==============================================================================”
Find vm using the vmdk (VM Name)
$VMRef = ($VMSummaries | ?{$_.DiskFile -match $Datastore.Name -and $_.DiskFile -match $vmdk.Path})
“VMDK belongs to VM
” -f $vmdk.Path, $VMRef.VM
If ($VMRef.Powerstate -eq “PoweredOn”)
{
Write-Host “VM is powered on” -ForegroundColor “yellow”
$Partitions = Get-WmiObject -Class Win32_DiskPartition -ComputerName $VMRef.HostName
If ($?)
{
$Disks = Get-WmiObject -Class Win32_DiskDrive -ComputerName $VMRef.HostName
$LogicalDisks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $VMRef.HostName
$DiskToPartition = Get-WmiObject -Class Win32_DiskDriveToDiskPartition -ComputerName $VMRef.HostName
$LogicalDiskToPartition = Get-WmiObject -Class Win32_LogicalDiskToPartition -ComputerName $VMRef.HostName
Write-Host “Read partition and disk information” -ForegroundColor “yellow”
$DiskMatch = $Disks | ?{($_.SCSIPort - 1) -eq $VMRef.SCSIController -and $_.SCSITargetID -eq $VMRef.SCSITarget}
If ($DiskMatch -eq $null){Write-Warning “NO MATCHES!”}
Else
{
Write-Host “Found match:” -ForegroundColor “yellow”
$DiskMatch
$PartitionsOnDisk = ($DiskToPartition | ?{$_.Antecedent -eq $DiskMatch.__PATH})
If ($PartitionsOnDisk -eq $null){Write-Warning “NO PARTITIONS!”}
Else
{
ForEach ($PartitionOnDisk in $PartitionsOnDisk)
{
Write-Host “Disk contains partition” -ForegroundColor “yellow”
$PartitionOnDisk.Dependent
$PartitionMatches = $Partitions | ?{$_.__PATH -eq $PartitionOnDisk.Dependent}
ForEach ($PartitionMatch in $PartitionMatches)
{
$LogicalDiskRefs = $LogicalDiskToPartition | ?{$_.Antecedent -eq $PartitionMatch.__PATH}
If ($LogicalDiskRefs -eq $null)
{
Write-Warning “NO LOGICAL DISKS!”
}
Else
{
ForEach ($LogicalDiskRef in $LogicalDiskRefs)
{
$LogicalDiskMatches = $LogicalDisks | ?{$_.__PATH -eq $LogicalDiskRef.Dependent}
ForEach ($LogicalDiskMatch in $LogicalDiskMatches)
{
Write-Host “Matching Logical Disk:” -ForegroundColor “yellow”
$LogicalDiskMatch
$myObj = “” | Select Datastore, DSSizeGB, DSFreeGB, DSPercentFree, DiskFile, VM, HardDisk, DriveLetter, DiskSizeGB, DiskFreeGB, PercFree
$myObj.Datastore = $Datastore.Name
$myObj.DSSizeGB =
::Round(($Datastore.CapacityMB * 1MB / 1GB),$Decimals)
$myObj.DSFreeGB = ::Round(($Datastore.FreeSpaceMB * 1MB / 1GB),$Decimals)
$myObj.DSPercentFree = ::Round((100*($Datastore.FreeSpaceMB/$Datastore.CapacityMB)),$Decimals)
$myObj.DiskFile = $vmdk.Path
$myObj.VM = $VMRef.VM
$myObj.HardDisk = $VMRef.DiskName
$myObj.DriveLetter = $LogicalDiskMatch.DeviceID
$myObj.DiskSizeGB = ::Round(($LogicalDiskMatch.Size / 1GB),$Decimals)
$myObj.DiskFreeGB = ::Round(($LogicalDiskMatch.FreeSpace / 1GB),$Decimals)
$myObj.PercFree = ::Round((100 * (($LogicalDiskMatch.FreeSpace / 1MB) / ($LogicalDiskMatch.Size / 1MB))),$Decimals)
Write-Host “RESULT:” -ForegroundColor “yellow”
$myObj
$myCol += $myObj
}
Clear-Variable LogicalDiskMatches -ErrorAction SilentlyContinue
}
}
Clear-Variable LogicalDiskRefs -ErrorAction SilentlyContinue
}
Clear-Variable PartitionMatches -ErrorAction SilentlyContinue
}
}
Clear-Variable PartitionsOnDisk -ErrorAction SilentlyContinue
}
Clear-Variable DiskMatch -ErrorAction SilentlyContinue
Clear-Variable Disks -ErrorAction SilentlyContinue
Clear-Variable LogicalDisks -ErrorAction SilentlyContinue
Clear-Variable DiskToPartition -ErrorAction SilentlyContinue
Clear-Variable LogicalDiskToPartition -ErrorAction SilentlyContinue
}
Clear-Variable Partitions -ErrorAction SilentlyContinue
}
Else
{
Write-Host “VM is powered off” -ForegroundColor “yellow”
}
Clear-Variable VMRef -ErrorAction SilentlyContinue
Write-Progress “Gathering Information” (”Processing VMDK ” -f $vmdk.Path) -Id 1 -Completed
}
}
}
Disconnect from VC
Disconnect-VIServer -Confirm:$False
OUTPUT
Write-Host “===================================================”
Write-Host “===================================================”
$TotalDSFree = ($myCol | Select Datastore, DSFreeGB -Unique | Measure-Object DSFreeGB -Sum).Sum
$TotalDSSize = ($myCol | Select Datastore, DSSizeGB -Unique | Measure-Object DSSizeGB -Sum).Sum
$AverageDSFree = ::Round(100 * ($TotalDSFree / $TotalDSSize),$Decimals)
$AverageDiskFree = ::Round(100 * (($myCol | Measure-Object DiskFreeGB -Sum).Sum / ($myCol | Measure-Object DiskSizeGB -Sum).Sum),$Decimals)
Write-Host “Total DS Free: $TotalDSFree”
Write-Host “Total DS Size: $TotalDSSize”
Write-Host “Average DS Free Percentage: $AverageDSFree”
Write-Host “Average Disk Free Percentage: $AverageDiskFree”
$myCol | Export-Csv -NoTypeInformation ‘C:\scipts\VMwareDiskUsage.csv’
Yes, you will need the correct ports open to run this script from your machine on an internal network or Powershell and the VI toolkit on a machine in the DMZ that can access your host.
If you found this information useful, please consider awarding points for Correct or Helpful.
Alan Renouf
What port are they?
Unless you changed the ports in your VC that should be HTTP (80) and HTTPS (443).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Is 80 needed ?
Is there a better way to do this?
What ports form internal to dmz? ( as I do not want my password in the DMZ and the ports open)
I don't think port 80 is required but I'm not 100% sure.
Perhaps Carter or one of the developers can confirm this ?
You could eventually specify "httpsOnly" in the proxy.xml file on the VC.
And you can also use another, non-standard port but that would require you to change the Web ports in the VIC and specify the new port on the Connect-ViServer cmdlet.
I'm not sure I understand what you mean with "...from internal to DMZ".
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Is it possible to open a port for rpc to connect to the servers from the script run internal. and do not have any script in Running in the DMZ
VM is powered on
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\code\disk.ps1:70 char:28
+ $Partitions = Get-WmiObject <<<< -Class Win32_DiskPartition -ComputerName $VMRef.HostName
==============================================================================
The RPC error is probably because port 135 is not open.
I think you should talk with your Security Officer and/or your Network Team and see what is possible.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What is the min security I can set as a user to run this from the dmz?
Do not know which is the best way... having the password in the dmz and the port open.
or
Opening RPC port and configure RPC to a range of ports on the server
I found this.
How to configure RPC dynamic port allocation to work with firewalls