VMware Cloud Community
jvm2016
Hot Shot
Hot Shot

foreach_loop_csvinput_powercli

hi Luc,

can you please suggest what is wrong in following .it used to work fine numerous times in past but for some reasons not today.

$vms_info=Import-Csv -Path "c:\users\user1\desktop\folder1\vmnames.csv"

$vms.count

foreach ($line in $vms_info)

{

$vma=get-vm $line.vmname

$vma|select name

}

iam getting folowing

pastedImage_0.png

0 Kudos
16 Replies
LucD
Leadership
Leadership

There could be an issue in the CSV file.

Does the 1st line contain the headers?

Is the column actually called vmname?

Is there a blank line in the CSV?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

well i used it many times in past so i checked all what yu asking .

it is something like this

vmname

vm1

vm2

vm3

vm4

vm5

however what actually i am looking is to add provisionedspace of vms provided by csv file .

thanks

0 Kudos
LucD
Leadership
Leadership

Is the error gone then?
There is not really a point in adding functionality when the basic loop is not working.


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

0 Kudos
StephenMoll
Expert
Expert

The snippet of script works fine unless the first line of the CSV is blank.

Then I see the error the OP describes.

0 Kudos
LucD
Leadership
Leadership

I think I mentioned the blank line in my earlier reply.


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

0 Kudos
StephenMoll
Expert
Expert

So you did! :smileyblush:

0 Kudos
StephenMoll
Expert
Expert

What wasn't mentioned was any errors or warnings raised before the one mentioned. I believe the import-csv commandlet will say :

"WARNING : one or more headers were not specified. Default names starting with "H" have been used in placed of any missing headers."

So should have been easy spot, if that was the actual case.

0 Kudos
jvm2016
Hot Shot
Hot Shot

it has not gone .howevereverthing seems correct .

also $vms_info variable has following

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

Can you run this

$vms_info = Import-Csv -Path "c:\users\user1\desktop\folder1\vmnames.csv"

$vms_info.Count


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

i tried that its giving me 5 .

0 Kudos
LucD
Leadership
Leadership

Now try

Import-Csv -Path "c:\users\user1\desktop\folder1\vmnames.csv" |

ForEach-Object -Process {

   Get-VM -Name $_.vmname | select Name

}


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

i thought of doing this way wherein we need to input vms.all i need is to add provisioned space of each vm .

can you pleasemodifyorange code in simplest wat to get sum of provisioned space .

$strings = @()

do {

    $answer = Read-Host -Prompt "Enter a substring (blank to end)"

    if($answer){

        $strings += $answer

    }

} until ($answer -eq '')

for ($i=0; $i -lt $strings.Length; $i++)

{

$vm=get-vm -Name $strings[$i]

$vm|select name,provisionedspacegb

$sum=0

$final_sum=$sum+$vm.ProvisionedSpaceGB

}

0 Kudos
LucD
Leadership
Leadership

Before we go any further, is the CSV and the loop working now?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

for some wieredreasons it is not however in the mean time i figured out the following way and it is working.however iam checking the csv and foreach loop one more time .

$strings = @()

do {

    $answer = Read-Host -Prompt "Enter a substring (blank to end)"

    if($answer){

        $strings += $answer

    }

} until ($answer -eq '')

$fragments = @()

for ($i=0; $i -lt $strings.Length; $i++)

{

$vm=get-vm -Name $strings[$i]

$vm|select name,provisionedspacegb

#$sum=0

$provisionedspacegb=$vm.ProvisionedSpaceGB

$provround=[math]::Round($provisionedspacegb)

$fragments += $provround

}

$sum = $fragments -join '+'

Invoke-Expression $sum

0 Kudos
LucD
Leadership
Leadership

So your question is answered?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

well in some other way iam still checking why the csvthing does not work .

0 Kudos