Wednesday, March 29, 2017

WFA Command to check ip addresses on a windows host

For a customer I'm currently writing a pretty advanced workflow to double check the entire iscsi environment.  One part is check if the windows host has enough iSCSI ip addresses.  In case of vmware that would be 1, in case of a physical machine that would be 2.

So parameters are :
- computername
- remote credentials
- network address
- network mask

Download the dar

Here is the code :


    [Parameter(Mandatory=$true,HelpMessage="Credential name to make a remote connection")]

    [Parameter(Mandatory=$true,HelpMessage="Network adddress")]

    [Parameter(Mandatory=$true,HelpMessage="Subnet mask")]

# helper functions to work with ipaddresses
function Get-NetworkAddress {
    param (
    $IpAddressBytes = $ip.GetAddressBytes()
    $SubnetMaskBytes = $Mask.GetAddressBytes()
    if ($IpAddressBytes.Length -ne $SubnetMaskBytes.Length) {
        throw "Lengths of IP address and subnet mask do not match."
        exit 0
    $NetworkAddressParts = @()
    for ($i=0;$i -le 3;$i++) {
        $NetworkAddressParts += $ipAddressBytes[$i]-band $subnetMaskBytes[$i]
    $NetworkAddressString = $NetworkAddressParts -Join "."
    return [IpAddress]$NetworkAddressString
function Test-IsInSameSubnet {
    param (
    $Network1 = Get-NetworkAddress -ip $ip1 -mask $mask1
    $Network2 = Get-NetworkAddress -ip $ip2 -mask $mask2
    return $Network1.Equals($Network2)

# remote function to get manufacturere
function getManufacturer(){
    return Invoke-Command -Session $session -ScriptBlock {(Get-WmiObject -Class Win32_ComputerSystem).Manufacturer}

# remote function to get all ipaddresses
function getIpAddresses(){
    return Invoke-Command -Session $session -ScriptBlock {
        [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() `
        | ForEach-Object { $_.GetIPProperties() } `
        | Select-Object -ExpandProperty 'UnicastAddresses';

$ErrorActionPreference = "stop"

    # get credentials
    $mycreds = Get-WfaCredentials $CredentialName
    # create remote ps session
    $session = New-PSSession -ComputerName $ComputerName -Credential $mycreds

    # is vm ? Set required interface counts
    $manufacturer = getManufacturer
    $isVm = $false
    $requiredVserverAddresses = 2
    if($manufacturer -match 'VMWare'){
        Get-WfaLogger -info -message  "[Host] This is a vm [$ComputerFQDN]"
        $requiredHostAddresses = 1
        Get-WfaLogger -info -message  "[Host] This is a physical machine [$ComputerFQDN]"
        $requiredHostAddresses = 2

    # has iscsi ip ?
    $iscsiIpList = @()
    $iscsiConnectionList = @()
    $ipAddresses = getIpAddresses

    # loop every ipaddres and check if in iscsi range
    foreach($ip in $IpAddresses){
        if($ip.IPv4Mask -ne ""){
            if(Test-IsInSameSubnet -ip1 $ip.Address -ip2 $NetworkAddress -mask1 $ip.IPv4Mask -mask2 $SubnetMask){
                Get-WfaLogger -info -message  "[host] IP $($ip.Address) is an iscsi address"

    # stop if iscsi count mismatch
    if($iscsiIpList.Count -ne $requiredHostAddresses){
        Get-WfaLogger -info -message  "[Host] Expected iscsi addresses [$requiredHostAddresses]"
        Get-WfaLogger -info -message  "[Host] Found iscsi addresses [$($iscsiIpList.Count)]"
        throw "[Host] iscsi IpAddress count mismatch"
        Get-WfaLogger -info -message  "[Host] iscsi IpAddress count correct [$requiredHostAddresses]"

    throw $_.Exception
        $out = Disconnect-PSSession $session -EA SilentlyContinue
        $out = Remove-PSSession $session -EA SilentlyContinue

1 comment :

  1. Thanks for sharing fabulous information. It' s my pleasure to read it.I am searching for vmware jobs in hyderabad location, this is useful for me.