vmware / PowerCLI-Example-Scripts

http://blogs.vmware.com/powercli
Other
765 stars 608 forks source link

Need to mount local disk ISO to a vmware VM #261

Open pythongitram opened 5 years ago

pythongitram commented 5 years ago

I would like to to mount local disk ISO to a vmware VM without copying to a Datastore. This can be achieved through remote console of the VM. But I need to do that through powercli or powershell. Could someone help on this if there is a solution.

pythongitram commented 5 years ago

Can someone help on this >?

mtelvers commented 5 years ago

Set-CDDrive does not allow you to do this. I really can't see how it would work as a connection would need to be maintained between the host and the PowerCLI instance in order to allow the VM to access the ISO.

Similarly if you use Remote Console (VMRC) the connection to the CD drive is only able to access the ISO while the application is running.

What's your overall objective here - perhaps we can suggest an alternative approach?

pythongitram commented 5 years ago

My main objective is that I need to map an ISO that is stored locally on my desktop. I can map that ISO from Remote console (VMRC) Manually. I would like to map the same ISO in an automated fashion. I do not want that ISO to be copied to datastore , then attach it .

mtelvers commented 5 years ago

How about something like this?

Firstly create, or in your case you probably have, a local folder containing your ISO files. I'm going to use C:\ISO

Secondly, download WinNFSd.exe from https://github.com/winnfsd/winnfsd/releases and save it in a folder. I've saved it to C:\WinNFSd

Next create a firewall rule to allow the allow the ESXi host to communicate with WinNFSd, thus:

 New-NetFirewallRule -DisplayName "NFS Server" -Direction Inbound -Action Allow -Program C:\WinNFSd\WinNFSd.exe

Run WinNFSd. The argument list is the local folder to be shared and the path that it will have on the NFS server's export list which just needs to match the path on the New-DataStore command later:

 Start-Process C:\WinNFSd\WinNFSd.exe -ArgumentList "C:\ISO /ISO"

You should now have a CMD window open along with the PowerCLI prompt. You can minimise the CMD window and continue.

Now you need to know the IP Address of your machine. You can use something like this command to determine it or just do it manually with ipconfig and run $myIPAddress = "Your IP Address". Your may need to tweak this command depending upon how complex your networking setup is.

 $myIPAddress = $(Get-NetIPAddress -InterfaceAlias Ethernet0 -AddressFamily IPv4).IPAddress

Instruct the ESXi host to mount the datastore. If you have a cluster with multiple hosts you can use Get-Cluster | New-Datastore but here I am just doing it on a test host. Name here set the datastore name which needs to match the name prefix in square brackets in the Set-CDDrive command later.

 New-Datastore -VMHost <your host> -Name ISO -NfsHost $myIPAddress -Path /ISO

Now set the ISO that you have in C:\ISO\myiso.iso to be the CD Drive on your VM

 Get-CDDrive <your VM> | Set-CDDrive -IsoPath "[ISO] myiso.iso" -Connected:$true -Confirm:$false

That's it!

You can tidy away everything with these commands to (1) unmount the ISO, (2) Remove the datastore, (3) Stop the NFS server, (4) Remove the firewall rule. Clearly, if you're going to do this a lot then there's mileage in leaving some of these things in place

 Get-CDDrive <your VM> | Set-CDDrive -NoMedia -Confirm:$false
 Remove-Datastore -VMHost <your host> -Datastore ISO -Confirm:$false
 Stop-Process -Name WinNFSd
 Remove-NetFirewallRule -DisplayName "NFS Server"

Of course, if you have a Windows Server around you can just install the Windows NFS Server Feature on that and have a Windows share which doubles as a VMFS datastore.