VMware Cloud Community
vwmagic
Enthusiast
Enthusiast

Powershell script to list all VM's (Windows or Linux) and their NFS mounts or CIFS shares

I am not sure if there are any indications on a VM that has any NFS mounts. I am looking for Powershell script to display VM's along with all NFS mounts. If you can please help me out?
CIFS shares are not so important but if you can also do the same? 

What I can think of is to use "mount" command on Linux VM, and I can grep those NFS mounts. But, powershell to allow me to get on each VM, and run the mount command?

 

Thank you! 

0 Kudos
7 Replies
LucD
Leadership
Leadership

Have you tried the Get-VMGuestDisk cmdlet?
It, provided VMware Tools are installed, should also return the FileSystemType.


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

0 Kudos
vwmagic
Enthusiast
Enthusiast

Just tried Get-VMGuestDisk cmdlet.

PS C:\Users\> get-vmguestdisk -vm xyz

DiskPath CapacityGB FreeSpaceGB FileSystemType
-------- ---------- ----------- --------------
/ 19.491 3.555 ext4
/boot 0.953 0.688 ext4
/app 0.953 0.885 ext4
/home 0.091 0.082 ext4
/app/log 0.953 0.885 ext4
/tmp 4.797 4.511 ext4
/var 6.765 6.121 ext4
/var/cache 2.890 1.288 ext4
/var/log 6.765 6.268 ext4
/var/tmp 3.812 3.581 ext4
/var/log/audit 1.906 1.755 ext4

 

Unfortunately, it only returns local DiskPath with "ext4" type without including NFS type. 

If there are no any indications for NFS. Is there anyway I can use powershell to run "mount" command on each one of Linux VM?

0 Kudos
LucD
Leadership
Leadership

You could use Invoke-VMScript and run the mount command, which should return the NFS mounts.


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

0 Kudos
vwmagic
Enthusiast
Enthusiast

Yes, That would do it. Thanks!

I have more than 1000 such vm's from the vCenter, and not so good at scripting. If it is not too much to ask. Would you please help on scripting to extract VM, and then under each VM, list all NFS file systems with "nfs" type? only extract those VM's with DNS names ending with "cc.abc.com". Appreciate it. 

 

PS C:\Users> invoke-vmscript -vm xyz.abc.com -ScriptText "mount|grep nfs" -GuestUser user1 -GuestPassword passwd1

ScriptOutput
-----------------------------------------------------------------------------------------------------------------------| sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
| nfs-server1-g.abc.com:/vol/prv0/u on /hmt/sirius1/prv0/u type nfs (rw,nosuid,nodev,noatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mou
ntaddr=10.192.27.44,mountvers=3,mountport=635,mountproto=udp,local_lock=none,addr=10.192.27.44)
| nfs-server1-g.abc.com:/vol/skv0/u on /hmt/sirius1/skv0/u type nfs (rw,nosuid,nodev,noatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mou
ntaddr=10.192.27.46,mountvers=3,mountport=635,mountproto=udp,local_lock=none,addr=10.192.27.46)
| nfs-server1-g.abc.com:/vol/chv0/u on /hmt/sirius1/chv0/u type nfs (rw,nosuid,nodev,noatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mou
ntaddr=10.192.27.48,mountvers=3,mountport=635,mountproto=udp,local_lock=none,addr=10.192.27.48)
| nfs-server1-g.abc.com:/vol/viv0/u on /hmt/sirius1/viv0/u type nfs (rw,nosuid,nodev,noatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mou
ntaddr=10.192.27.50,mountvers=3,mountport=635,mountproto=udp,local_lock=none,addr=10.192.27.50)
| nfs-server1-g.abc.com:/vol/prv0/sy on /hmt/sirius1/prv0/sy type nfs (rw,nosuid,nodev,noatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,m
ountaddr=10.192.27.42,mountvers=3,mountport=635,mountproto=udp,local_lock=none,addr=10.192.27.42)
| nfs-server1-g.abc.com:/vol/prv0/rd on /hmt/sirius1/prv0/rd type nfs (rw,nosuid,nodev,noatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,m
ountaddr=10.192.27.44,mountvers=3,mountport=635,mountproto=udp,local_lock=none,addr=10.192.27.44)

-----------------------------------------------------------------------------------------------------------------------

0 Kudos
LucD
Leadership
Leadership

You could try something like this

Get-VM -PipelineVariable vm |
Where-Object { $_.Guest.HostName -match "cc.abc.com$" } |
ForEach-Object -Process {
  $reply = Invoke-VMScript -VM $vm -ScriptText "mount|grep nfs" -GuestUser user1 -GuestPassword passwd1
  $reply.ScriptOutput.Split("`n") | ForEach-Object -Process {
    $_ -match "(.*)\s+on\s+(.*)\s+type" | Out-Null
    New-Object -TypeName PSObject -Property ([ordered]@{
        VM = $vm.Name
        Export = $matches[1]
        MountPoint = $matches[2]
      })
  }
}


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

vwmagic
Enthusiast
Enthusiast

It works well. Thanks a lot!

Due to issues with credential, I am not able to get into quite some VM's and got the following error. If you can please add the line to print out the VM's name when issues like this encountered ?


invoke-vmscript : 5/20/2023 8:43:15 PM Invoke-VMScript Timeout error while waiting for VMware Tools to start in the guest.
At line:1 char:1
+ invoke-vmscript -vm anago.cc.columbia.edu -ScriptText "mount | grep n ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationTimeout: (:) [Invoke-VMScript], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_WaitProcessInGuest_OperationTimeout,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

0 Kudos
LucD
Leadership
Leadership

0 Kudos