Powershell: Check MAC Address in range

By YellowOnline on Friday 18 September 2015 14:23 - Comments (6)
Category: Powershell, Views: 2.861

This tiny script will check if a MAC address falls within a valid range. I hope you'll never need it, but I did (thank you VMware for restricting MAC Address ranges across ESX versions...).

It's a monster (isn't line 12 lovely?), I know. Suggestions to do this more elegantly - regex comes to mind - are welcome of course.

PowerShell:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Function Validate-MACRange ($MACaddressToValidate)
    {
    $ValidRangeFrom = '00:50:56:00:00:00'
    $ValidRangeTo   = '00:50:56:3F:FF:FF'
    $a = $MACaddressToValidate.Replace("-",":").Split(":")
    $b = $ValidRangeFrom.Replace("-",":").Split(":")
    $c = $ValidRangeTo.Replace("-",":").Split(":")
    $i = 0
    $Valid = $True
    Do
        {
        If ($([int]"0x$($a[$i])" -GE [int]"0x$($b[$i])") -And ([int]"0x$($a[$i])" -LE [int]"0x$($c[$i])") -EQ $False){$Valid = $False}
        $i++
        }
    Until ($i -EQ 6)
    Return $Valid;
    }


Het monstertje leeft...

Edit
Here's another approach that is faster (thanks dugo & even more erwin80):

PowerShell:
1
2
3
4
5
6
7
8
9
10
Function Validate-MACRange2 ($MACaddressToValidate) 
    {
    $ValidRangeFrom = '00:50:56:00:00:00'
    $ValidRangeTo   = '00:50:56:3F:FF:FF'
    $a = [long] ("0x"+$MACaddressToValidate.Replace(":","").Replace("-",""))
    $b = [long] ("0x"+$ValidRangeFrom.Replace(":","").Replace("-",""))
    $c = [long] ("0x"+$ValidRangeTo.Replace(":","").Replace("-",""))
    $Valid = $a -GE $b -AND $a -LE $c
    Return $Valid;
    }



The first method took 4456 ticks, the second 2807. Quite a difference.

Koken! Frangipanetaart nl

Door YellowOnline op zondag 13 september 2015 22:27 - Reacties (22)
Categorie: Cooking, Views: 3.930

Hier in Berlijn zijn bakkers nauwelijks te vinden en banketbakkers nog veel minder. Vrijwel overal waar men taart verkoopt is dit industrieel gebak. De beste plekken om taart of gebak te kopen zijn cafés die zelf bakken. Eigenlijk het tegendeel van België, waar bakkers de beste taarten hebben en cafés opgewarmde diepvriesproducten verkopen.

Enfin, ik ben niet naar dit land verhuisd omwille van de onbestaande culinaire hoogstandjes alhier. Gelukkig voor mijn Duitse vriendenkring heb ik met mezelf ook mijn keuken geïmporteerd. Vandaag (alweer) een Belgische klassieker die ondanks de naam weinig met Italië te doen heeft en niet te verwarren is met de Franse galette du roi (dat er wel naaste familie van is): de frangipanetaart.


Lees verder »

Powershell: Copying new and modified files only

By YellowOnline on Thursday 10 September 2015 14:45 - Comments (6)
Category: Powershell, Views: 3.319

Here's a quick function for people like me who work in environments without ROBOCOPY (yes, it's possible). Obviously this doesn't really compare to Robust File Copy, but it allows you to at least copy new and modified files only without dependencies.


PowerShell:
1
2
3
4
5
6
7
Function NotExactlyRobocopy ($SourceFolder, $TargetFolder)
    {
    $SourceFiles = @(Get-ChildItem -Path $SourceFolder)
    $TargetFiles = @(Get-ChildItem -Path $TargetFolder)
    $Update = Compare-Object $SourceFiles $TargetFiles -Property FullName, LastWriteTime, Length | Where-Object {$_.SideIndicator -EQ "<=" }
    $Update | ForEach-Object { Copy-Item -Path $_.Fullname -Destination $TargetFolder} 
    }



Disclaimer: Only tested with the -WhatIf switch that I removed from the code. Use at own risk.

It isn't recursive yet and doesn't take care of system files or hidden files, simply because I didn't need those when writing this. I'll make it better version tommorrow, but at least it gives you an idea of how to do it (adding parameters to Copy-Item isn't exactly hard).