VMware Cloud Community
emcclure
Enthusiast
Enthusiast
Jump to solution

Need a script to scan hosts on a vCenter and see what datastores are mounted to each one

Hello,

I'm trying to figure this out, but not having much luck so far.  I'm cleaning up a vCenter (patching hosts, removing vSwitches and port groups and unused datastores) to move some hosts to a new vCenter.  I just want to be able to scan all the hosts in the vCenter and get a text file or csv file that shows me what NFS datastore are mounted on each host.  I haven't had much luck on google and trying this isn't working well for me.  I know if I do something like this:

Get-Datacenter -Name MyDatacenter | Get-VMHost -Name hostname | Get-Datastore | where {$_.type -eq "NFS"} | Format-Table I will get a result for the host.  However it only shows me the name of the datastore, the free space and capacity.  It doesn't tell me what host it's attached to which I really want.  I saw this thread: powershell script that can pull NFS datastore info mounted from VC host

but it didn't do anything for me.  I also found the following code on another thread but for the vSwitch info (from LucD):

&{foreach($esx in Get-VMHost){

    $vNicTab = @{}

    $esx.ExtensionData.Config.Network.Vnic | %{

        $vNicTab.Add($_.Portgroup,$_)

    }

    foreach($vsw in (Get-VirtualSwitch -VMHost $esx)){

        foreach($pg in (Get-VirtualPortGroup -VirtualSwitch $vsw)){

            Select -InputObject $pg -Property @{N="ESX";E={$esx.name}},

                @{N="vSwitch";E={$vsw.Name}},

                @{N="Active NIC";E={[string]::Join(',',$vsw.ExtensionData.Spec.Policy.NicTeaming.NicOrder.ActiveNic)}},

                @{N="Standby NIC";E={[string]::Join(',',$vsw.ExtensionData.Spec.Policy.NicTeaming.NicOrder.StandbyNic)}},

                @{N="Portgroup";E={$pg.Name}},

                @{N="VLAN";E={$pg.VLanId}},

                @{N="Device";E={if($vNicTab.ContainsKey($pg.Name)){$vNicTab[$pg.Name].Device}}},

                @{N="IP";E={if($vNicTab.ContainsKey($pg.Name)){$vNicTab[$pg.Name].Spec.Ip.IpAddress}}}

        }

    }

}} | Export-Csv report.csv -NoTypeInformation -UseCulture

So I feel like that could possibly work, though I'm not quite sure what to change to what exactly to do the datastore tab.  I'm assuming I'd change $vNicTab to $vDatastoreTab and probably the Get-VirtualSwitch to Get-Datastore, but other than that I wouldn't know where to begin.  Can anybody help with this?

Thanks.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do

Get-Datacenter -Name MyDC | Get-Datastore -PipelineVariable ds | where{$_.Type -eq 'NFS'} | Get-VMHost |

select Name,@{N='Datastore';E={$ds.Name}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You could do

Get-Datacenter -Name MyDC | Get-Datastore -PipelineVariable ds | where{$_.Type -eq 'NFS'} | Get-VMHost |

select Name,@{N='Datastore';E={$ds.Name}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Thanks LucD that worked perfectly.

Reply
0 Kudos