Tag: Windows server 2012

Prevent write-output to perform linebreak

Sometimes you want to output some text and append an variable. Problem is that write-output usually performs a linebrak when appending.

$text = "some text."
Write-Output "this is " $text

Output will be:
this is
some text.

There are two ways you can solve this. One is to append the text to the variable before doing write-output. Second is to put the variable inside the quotation marks.

#Example 1
$text = "some text."
$textmessage = "This is " + $text
Write-Output $textmessage

#Example 2
$text = "some text."
Write-Output "this is $($text)"

Configure DHCP server with cmd or powershell

I wanted to share real quick how you set up and configure a DHCP server on Windows Server 2008 R2 or 2012 (R2) with he command line or powershell.

Windows Server 2008 R2:
Let’s begin with Windows Server 2008 R2, which doesn’t have any dhcp cmdlets in powershell so we have to mostly stick to the commandline.

To install the DHCP role, open an elevated powershell window:
Import-module ServerManager
Add-windowsfeature -Name DHCP

Note that the DHCP Server service is installed but stopped and disabled. Now we have to go to an elevated command prompt for the rest.To set the service to automatic startup and start the service.
sc config dhcpserver start= auto
net start dhcpserver

Authorize the server in AD:
netsh dhcp add server Server1.dhcplab.local

Create a scope, which is empty
netsh dhcp server add scope "Scope1"

Add a range of IP addresses for leasing
netsh dhcp server scope add iprange

Set the default gateway option
netsh dhcp server scope set optionvalue 003 IPADDRESS

Set the lease time in seconds
netsh dhcp server scope set optionvalue 51 DWORD 28800

Set the DNS server
netsh dhcp server scope set optionvalue 006 IPADDRESS

Add a DHCP reservation, for a printer in this case
netsh dhcp server scope add reservedip A1B2C3D4E5F6 "Printer1" "Reservation for Printer1"

Windows Server 2012
Everything in an elevated powershell window. How sweet it is! 🙂

Install the DHCP Role (no need to configure the service afterwards)
add-windowsfeature -Name DHCP -includemanagementtools

Authorize the DHCP server

Add scope with IP range for leasing
Add-DhcpServerv4Scope -Name "Scope2" -StartRange -EndRange -SubnetMask

Set lease duration (days.hours:minutes:seconds)
Set-DhcpServerv4Scope -ScopeId -LeaseDuration 1.00:00:00

Set default gateway
Set-DhcpServerv4OptionValue -ScopeId -Router

Set dns server and domain name
Set-DhcpServerv4OptionValue -DnsServer -DnsDomain dhcplab.local

Set IP reservation, again for a printer
Add-DhcpServerv4Reservation -ScopeId -IPAddress -ClientId A1-B2-C3-D4-E5-F6 -Description "Reservation for Printer1" -Name "Printer1"

How to easily boot into safe mode

In Windows Server 2012 the shutdown.exe command has a new switch, -o, which lets you control the behaviour of the next reboot. You can use this to boot straight into the boot menu and then select safe mode, last known good etc

Backup Hyper-V with Windows Server Backup 2012

Backup VM
Backuptarget = drive letter to backup destination (not folder)
VM Host name = identical with VM name in hyperv console

wbadmin start backup -backuptarget:[drive letter] -hyperv:[VM name] -quiet

RESTORE VM to a new server
Backuptarget = UNC path to data location on old server
Recoverytarget = path on new server

C:\>wbadmin get versions -backuptarget:\\[ip]\[share]
C:\>wbadmin get items -backuptarget:\\[ip]\[share] -version:[versionID]
C:\>wbadmin start recovery -itemtype:hyperv -items:[VM identifier] -backuptarget:\\[ip]\[share] -recoverytarget:[path] -alternatelocation -version:[versionID]