ETidalgo
7/16/2018 - 11:21 PM

Get IP address for hyperv VM

function Get-VmIpAddresses ($VmName) {
  get-vmnetworkadapter -vmname "$VmName"  | % { $_.IpAddresses }
} 

function Get-VmIpv4Address ($VmName) {
  get-vmnetworkadapter -vmname "$VmName"  | % { $_.IpAddresses } | ? { $_ -notmatch ":" } | select -first 1
} 

## Get IPV4 and IPV6
"etidos" | % { 
  $baseName = $_ ; "app","data" | % { 
    $vmName = "$baseName$_" ; write-host $vmName ; Get-Vm-IpAddresses $vmName 
  }
}

## Alternate get IPv4 only
"etidos" | % { 
  $baseName = $_ ; "app","data" | % { 
    $vmName = "$baseName$_" ; write-host $vmName ; Get-Vm-IpAddresses $vmName | ? { $_ -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' } 
  }
}

function Test-Administrator  
{  
    $user = [Security.Principal.WindowsIdentity]::GetCurrent();
    (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)  
}