Niram
Contributor
Contributor

cmdlet ForEach-Object at command pipeline position 2

Hi all,

I have the following script I wrote and for some reason when I run it, I get a "supply values for the following parameter" although all the parameters values were included in a CSV file I created for input.

The script is used for mapping virtual disks to my VMs.

This is my script:

Import-Csv -Path Luns.csv | ForEach-Object `
{
  $vm = Get-VM $_.VM
  $LunType = $_.LunType
  if (LunType eq "RDMP")
  {
    $Lun = $_.Lun
    $deviceName = ($vm | Get-VMHost | Get-ScsiLun | Where-Object {$_.RunTimeName.Split(":")[3].TrimStart("L") -eq $Lun}).ConsoleDeviceName 
    New-HardDisk -VM $vm -DiskType RawPhysical -DeviceName $deviceName
  }
  elseif (LunType eq "VMFS")
  {
    $LunSize = $_.LunSize
    $Datastore = $_.DataStore
    New-HardDisk -VM $vm -Datastore $Datastore -CapacityKB $LunSize*1024*1024
  }
  Invoke-VMScript -vm $vm -guestuser administrator -guestpassword kashya -scripttype bat -scripttext "diskpart /s c:\part.txt"
  Invoke-VMScript -vm $vm -guestuser administrator -guestpassword kashya -scripttype bat -scripttext "echo y | format e: /q /fs:ntfs /v:"
}

And this is my CSV:

VMLunLunTypeLunSizeDataStore
Clus1VM31RDMP
Clus1VM32RDMP
Clus1VM53RDMP
Clus1VM5VMFS5DS3
Clus1VM65RDMP
Clus1VM7VMFS5DS3

Thanks for the help,

Nir

0 Kudos
1 Reply
LucD
Leadership
Leadership

Perhaps something went wrong with the copy paste, but the comparison statements should say

if($_.LunType -eq "RDMP")

and

elseif ($_.LunType -eq "VMFS")

or (if you want to use the local variable)

if($LunType -eq "RDMP")
...
elseif ($LunType -eq "VMFS")


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

0 Kudos