List name column of Hyper-V VM using powershell - powershell

This powershell command shows all Hyper-V VM which are running (with more columns: Name, State, CPU Usage, etc.):
Get-VM | where {$_.State -eq 'Running'}
How can I get list only with name column of all running Hyper-V VM?

To do that just select the property you need.
Get-VM | where {$_.State -eq 'Running'} | select Name
or VmName, not sure what's the proper property name

Related

Can powercli tell me what the VM Name is when listing network adapters for vms?

I want to get a list of VMs with a given vlan name configured so that when I am rolling back a vlan, with ACI I am certain that it is gone.
This script works, I connect to the vcenter with powercli and pass in a vlan_name:
foreach ($vm in Get-VM){
$nic = Get-NetworkAdapter -VM $vm.name
if ( $nic.NetworkName -eq "{{ vlan_name }}" ){
echo $vm.name
}
}
The problem is, it is an O(n) sort of algorithm and takes a long time to run (I have thousands of VMs and hundreds of vlans)
The annoying thing is
Get-VM | Get-NetworkAdapter
lists all the vlan's quickly, but doesn't output the vm names.
Is there a way I can get the VM use by Network Adapter?
This PowerShell lists out the VM name, the network adapter, and the type of network it's connected.
PowerShell
Get-VM | Get-NetworkAdapter | Select-Object #{N="VM";E={$_.Parent.Name}}, Name, Type;
or
Get-VM | Get-NetworkAdapter | Select-Object Parent, Name, Type;
Sample Output
VM Name Type
-- ---- ----
fserver3 Network adapter 1 Vmxnet3
pserver2 Network adapter 1 Vmxnet3
hserver2 Network adapter 1 Vmxnet3
lserver22 Network adapter 2 Vmxnet3
server1 Network adapter 1 Vmxnet3
server2 Network adapter 1 Vmxnet3
PowerShell (Filter Network Name)
Get-VM | Get-NetworkAdapter | Where-Object {$_.Name -eq "vlan_name"} | Select-Object #{N="VM";E={$_.Parent.Name}},Name,Type;
Supporting Resources
Select-Object
Where-Object
Calculated_Properties
Add a calculated property with Select-Object in PowerShell

Get Hyper-V VM Tag

I can set a tag for the VM through Windows Admin Center, but I can't find how to view this tag through powershell.
I need this for a script so that I take all VM names with a specific tag
UPD:
I am using hyper-v server core 2019
Thanks to #zett42, I solved the problem, albeit in a different way
$vm = Get-VM my_vm
$vm | Set-VM -Notes "mytag" -Confirm:$false
$VMs = Get-VM | Where-Object Notes -match "mytag" | Select-Object -Expand Name
$VMs | Out-File ./vms.txt

Powershell command to List the services on a specific/individual cluster node

How to find the list of service on a specific node in a given cluster using Powershell?
There are 2 nodes in this cluster group ServerName_1 and ServerName_2, I am trying to fetch the services on Server_name1.
I have tried running the below commands but I did not get any output or error.
I tried using the below command and I could get the results:
PS C:\Users\sd> Get-ClusterGroup | Where-Object {$_.State -EQ "Online"}
Name OwnerNode State
---- --------- -----
Service_1 ServerName_1 Online
Service_2 ServerName_2 Online
However, when i tried to extract the OwnerNode using the same command i do not see any result , as observed below:
PS C:\Users\sd> Get-ClusterGroup | Where-Object {$_.OwnerNode -EQ <ServerName_1>}
PS C:\Users\sd>
As I do not see any output, I am not sure whether the command i executed is correct?
I need this, so that I may start the specific service on ServerName_1 alone.
PS C:\Users\sd> Start-ClusterGroup -Name <ServiceName> | Where-Object
{ $_.OwnerNode -eq "<ServerName1>" }
PS C:\Users\sd>
You don't need the < > in your code, update
Get-ClusterGroup | Where-Object {$_.OwnerNode -EQ <ServerName_1>}
to
Get-ClusterGroup | Where-Object {$_.OwnerNode -EQ "ServerName_1"}
I tested this and it works as expected:
List the ClusterGrop Names-
Get-ClusterNode -Name "Server_name" | Get-ClusterGroup
Also,
List the ClusterGroup Services -
Get-ClusterNode -Name "Server_name" | Get-ClusterResource

