Command/PlcSim/GetPlcSimAdapterData.ps1


# Useful command:
# get-netipconfiguration | Format-Table -Property InterfaceAlias,InterfaceDescription,IPv4Address

# function ListNetworkAdapters()
# {
# $adapters = Get-NetAdapter
# #$virtualAdapters = $adapters | Where-Object Virtual -eq $true
# $virtualAdapterNames = $adapters | Select-Object Name,InterfaceDescription,Virtual,Status
# $table = $virtualAdapterNames | Format-Table -Property Name,InterfaceDescription,Virtual,Status
# Write-Output $table
# }

# function WriteIpAddressOfPhysicalNetworkAdapter() {
# $adapters = Get-NetAdapter
# [array]$physicalAdapters = $adapters | Where-Object {
# $_.Virtual -eq $false }

# foreach($adapter in $physicalAdapters)
# {
# $adapterName = $adapter.Name
# $adapterInterfaceDescription = $adapter.InterfaceDescription

# $interfaceIndex = $adapter.InterfaceIndex
# $ipAddress = Get-NetIPAddress -InterfaceIndex $interfaceIndex

# $ipv4Address = $ipAddress.IPv4Address

# Write-Output "Name: $adapterName"
# Write-Output " InterfaceDescription: $adapterInterfaceDescription"
# Write-Output " IPv4 Address: $ipv4Address"
# }
# }

[string] $global:PlcSimVirtualAdapterDescription = "Siemens PLCSIM Virtual Ethernet Adapter"

function CoCreate()
{
    [PsCustomObject]$co = @{}
    return $co
}
function CoAddItem($co, $name, $value)
{
    Add-Member -InputObject $co -NotePropertyName $name -NotePropertyValue $value
}
function CoRemoveItem($co, $name)
{
    $co.psobject.properties.remove($name)
}
function CoHasItem($co, $name)
{
    return [bool]($co.psobject.properties.match($name).Count -gt 0)
}
function CoGetItems($co)
{
    return $co.psobject.properties.name
}


function GetPlcSimAdapterData()
{
    $co = CoCreate
    CoAddItem $co "ExistsAdapter"
    CoAddItem $co "DHCP"
    CoAddItem $co "IpAddress"
    CoAddItem $co "PrefixLength"
    CoAddItem $co "JuniperServiceEnabled"
    CoAddItem $co "NetAddresses"

    $adapters = Get-NetAdapter
    [array]$virtualSiemensAdapters = $adapters | Where-Object {
        $_.InterfaceDescription -eq $PlcSimVirtualAdapterDescription }
    $count = $virtualSiemensAdapters.Count
    if ($count -eq 1) 
    {
        $co.ExistsAdapter = $true
        $adapter = $virtualSiemensAdapters[0]
        $interfaceIndex = $adapter.InterfaceIndex
        $ipInterfaceObject = Get-NetIPInterface -AddressFamily IPv4 -InterfaceIndex $interfaceIndex
        $dhcp = $ipInterfaceObject.Dhcp
        $ipAddressObject = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $interfaceIndex
        $ipAddress = $ipAddressObject.IPv4Address
        $prefixLength = $ipAddressObject.PrefixLength
        $co.DHCP = $dhcp
        $co.IpAddress = $ipAddress
        $co.PrefixLength = $prefixLength
        $co.NetAddresses = [array]$ipAddressObject
        
        $adapterBinding = Get-NetAdapterBinding -InterfaceDescription $PlcSimVirtualAdapterDescription -DisplayName "Juniper Network Service"
        $co.JuniperServiceEnabled = $adapterBinding.Enabled
    }
    else
    {
        $co.ExistsAdapter = $false
    }
    return $co
}

function GetPlcSimAdapter()
{
    $adapters = Get-NetAdapter
    [array]$adapters = $adapters | Where-Object {
        $_.InterfaceDescription -eq $PlcSimVirtualAdapterDescription }
    $count = $adapters.Count
    if ($count -eq 1) 
    {       
        $adapter = $adapters[0]
        return $adapter
    }
    return $null
}

Import-Module CmxModule -Force
Write-Host "Get PlcSim Adapter Data . . ."
Write-Host "InterfaceDescription: $PlcSimVirtualAdapterDescription"
$co = GetPlcSimAdapterData
Write-Host "ExistsAdapter: $($co.ExistsAdapter)"
Write-Host "JuniperServiceEnabled: $($co.JuniperServiceEnabled)"
Write-Host "DHCP: $($co.DHCP)"
Write-Host "IPAddressCount: $($co.NetAddresses.Count)"
Write-Host "IPAddress `t`t SubnetMask"
foreach($netAddress in $co.NetAddresses)
{
    Write-Host "$($netAddress.IPv4Address) `t`t $($netAddress.PrefixLength)"
}

# Do some checks and report them to the user.
if($co.JuniperServiceEnabled)
{
    Write-Warning "The JuniperService is currently enabled, but it must be disabled. Otherwise you may not be able to find the simulation instance within TIA Portal."
}
if($co.NetAddresses.Count -gt 1)
{
    Write-Warning "The network adapter contains several IPv4 addresses. Make sure that only a single IP address exists and set this IP address to 192.168.0.254 /24 with subnet mask 255.555.255.0."
}
if($co.NetAddresses.Count -lt 1)
{
    Write-Warning "The network adapter contains no IPv4 address. Make sure that only a single IP address exists and set this IP address to 192.168.0.254 /24 with subnet mask 255.555.255.0."
}


Read-Host "Exit script ? "