ForEach Block of results - Prompt Input.

by maverick918 at 2013-02-25 07:56:00

Hello -

I have a question about the results I’m getting from a script. I have modified a version of Get-VMDiskMapping (From PowerCLI Reference) to work in my environment and I get output like this.

VMSCSIController : SCSI controller 0
VMDiskName : Hard disk 2
SCSI_Id : 0 : 0
VMDiskFile : [datastore1] VM/VM.vmdk
VMDiskSizeGB : 50
WindowsDisk : Disk 1
WindowsDiskSizeGB : 49.9993586540222

VMSCSIController : SCSI controller 0
VMDiskName : Hard disk 3
SCSI_Id : 0 : 1
VMDiskFile : [datastore2] VM/VM_1.vmdk
VMDiskSizeGB : 36
WindowsDisk : Disk 2
WindowsDiskSizeGB : 35.9961676597595

This returns in RAW text so it cannot be used as a normal array so I need to re-import it as an array to use the data at all. What this does is count the hard disks and report back on them so it could be any number depending on the system.

What my goal is - import the data - ask user for input based on an array value.

For example:

The user will see this.

WindowsDisk : Disk 1
VMDiskSizeGB : 50

What is the Letter for this Disk?
What is the Label for this Disk?

WindowsDisk : Disk 2
VMDiskSizeGB : 36

What is the Letter for this Disk?
What is the Label for this Disk?

Until Disk # stops incrementing I’d like this to continue to prompt for input. But how will I do this? Will I need a dynamic variable that creates with each Disk #? I’d like to be able to run this on multiple systems so hanging variable data should be dropped after each system - some sort of clear command. Once all input is received for one system it will create a diskpart file with the data and send it to the system and execute it. Thats the end goal. I have all the pieces except this one. I will research more as well and If I find anything I’ll report here.

Thanks for the help.
by poshoholic at 2013-02-26 08:47:01
You mean something like this?

# Rough pseudo-code
foreach ($diskMappingRecord in Get-VMDiskMapping) {
$diskMappingRecord | Format-List WindowsDisk,VMDiskSizeGB
$letter = Read-Host -Prompt 'What is the Letter for this Disk?'
$label = Read-Host -Prompt 'What is the Label for this Disk?'
# Do your work to assign the letter and label here
}
by maverick918 at 2013-02-26 11:12:17
Exactly like that! haha thanks Kirk!

This has again sparked me into action. I’ll run with this and see where it takes me. Thanks for the fuel!

Jake