Command/More/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"

    $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
        
        $adapterBinding = Get-NetAdapterBinding -InterfaceDescription $PlcSimVirtualAdapterDescription -DisplayName "Juniper Network Service"
        $co.JuniperServiceEnabled = $adapterBinding.Enabled
    }
    else
    {
        $co.ExistsAdapter = $false
    }
    return $co
}

function SetPlcSimAdapterData([string]$ipAddress, [int]$prefixLength)
{
    $adapter = GetPlcSimAdapter
    if ($null -ne $adapter) 
    {
        $interfaceIndex = $adapter.InterfaceIndex
        $ipInterfaceObject = Get-NetIPInterface -AddressFamily IPv4 -InterfaceIndex $interfaceIndex
        $dhcp = $ipInterfaceObject.Dhcp
        if($dhcp -eq 'Enabled')
        {
            $null = New-NetIPAddress -InterfaceIndex $interfaceIndex -IpAddress $ipAddress -PrefixLength $prefixLength
        }    
    }
}

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
if (-not (IsElevated))
{
    $thisFile = $myinvocation.MyCommand.Definition
    Start-Process powershell.exe -Verb RunAs -ArgumentList "`"$thisFile`" $args"
    exit
}
Write-Output "Set PlcSim Adapter Data . . ."
SetPlcSimAdapterData "192.168.0.254" "24"
Write-Output "Done"

Write-Output "Get PlcSim Adapter Data . . ."
$co = GetPlcSimAdapterData

Write-Output "ExistsAdapter: $($co.ExistsAdapter)"
Write-Output "DHCP: $($co.DHCP)"
Write-Output "IpAddress: $($co.IpAddress)"
Write-Output "PrefixLength: $($co.PrefixLength)"
Write-Output "JuniperServiceEnabled: $($co.JuniperServiceEnabled)"

Read-Host "Exit script ? "