'Powershell - Cannot validate argument on parameter 'Name'. The number of characters (0) in the argument is too small

Its my first day with powershell scripting I am trying using VMM Cmdlet Get-SCVirtualMachine it works fine when I use it like

PS C:\> $VM = Get-SCVirtualMachine -Name "VM01"

But it gives me an error when I try

PS C:\> $vmName = 'VM01'
PS C:\> $VM = Get-SCVirtualMachine -Name "$vmName"

The error I get is

Cannot validate argument on parameter 'Name'. The number of characters (0) in the argument is too small. Specify an argument whose length is greater than or equal to "1" and then try the command again.

Can someone tell me what is it that I am doing wrong?

Thanks



Solution 1:[1]

The command looks OK and should work. Are you getting the error as a part of script or just by the example you posted?

You mention that you're using the Get-SCPerformanceData cmdlet and your example uses another cmdlet.

Try removing the quotes from the parameter, does it help?

Solution 2:[2]

try

PS C:\> $vmName = "VM01"
PS C:\> $VM = Get-SCVirtualMachine -Name $vmName

Solution 3:[3]

Try this:

$vmName = '-Name VM01'
Get-SCVirtualMachine $vmName

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shay Levy
Solution 2 Community
Solution 3 jww