Thursday, 24 September 2020

DHCP and Powershell

Powershell. Like all coding, it kinda makes sense when you can work through a command, although the chances of ever remembering the correct syntax whilst in the fray of a system meltdown seems remote. Like any foreign language, I suppose it's fine so long as you're in a situation of total immersion. For the rest of us, it's a phrase book all the way.

This morning's fun: trying to extract MAC addresses from a DHCP database so they can be set as reservations. I've done this before with Netsh but the details escape me. And I have that nagging feeling that I ought to be using Powershell, so...

To export leases:

Get-DHCPServerv4Lease -Computer <name> -ScopeID x.x.x.0 | Export-Csv <filename>

For "name" it really is just the name, no \\ required.

Importing can either be done on an individual basis in the console window, or via a .csv file.

The interactive command is

Add-DHCPServerv4Reservation -ScopeID x.x.x.0 -IPAddress x.x.x.x -ClientID "<MAC address>" -Description "<description>"

Note, the MAC address is the hyphenated version.

To do this via .csv, you need the headings "ScopeID", "IPAdress", "Name", "ClientID" and "Description" (typically of Microsoft, these are different names, and in a different order, from those in the export file - this may not be essential, but who knows?)

When you enter the MAC addresses and descriptions, you need the hyphenated address but no quotes.

Powershell command to import from a file:

Import-CSV -Path "<path>" | Add-DHCPServerv4Reservation -ComputerName "<fqdn>"

No, don't ask me why you can export from the NetBIOS name but you need the FQDN to import. It may be moot unless you have an enterprise setup, but I don't feel like testing it on a live server.

Hat-tip to Expert in the Cloud and Microsoft's own docmentation.


Saturday, 5 September 2020

Office

More fighting with Office on the new install, but I found something useful. I knew the default Ctrl-V paste had been set to "keep formatting" - this is also the first option if you right-click to paste from the menu - but I didn't realise you could change the default. File > Options > Advanced > Cut, Copy and Paste section. Options are Keep Source Formatting, Merge, or Keep Text.