
Today we needed to check which VM’s in System Center Virtual Machine Manager (SCVMM) were having ISO’s mounted. In order to prevent us from having to check each of our 200+ virtual machines individually we decided to script this with powershell. This is the script we came up with:
param([string] $VMMServer = $(throw “Please specify a VMM-Server to connect to.”))
Get-VirtualDVDDrive -VMMServer $VMMServer -All | Where-object {$_.ISO -ne $null} | select-object Name, ISO, ISOLinked | Sort-object -Property Name
The script accepts one parameter, the VMM server, and returns the VM-Name, ISO-Name and ISOLinked, which tells you if the ISO is shared instead of copied.
Furthermore we wanted to dismount the ISO’s for all VM’s which had the vmguest-image mounted. Surprisingly enough when searching for a way to mount or unmount an ISO in SCVMM with powershell we didn’t get any results in google (but then again, maybe we didn’t look good enough).
Here’s a script to mount and dismount images to/from VM’s
# UnMount
param([string] $VMServer = $(throw “Please specify a VM to dismount media from.”))
get-virtualDVDDrive -VM $VMServer | set-VirtualDVDDrive -NoMedia
#Mount
param([string] $VMServer = $(throw “Please specify a VM to mount media to.”))
$ISO = get-ISO | where-object {($_.Name -eq ‘[YourISOFile].ISO’) -and ($_.LibraryGroup -eq ‘[YourLibrarygroup]’)}
get-virtualDVDDrive -VM $VMServer | set-VirtualDVDDrive -ISO $ISO -Link
In this script we used the librarygroup because we maintain copies of the same ISO’s in two library groups devided over two locations.
If you would then want to unmount the mounted vmguest images from all VM’s you would get something like this:
Get-VirtualDVDDrive -VMMServer $VMMServer -All | Where-object {($_.ISO -ne $null) -and ($_.ISOLinked -eq $false) -and ($_.ISO -like ‘vmguest’)} | Set-VirtualDVDDrive -NoMedia
Because of the ‘-and ($_.ISOLinked -eq $false)’ only those vmguest images which were not shared will be unmounted in this script.
4 comments
was looking around for something to do this for a good while before coming across your post – thanks a lot for sharing! Worked perfectly
sohungee
Very useful post Mark, It would be great if i could some how get the path of the ISO, My problem is I have a iso file with very large name when it is copied to host its name is correct in the path but SCVMM restricts it to 64 characters so when I tried to mount with the full file name it is unable to find the file name. is there a way to find out the path of iso and check if it contains the file name and mount instead of checking for name some thing like $_.Path.contains(MYfilename). Pardon my language, waiting for response
Hussain
Hi Hussain, To list the SharePath or the folders you could try something like the following:
Get-VirtualDVDDrive -VMMServer [YourVMMServer] -All | Where-object {$_.ISO -ne $null} | select-object Name, ISO, ISOLinked, @{Name="SharePath";Expression={$_.ISO.SharePath}}, @{Name="Directory";Expression={$_.ISO.Directory}} | Sort-object -Property Name | Out-GridView
In this case I ported the results to a gridview to get a better view. You need powershell ISE on the machine where you run this command to display the grid. It will also work without the ‘Out-Gridview’.
As for the mounting part, you could use a partial match of the filename of pathname if you are positive this will only return one result.
This can be done as follows:
$ISO = get-ISO | where-object {($_.Name -match ‘[PartOfFileName]’) -and ($_.LibraryGroup -eq ‘[YourLibrarygroup]‘)}
OR
$ISO = get-ISO | where-object {($_.SharePath -match ‘[PartOfFileName]’) -and ($_.LibraryGroup -eq ‘[YourLibrarygroup]‘)}
The above commands look almost similar to the original ones in the post, except for the ‘-match’ instead of ‘-eq’.
Hope this helps,
Mark
Mark Wolzak
Thanks a lot Mark. You saved me a lot of time.
Ilkin Jamalli