I'm working on a Wake-On-Lan script and I have this code that is working I am just trying to understand it better.

Here is an excerpt that I am having trouble with:

$mac = "78ab78ab78ab" #some user input mac address reformatted to something like this one
$packet = [byte[]](,0xFF * 102)
6..101 |% { $packet[$_] = $mac[($_%6)] }

[byte[]](,0xFF * 102)

This is saying to make an array of byte values 0xFF 102 times correct? So packet[0] should equal 0xFF through packet[101] = 0xFF

6..101 |% { $packet[$_] = $mac[($_%6)] }

This is a loop that operates on an element of the $packets array we declared earlier based on the iterator ($_) that grows by one as it progresses through the loop. I'm not sure what the $_%6 does in particular the %6 operator. I have ran through the loop and output the value (which does change) but since I'm not familiar with the operator I'm not sure what the output really means.

有帮助吗?

解决方案

I think your main question is around $_%6. You should read it as $_ % 6, where $_ and 6 are your operands, and % in Powershell is a binary Mod operator from VB.NET world. It returns a remainder of integer division. So, for example 5 Mod 2 = 1.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top