PowerCLI Get-VM filter out blank IP Address

I need to filter out the blanks in the "IP Address" column of the below PowerCLI script but am having a hard time figuring it out.
Here's the core script.
Get-VM | select Name, #{N="IP Address";E={#($_.guest.IPAddress[3])}}, Guest, PowerState
Here's one of the scripts I've tried and failed at.
Get-VM | select Name, #{N="IP Address";E={#($_.guest.IPAddress[3])}}, Guest, PowerState | where-object { #{N="IP Address";E={#($_.guest.IPAddress[3])}} -ne "" }
Any help would be appreciated.
Thank you,
-Rob
You have already made the customer property. You can now use it directly for filtering
Get-VM | select Name, #{N="IP Address";E={#($_.guest.IPAddress[3])}}, Guest, PowerState | where-object{$_."IP Address"}
or
Get-VM | Where-Object{$_.guest.IPAddress[3]} | select Name, #{N="IP Address";E={#($_.guest.IPAddress[3])}}, Guest, PowerState
If the property is null or empty the that will evaluate to false. In your example you were evaluating the creation of hash table. First example checks your newly created IP Address property. Second checks the source data for that property. Either way they should evaluate the same.

How to get the default gateway from powershell?

If a computer has multiple gateways, how can I determine which is the default gateway using the PowerShell?
If you're on PowerShell v3, you can use Get-NetIPConfiguration e.g.:
Get-NetIPConfiguration | Foreach IPv4DefaultGateway
I think this will be more cross platform:
Get-NetRoute |
where {$_.DestinationPrefix -eq '0.0.0.0/0'} |
select { $_.NextHop }
You need to know which of the multiple gateways are used? If so. From what I remember, when multiple gateways are available the gateway with the lowest metric("cost" based on link speed) is used. To get this, run the following command:
Get-WmiObject -Class Win32_IP4RouteTable |
where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} |
Sort-Object metric1 | select nexthop, metric1, interfaceindex
if there are multiple default gateways with the same cost, I think it's decided using the binding order of the network adapters. The only way I know to get this is using GUI and registry. To include binding order you could save the output of the script over, get the settingsid from Win32_networkadapterconfiguration (identify using interfaceindex), and read the registry key HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Linkage\Bind. This key lists the binding order it seems, and the settingsid you get from win32_networkadapterconfiguration is the GUID they identify the device with. Then sort the gateways with equal metrics using their order in the Bind reg.key and you got your answer.
Explained in: Technet Social - NIC adapter binding
I found it as the below which lists all active gateways, correct me if I am wrong
(Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway
Use the WMI queries to pull the data that you're looking for. Below is a fairly simple example to pull the default gateway for a device specified in the first line variable. This will query the device for network adapters and display the found information (for each adapter) to the console window - pulls adapter index, adapter description, and default gateway
Shouldn't take much to expand this to process multiple devices, or process based on a list fed via an input file.
$computer = $env:COMPUTERNAME
Get-WmiObject win32_networkAdapterConfiguration -ComputerName $computer |
Select index,description,defaultipgateway |
Format-Table -AutoSize
$ActiveNet = Get-NetAdapter –Physical |Where-Object {$_.status -eq "Up"} | select name
$Network = Get-NetIPAddress |Where-Object EnabledDefault -EQ 2 | Where-Object InterfaceAlias -EQ $ActiveNet.name | Where-Object IPv4Address -NE $null | select *
$DefautGateway = Get-NetRoute -InterfaceIndex $Network.InterfaceIndex -DestinationPrefix "0.0.0.0/0" | select NextHop
$DefautGateway.NextHop