how to get the VMs name via powershell command

Hi all:

I can get the VMs name for the specific  host via the command "Get-VM" as below:

PS E:\>  Get-VM -ComputerName 172.18.139.240 | Where-Object {$_.State eq 'Running'} | select name |select Name | foreach {$_.Name}

win2008
win7

SO, I can get the VMs name win2008 and win7

I want to get  the VMs String, But it is error format as below line:

 $VMname =Get-VM -ComputerName 172.18.139.240 | Where-Object {$_.State eq 'Running'} | select name |select Name | foreach {$_.Name}

The error info:

powershell : Get-SCVirtualMachine : A parameter cannot be found that matches parameter name

Why ? I think "Get-VM -ComputerName 172.18.139.240 | Where-Object {$_.State eq 'Running'} | select name |select Name | foreach {$_.Name}"  is String not table





  • Edited by jonesliu Friday, June 19, 2015 10:01 AM
June 19th, 2015 9:24am

can you tell me how  to modify my script??

$VMname =Get-VM -ComputerName $ComputerName| Where-Object {$_.State eq 'Running'} |select Name | foreach {$_.Name}

    Foreach($VM in $VMname)
    {
      Stop-VM -Name $VM -TurnOff

    }

I also try the script as below:
    $runningVM=Get-VM ComputerName $ComputerName | Where-Object {$_.State -eq 'Running'}

    foreach($cn in $runningVM){
        Write-Host "Shutting down $($cn.name)"
        stop-vm $cn.name -force
     }

The error info:

powershell : Get-SCVirtualMachine : A parameter cannot be found that matches parameter name
At line:1 char:1
+ powershell E:\shutdownVMs_scvmm.ps1 -computername 172.18.139.240
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Get-SCVirtualMa...parameter name :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

'ComputerName'.
At E:\shutdownVMs_scvmm.ps1:56 char:23
+     $runningVM=Get-VM -ComputerName $ComputerName | Where-Object {$_.State
-eq ' ...
+                       ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-SCVirtualMachine], Par
   ameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.SystemCenter.Vi
   rtualMachineManager.Cmdlets.GetVMCmdlet

    

  • Edited by jonesliu Tuesday, June 23, 2015 3:07 AM
Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 1:36am

SCVMM 2012R2 Command:

$vm = Get-SCVirtualMachine |
where {$_.ComputerName -like "GuestVMName*" -and $_.VMHost -like "HyperVServerName*"}

$vm.name 

$vm = Get-SCVirtualMachine | where {$_.ComputerName -like "GuestVMName*" -and $_.Status eq 'Running'}

foreach ($machine in $vm)
    {
    write-host $machine.name --- $machine.Status
    stop-vm $machine
    write-host $machine.name --- $machine.Status
    sleep 2
    start-vm $machine
    write-host $machine.name --- $machine.Status
    }

HyperV 2012R2 (no VMM) Command:

get-vm | where {$_.name -like "HTS*"}
Add "-computername "YourHyperVServer.DomainName.local"" after get-vm if you want to list VMs on a remote HyperV server.



June 26th, 2015 10:34am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics