Hi Trying to run below script but getting the error message.
It is working fine with foreach-object but getting the error message with foreach($VMNAME in $VMCSV)

$VMCSV=Import-csv 'D:\17-10-20\Startvm.csv'
foreach($VMNAME in $VMCSV){
$Name = $VMNAME.VMNAME
$RGN = $VMNAME.RGN
$Location = $VMANME.Location
$VMLIST = Get-azurermvm -status -Name $Name -ResourceGroupName $RGN
$Status   = $VMLIST.Statuses[1].DisplayStatus
}
3 Spice ups

well… what is the error?

how do you have it written with foreach-object?

Hi Support,

Please find the error message in below.

Cannot validate argument on parameter ‘Name’. The argument is null or empty. Provide an argument that is not null or empty, and then try the
command again.
At line:5 char:32

support…?

What columns do you have in your CSV? Do you have a column called ‘vmname’ and ‘RGN’?

Does every row in the CSV have a value?

$VMCSV = Import-csv 'D:\17-10-20\Startvm.csv'
foreach($VM in $VMCSV){
    $VMLIST = Get-azurermvm -Name $vm.vmname -ResourceGroupName $vm.RGN -status
    $Status   = $VMLIST.Statuses[1].DisplayStatus
    $status
}

Hi Neally,

Thanks.

Script is running fine but also showing the error message.

PS C:\WINDOWS\system32> $VMCSV = Import-csv 'D:\17-10-20\Startvm.csv'
foreach($VM in $VMCSV){
    $VMLIST = Get-azurermvm -Name $vm.vmname -ResourceGroupName $vm.RGN -status
    $Status   = $VMLIST.Statuses[1].DisplayStatus
    $status
}
VM deallocated
VM running

Error Message:
Get-AzureRmVM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:3 char:35
+     $VMLIST = Get-azurermvm -Name $vm.vmname -ResourceGroupName $vm.R ...
+                                   ~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-AzureRmVM], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Compute.GetAzureVMCommand


can you please post a sanitized version of your CSV ?

again, are all the row populated?

it is basically saying there is a blank value.

Hi Neally,

CSV is correct but i don’t know why it’s showing the error message.

RGN,VMNAME,Location,Subscriptionid
lab_RG,dcserver,East US,69689b9511111111111111
LAB_RG,win-2019,East US,98654b9555555555555555

looks rights.

how does your code for the foreach-object look like that works?

Hi Neally,

Yes, I am also checking why getting the error message.

Thanks for support

can you post the working code?

im super positive it has to do with your input file, rather than the foreach loop.

does it give you that error for every single row?

what if you run it like so, do you see any blank values?

$VMCSV = Import-csv 'D:\17-10-20\Startvm.csv';

foreach($VM in $VMCSV){
    write-output "'$($vm.vmname)'"
    write-output "'$($vm.rgn)'"
}

Hi Neally,

No, Output is correct.

Looks like you had similar problem here:

but you never answered?

the foreach looks right, im not sure why it does not work.

You said foreach-object does work.

Can you post the code for the foreach-object you are using?

I want to see if / where the difference is, but it still looks like data issue.

Again, do you get the error for every row, or just for a few?

I think the format is missing

$VMCSV=Import-csv "D:\17-10-20\Startvm.csv""
foreach($VMNAME in $VMCSV)
{
$Name = $VMNAME.VMNAME
$RGN = $VMNAME.RGN

$VMLIST = Get-AzureRmVM -ResourceGroupName $RGN -Name $Name -Status
$VMLIST.Statuses[1].DisplayStatus

}

Maybe trying with a for a loop, I answered something like this the other day where for some reason the user could not get the foreach to work

$VMCSV = Import-csv 'D:\17-10-20\Startvm.csv'

for($i = 0; $i -lt $VMCSV.count -1; $i++){

try{

    $VMLIST = Get-azurermvm -status -Name $VMCSV[$i].VMName -ResourceGroupName $VMCSV[$i].RGN
    $Status   = $VMLIST.Statuses[1].DisplayStatus

}
catch{

    "Check sub index of $i"

}

}