General Floppy script [Hyper-V]

Having written an “all-in-one” script for DVD media management with a Hyper-V virtual machine I figured I would take a look at what would be involved in doing the same sort of thing for floppy disks.  For the most part this was as simple as doing a search and replace on the key methods and properties.

Now I have a floppy disk script that will let you:

  • View the current floppy disk configuration
  • Connect a new virtual floppy disk
  • Eject the current virtual floppy disk

(note that Hyper-V does not support the use of physical floppy disks)

Here is a screen shot of it running on my computer:

image

And here is the code for the script.

Once again – this is a pretty huge script but I have attached a copy of the original to the end of this post.

 # Function for handling WMI jobs / return values
 Function ProcessResult($result, $successString, $failureString)
 {
    #Return success if the return value is "0"
    if ($result.ReturnValue -eq 0)
       {write-host $successString} 
  
    #If the return value is not "0" or "4096" then the operation failed
    ElseIf ($result.ReturnValue -ne 4096)
       {write-host $failureString "  Error value:" $result.ReturnValue}
  
    Else
       {#Get the job object
       $job=[WMI]$result.job
  
       #Provide updates if the jobstate is "3" (starting) or "4" (running)
       while ($job.JobState -eq 3 -or $job.JobState -eq 4)
          {write-host $job.PercentComplete "% complete"
           start-sleep 1
  
           #Refresh the job object
           $job=[WMI]$result.job}
  
        #A jobstate of "7" means success
        if ($job.JobState -eq 7)
           {write-host $successString}
        Else
           {write-host $failureString
           write-host "ErrorCode:" $job.ErrorCode
           write-host "ErrorDescription" $job.ErrorDescription}
        }
 }
  
 # Function for ejecting the floppy
 Function EjectFloppy()
 {
  
    # If there is media in the drive - eject it
    if ($existingRASD) {
       $result = $VMMS.RemoveVirtualSystemResources($VM, @($existingRASD))
       ProcessResult $result "The floppy disk has been ejected." "Failed to clear the floppy drive."}
 }
  
 # Prompt for the Hyper-V Server to use
 $HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
  
 # Prompt for the virtual machine to use
 $VMName = Read-Host "Specify the name of the virtual machine"
  
 # Get the management service
 $VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
  
 # Get the virtual machine object
 $VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $HyperVServer
  
 # SettingType = 3 ensures that we do not get snapshots
 $SystemSettingData = $VM.getRelated("Msvm_VirtualSystemSettingData") | where {$_.SettingType -eq 3}
  
 # Get the floppy drive
 $FloppyDrive = $SystemSettingData.getRelated("Msvm_ResourceAllocationSettingData") | where{$_.ResourceType -eq 14}
  
 # Setup parameters for main menu prompt
 $message = "What do you want to do with the floppy drive?"
 $info = New-Object System.Management.Automation.Host.ChoiceDescription "&Display Information", "Display information about the current contents of the floppy drive."
 $eject = New-Object System.Management.Automation.Host.ChoiceDescription "&Eject", "Eject the current contents of the floppy drive."
 $vfd = New-Object System.Management.Automation.Host.ChoiceDescription "Connect &VFD", "Connect a VFD file to the floppy drive."
 $quit = New-Object System.Management.Automation.Host.ChoiceDescription "&Quit", "Exit the Floppy Tools script."
 $options = [System.Management.Automation.Host.ChoiceDescription[]]($info, $eject, $vfd, $quit)
  
 do 
    {
  
    # Check to see if there is already media in the floppy drive
    $existingRASD = $SystemSettingData.getRelated("Msvm_ResourceAllocationSettingData") | where {$_.Parent -eq $FloppyDrive.__Path}
  
    # Ask the user what they want to do with the floppy drive
    write-host
    $promptResult = $host.ui.PromptForChoice($null, $message, $options, 0) 
    write-host
  
    switch ($promptResult)
       {
          0  {# Display information
         
              # If there is media in the drive - display key information
              if ($existingRASD) {
                 write-host "Media type: " $existingRASD.ElementName
                 write-host "Media     : " $existingRASD.Connection
                 }
              else {
                 write-host "No floppy disk is configured."
                 }
              }
     
          1 {# Eject the floppy
             EjectFloppy
             }
            
          2 {# Connect a VFD file
            
             # Prompt for the VFD file to connect
             $vfdPath = Read-Host "Specify the VFD file to connect to the virtual machine"
            
             EjectFloppy
             # Create a new object for a floppy media, connected to the floppy drive
             $FloppyAllocationCapabilities = (gwmi -computername $HyperVServer -namespace root\virtualization Msvm_AllocationCapabilities `
                                              -filter "ResourceType=21 and ResourceSubType='Microsoft Virtual Floppy Disk'").__Path.Replace('\', '\\')
             $FloppySettingsData = [wmi](gwmi -computername $HyperVServer -namespace root\virtualization Msvm_SettingsDefineCapabilities `
                                         -filter "GroupComponent='$FloppyAllocationCapabilities' and ValueRange=0").PartComponent
             $FloppySettingsData.Connection = $vfdPath
             $FloppySettingsData.Parent = $FloppyDrive.__Path
  
             # Add the media to the virtual machine
             $result = $VMMS.AddVirtualSystemResources($VM, $FloppySettingsData.GetText(1))
  
             ProcessResult $result "The VFD file has been connected." "Failed to connect the VFD file."
             }
            
        }
    }
    
 until ($promptResult -eq 3)

 

Cheers,
Ben

FloppyTools.zip