Managing Core Server with Powershell

A Core Server saves a huge amount of resources overall, but also intimidates many using it having to use only commands. and often managed from another server adding it to Server Manager, but knowing how to Administrate a Server with Powershell makes you feel .. well, powerful. I hope to give you a head start with these scripts, making it easier to manage. 

VMWare Tools

.\setup64.exe /s /v “/qn reboot=r”

Windows Activation

First we need to make sure Windows Server is activated with a valid product key. 

  • Using cmd will suffice, works either way using the native slmgr.vbs script.

slmgr /xpr for activation status

For activating Windows, type:

slmgr.vbs -ipk 12345-12345-12345-12345-12345 (using your product key)
slmgr.vbs -ato

In case you do not have a product key, you can use use slmgr /dli for current grace period, and slmgr /rearm to extend the grace period (if possible). slmgr /? gives you more options.

Powershell Setup

Before Powershell will be really useful, we should set up the appropriate working environment. Powershell version should be at least 5.1, which will be the case for currently supported versions. Use $psversiontable to display the current version. Version 5.1 was released in January 2017.

I like to throw off a few commands to set up a working environment and lighten the workload for future use. The below script will set the execution policy and a working folder and profile. Using the profile.ps1 start-up routines can be run when launching Powershell. Type $Profile for current profile in use. It also updates Help, which should not be underestimated, Help <command> -examples is extremely useful when you need the exact syntax.

-ExecutionPolicy Unrestricted is the least restricted setting, which allows all scripts. Consider -RemoteSigned for running native Microsoft cmdlets.

Policy Setting

Powershell behaviour can be set in the domain or local policies.

  • For domain policy, type gpmc.msc
  • For local policy, type gpedit

The settings are the same. Computer Configuration -> Administrative Template -> Windows Components -> Windows Powershell.

Repository

Let us set up the PSGallery repository. After a repository is registered, you can reference it from the Find-Module, Install-Module, and Publish-Module cmdlets. The registered repository becomes the default repository in Find-Module and Install-Module.

There are other repositories, such as NuGet which we will use later on.

Default settings

Set-PSRepository -Name “PSGallery” -InstallationPolicy Trusted

PSGallery is now trusted

Do not use more repositories than needed, if both PSGallery and PowershellModules are registered, you will have to specify which repository you want the module installed from.

Windows Update

Having the PSGallery repository set up, it is easy to update Windows, using the Windows Update Powershell Modules from PSGallery

Install-Module PSWindowsUpdate
Get-Module

A simple Update routine

Get-WUInstall -AcceptAll -Verbose -Install -AutoReboot

Install a specific KB with Get-WUInstall -KBArticleID KB4534307 -AcceptAll -ForceInstall

Get-WUHistory to get a list of installed updates.

For more options. Get-WuInstall -Full -Examples will get you enough reading for the day, with a huge load of options

Network Configuration

$IP = "192.168.1.141"
$MaskBits = 24 # This means subnet mask = 255.255.255.0
$Gateway = "192.168.1.254"
$Dns = "192.168.1.141", "192.168.1.254"
$IPType = "IPv4"
# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
 $adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
}
If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
 $adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
}
 # Configure the IP address and default gateway
$adapter | New-NetIPAddress `
 -AddressFamily $IPType `
 -IPAddress $IP `
 -PrefixLength $MaskBits `
 -DefaultGateway $Gateway
# Configure the DNS client server IP addresses
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS

Hint: On a Core Server, bring up Notepad or or similar for the script, save it before running.

Computername

$env:computername to see the current computer name.

Rename-computer -newname <computername> to change it.

Server Configuration

Type: sconfig.cmd

Domain Controller

Let’s add the Core Server as the 2nd Domain Controller (BDC in old terms, ie. Backup Domain Controller)

Install-WindowsFeature AD-Domain-Services –IncludeManagementTools

  • Reboot after installation.

Install-ADDSDomainController -DomainName virak.local -InstallDNS:$True –Credential (Get-Credential)