VMware Cloud Community
zenivox
Hot Shot
Hot Shot
Jump to solution

Weird Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty.

Hi there, I have prepared a simple script to do basic tasks on VMs and it works. However after all executes fine at the end I get the error below:

Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty.

Again all works fine, here's the script by the way:

# retrieve list of VMs from txt file

$vmList = Get-Content -Path "C:\Users\cavalloga\Desktop\VMs.txt"

# convert list of VMs text in list of VMs objects

foreach($txt in $vmList){

     if((Get-VM -Name $txt -ErrorAction SilentlyContinue).Name -eq $null){

     $otherVMs += "$txt `r`n"

     Write-Host "VM's name $txt is either mispelled or VM is in a different vCenter server" -ForegroundColor Black -BackgroundColor Yellow

     } else {

        $vmNames += Get-VM $txt

        }

}

$otherVMs | Out-File "C:\Users\cavalloga\Desktop\otherVMs.txt"

I removed PowerCLI 6.5 and installed the latest. If that is the reason maybe some 6.5 leftovers are bugging? Where can I find for its leftover in case that is the reason. Many thanks guys and have a good weekend!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you check that $vmList is actually an array, without any empty elements in there?

Do a

Get-Content -Path "C:\Users\cavalloga\Desktop\VMs.txt"

And also a

(Get-Content -Path "C:\Users\cavalloga\Desktop\VMs.txt" ).Count

Did you remove all traces of the old PowerCLI version?

  • Uninstall via the Programs and Features control panel applet
  • Check if there were any VMware folders remaining and if yes, delete them

I would suggest to clean up everything, including the folders for the new PowerCLI version.
Then restart your station, and do a new install with

Install-Module -Name VMware.PowerCLI

There are more details on doing the upgrade in Welcome PowerCLI to the PowerShell Gallery – Install Process Updates


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Did you check that $vmList is actually an array, without any empty elements in there?

Do a

Get-Content -Path "C:\Users\cavalloga\Desktop\VMs.txt"

And also a

(Get-Content -Path "C:\Users\cavalloga\Desktop\VMs.txt" ).Count

Did you remove all traces of the old PowerCLI version?

  • Uninstall via the Programs and Features control panel applet
  • Check if there were any VMware folders remaining and if yes, delete them

I would suggest to clean up everything, including the folders for the new PowerCLI version.
Then restart your station, and do a new install with

Install-Module -Name VMware.PowerCLI

There are more details on doing the upgrade in Welcome PowerCLI to the PowerShell Gallery – Install Process Updates


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

0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

silly me... this was they key:

".....without any empty elements in there?"

thanks again Luc!

0 Kudos
dbanrocks
Contributor
Contributor
Jump to solution


I need help on the same. Trying to perform storage migration 2 vms at a time,

This is my Script:


connect-viserver vcenter.aptean.com


# Maximum simultaneous tasks
$maxtasks = 2

$VMsToRelocate = Get-Content -path E:\Scripts\PowerCLI\Migration\test.txt
$Datastore = Get-DatastoreCluster -Name "Products"

$i = 0

foreach ($VM in $VMsToRelocate)
{
if ((get-vm $VM | get-datastorecluster) -ne $datastore)
{
$i=$i+1

$tasks = Get-Task -Status "Running" | where {$_.Name -eq "ApplyStorageDrsRecommendation_Task"}

while($tasks.Count -gt ($maxtasks-1))
{
sleep 5
$tasks = Get-Task -Status "Running" | where {$_.Name -eq "ApplyStorageDrsRecommendation_Task"}
}

Write-Host $i ". Relocating VM:" $VM.Name "to" $Datastore

Get-VM -Name $VM.Name | Move-VM -datastore $Datastore > $null -runasync
}
}
Write-Host $i " Migrations Completed!"

Disconnect-VIServer -Server vcenter.aptean.com -confirm:$false


And this is the output I am getting

PS C:\Users\Administrator>>> E:\Scripts\PowerCLI\Migration\sDRS_Pure2Nimble_2vms.ps1

Name Port User
---- ---- ----
vcenter.aptean.com 443 PROD\sa_dbanerjee
1 . Relocating VM: to Products
Get-VM : 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 E:\Scripts\PowerCLI\Migration\sDRS_Pure2Nimble_2vms.ps1:28 char:18
+ Get-VM -Name $VM.Name | Move-VM -datastore $Datastore > $null -ru ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

2 . Relocating VM: to Products
Get-VM : 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 E:\Scripts\PowerCLI\Migration\sDRS_Pure2Nimble_2vms.ps1:28 char:18
+ Get-VM -Name $VM.Name | Move-VM -datastore $Datastore > $null -ru ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

3 . Relocating VM: to Products
Get-VM : 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 E:\Scripts\PowerCLI\Migration\sDRS_Pure2Nimble_2vms.ps1:28 char:18
+ Get-VM -Name $VM.Name | Move-VM -datastore $Datastore > $null -ru ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

3 Migrations Completed!

Powercli Versions:

PS C:\Users\Administrator>>> Get-Module

ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0.0.0 ISE {Get-IseSnippet, Import-IseSnippet, New-IseSnippet}
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script 10.0.0.... VMware.VimAutomation.Cis.Core {Connect-CisServer, Disconnect-CisServer, Get-CisService}
Script 10.0.0.... VMware.VimAutomation.Common
Script 10.0.0.... VMware.VimAutomation.Core {Add-PassthroughDevice, Add-VirtualSwitchPhysicalNetworkAdapter, Add-VMHost, Add-VMHostNtpServer...}
Script 10.0.0.... VMware.VimAutomation.Sdk {Get-InstallPath, Get-PSVersion}

PS C:\Users\Administrator>>> Get-PSVersion

Major Minor Build Revision
----- ----- ----- --------
5 1 14393 4583

 

PS C:\Users\Administrator>>> Get-PowerCLIVersion
WARNING: The cmdlet "Get-PowerCLIVersion" is deprecated. Please use the 'Get-Module' cmdlet instead.

PowerCLI Version
----------------
VMware PowerCLI 10.0.0 build 7895300
---------------
Component Versions
---------------
VMware Cis Core PowerCLI Component PowerCLI Component 10.0 build 7893915
VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 10.0 build 7893909

Tags (2)
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are using an older PowerCLI version, you should consider upgrading.

Why are you using $VM.Name?

The name of the VMs is in $VM, which has no property Name.
Just use $VM


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

0 Kudos