Powershell create multicast listener

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
User avatar
sekou2331
Posts: 249
Joined: Wed Aug 15, 2012 6:07 am

Powershell create multicast listener

Post by sekou2331 » Thu Nov 16, 2017 4:21 pm

I am looking for a way to test various multicast groups on windows server. Is there a way to write a powershell script to bind to a specific interface and attempt to consume data from a udp source? Right now I am using something called mdump.exe but it is not working and I am looking to write an alternative so that I can have more control.

User avatar
jvierra
Posts: 11720
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell create multicast listener

Post by jvierra » Thu Nov 16, 2017 4:51 pm

Search for c# examples of how to open a UDP port as a listener. There are quite a few out there.

User avatar
jvierra
Posts: 11720
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell create multicast listener

Post by jvierra » Thu Nov 16, 2017 4:53 pm

Here is one that is fairly complete: http://www.jarloo.com/c-udp-multicasting-tutorial/

User avatar
sekou2331
Posts: 249
Joined: Wed Aug 15, 2012 6:07 am

Re: Powershell create multicast listener

Post by sekou2331 » Thu Nov 16, 2017 4:56 pm

Are you saying there is no way of writing this in powershell?

User avatar
jvierra
Posts: 11720
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell create multicast listener

Post by jvierra » Thu Nov 16, 2017 5:14 pm

Convert the C# example to PowerShell.

User avatar
mxtrinidad
Site Admin
Posts: 53
Joined: Sun Mar 03, 2013 12:42 pm

Re: Powershell create multicast listener

Post by mxtrinidad » Thu Nov 16, 2017 5:18 pm

Yes! You can write it with PowerShell, as it's a .NET technology, you can consume .NET code snippet as JVierra show you in the link.

Please read the Get-Help on Add-Type which show you how to do this.

Get-Help Add-Type -showwindow

This is one way to extend PowerShell using .NET Framework components and use by many in the community.
You can be very creative.

:)

User avatar
jvierra
Posts: 11720
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell create multicast listener

Post by jvierra » Thu Nov 16, 2017 5:52 pm

Here is a quick linear conversion:

Code: Select all


function StartListener{
    Param(
        [Parameter(Mandatory)]
        $IPAddress,
        [Parameter(Mandatory)]
        $Port
    )
    
    $client = [System.Net.Sockets.UdpClient]::New()
    
    $client.ExclusiveAddressUse = $false;
    $localEp = [System.Net.IPEndPoint]::New([IPAddress]::Any, $Port);
    
    $client.Client.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::Socket, [System.Net.Sockets.SocketOptionName]::ReuseAddress, $true);
    $client.ExclusiveAddressUse = $false;
    
    $client.Client.Bind($localEp);
    
    $multicastaddress = [IPAddress]::Parse($IpAddress);
    $client.JoinMulticastGroup($multicastaddress);
    
    [Console]::WriteLine('Listening this will never quit so you will need to Ctrl-Break it');
    
    while ($true) {
        [Byte[]]$data = $client.Receive([ref]$localEp);
        $strData = [Encoding.Unicode]::GetString($data);
        [Console]::WriteLine($strData);
    }
}

User avatar
sekou2331
Posts: 249
Joined: Wed Aug 15, 2012 6:07 am

Re: Powershell create multicast listener

Post by sekou2331 » Mon Nov 20, 2017 11:01 am

Looking at this I am getting the error below. Also was looking into this and I wanted to know how would i force a interface. I see the MulticastOption looks to be the way to have a specific interface subscribe to a specific multicast group

Method invocation failed because [System.Net.Sockets.UdpClient] does not contain a method named 'New'.
+ $client = [System.Net.Sockets.UdpClient]::New()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException


Powershell version:
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1

User avatar
jvierra
Posts: 11720
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell create multicast listener

Post by jvierra » Mon Nov 20, 2017 11:33 am

"new" only works in PS 5.

$client = New-Object System.Net.Sockets.UdpClient

User avatar
sekou2331
Posts: 249
Joined: Wed Aug 15, 2012 6:07 am

Re: Powershell create multicast listener

Post by sekou2331 » Mon Nov 20, 2017 1:41 pm

Ok so I changed the code to work with Powershell version 4. Is it suppose to outputing gibberish? I think the encoding is incorrect.

Code: Select all

function StartListener{
    Param(
        [Parameter(Mandatory)]
        $IPAddress,
        [Parameter(Mandatory)]
        $Port
    )
    
    $client = New-Object System.Net.Sockets.UdpClient
    $client.ExclusiveAddressUse = $false;
    $localEp =  New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any,$port)  
      
    $client.Client.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::Socket, [System.Net.Sockets.SocketOptionName]::ReuseAddress, $true);
    $client.ExclusiveAddressUse = $false;
    
    $client.Client.Bind($localEp);
    
    $multicastaddress = [IPAddress]::Parse($IpAddress);
    $client.JoinMulticastGroup($multicastaddress);
    
    [Console]::WriteLine('Listening this will never quit so you will need to Ctrl-Break it');
    
    while ($true) {
        [Byte[]]$data = $client.Receive([ref]$localEp);
        $strData = New-object System.Text.UnicodeEncoding
        $strData.GetString($data)
        [Console]::WriteLine($strData.GetString($data));
    }
}

Locked