Category Archives: Scripting

Desired State Configuration – A very basic intro

I’ve seen this topic before when reviewing the 70-410 exam books, though for that exam very little is mentioned or indeed required. So until now I’ve given it scant notice. However, having read more about it I can see it will continue to have a growing use in the years ahead, as my requirement to provision multiple standardised servers grows. DSC really looks like it can replace the need for storage-heavy VM templates and multiple GPOs and can quickly deploy standardised servers and maintain the initial settings in the event they are changed by well-meaning, but meddling System Administrators.

As I have only just dipped my toe into the DSC ocean, this post is really as much for my benefit as anyone eases (though I’m always glad to see you of course). It’s just a basic step-through to create a MOF file and apply that to the target. I don’t mention any of the underlying concepts and terminology because there’s a plethora of information out there on Technet and beyond.

Task: I need to ensure a new directory is created on my domain controller (CIV-DC1)

Create the Configuration file:

Configuration AccountingDir {

Import-DscResource –ModuleName ‘PSDesiredStateConfiguration’

Node CIV-DC1 {
#create a new directory in the C drive called accounting

File Accounting { 
Type = “Directory”
Ensure = “Present”
DestinationPath = “C:\Accounting” }

} #Node-complete

} #configuration-complete

#run this to create the MOF file
#the name of the configuration file
AccountingDir -OutputPath c:\temp

#run this to apply the MOF file to the target
Start-DscConfiguration -path C:\temp -Wait -Verbose -Force

NB: The image below will be used during the next section, I used the PowerShell ISE:


NB: Please note line 3, when I did not have this I got the following error:


Step 1: Load the Configuration Function Into Memory

Select the Configuration text and run this in ISE

Step 2: Generate the MOF file

Highlight the command (the name of the Configuration and specify a location where therMOF file will be stored) and run this in ISE, you should get the following output:


Step 3: Apply the MOF settings to the target

Highlight the Start-DscConfiguration line, which includes the location of the MOF file (you don’t stipulate the actual MOF file) and run in ISE. The target for the MOF file is stipulated in the first lines of the MOF file so PowerShell and LCM know what the target is. If it is successfully applied you will see the following:


Visually checking on CIV-DC1 shows the new directory:


You can also run a test to confirm if the settings in the MOF file are still active/applied on the target using the Test-DscConfiguration command:



Disclaimer: provided “AS IS” with no warranties and confer no rights


PS Script: Form to get MAC Address (v1.0)

Description: Enter the name of a computer and get it’s MAC address. The form is still a work in progress, with some of the functions and results needing tightened up and the form is ugly.

IMPORTANT: Remember if you copy and paste this text I often find the format of the quotation marks gets changed so the script becomes corrupt and fails.


#region start declare .NET FUNCTIONALITY
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
Add-Type -AssemblyName System.Windows.Forms
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null
$psexec = ‘\\<computer name>\share$\PSTools\PsExec.exe’
#endregion end declare .NET FUNCTIONALITY


#region FUNCTION #0 Get computer MAC from ARP table – not used in this script
Function arpcheck () {
$IP = [System.Net.Dns]::GetHostByName($computer).AddressList[0].IpAddressToString #the IP of the computer I need to get the MAC for.
$mac = arp -a
($mac | ? { $_ -match $ip } ) -match “([0-9A-F]{2}([:-][0-9A-F]{2}){5})” | out-null;

if ( $matches ) {


} else {

“Not Found”

#endregion FUNCTION #0 Get computer MAC from ARP table

#region FUNCTION #1 Display Form
Function showform () {
[void] $Form.ShowDialog()
#endregion FUNCTION #1 Display Form

#region FUNCTION #2 Conversion and Display

function get-mac2 ()
$computer = $ECN.text

#test if WinRM is enabled on the remote computer, this script requires WinRM is enabled.
$winrm = test-wsman -ComputerName $computer -ErrorAction SilentlyContinue
if ($winrm)

$VPNTC = Test-Connection $computer -Count 1 -ErrorAction SilentlyContinue
$VPNIP = ($VPNTC.IPV4Address).IPAddressToString
If ($VPNIP -match “92.*”){$MAC.text=”ERROR:VPN Reply”} #a test/result if I’m on my VPN and the computer is offline, an active ping response is still returned by the VPN starting with 92.
$TC = Test-Connection -count 1 $computer -Quiet
# If the PC is online then get the InterfaceIndex ID of the active NIC
If ($TC -eq “True”){
$result1 = Invoke-Command -ComputerName $computer {(get-wmiobject win32_networkadapter -filter “netconnectionstatus = 2″).InterfaceIndex | out-string}
$result1 = $result1.Trim()
# If multiple NICs are active then this script won’t work – it still a work in progress 🙂
If ($result1.length -lt 3) {
$result2 = Invoke-Command -ComputerName $computer {(gwmi Win32_NetworkAdapter | where {$_.InterfaceIndex -eq $using:result1}).MACAddress}
Else {$MAC.text=”ERROR:Multi MACs”}
Else {$MAC.text=”ERROR:PC Offline”}
Else {$MAC.text= “WinRM Disabled”}
#endregion FUNCTION #2 Conversion and Display

#region FUNCTION #3 Copy to clipboard
Function ClipMe() {
#endregion FUNCTION Copy to clipboard

#endregion FUNCTIONS

#region FORM

$Form = New-Object System.Windows.Forms.Form
$form.text = “MAC Address Finder”
$Form.Size = New-Object System.Drawing.Size(400,300)
$Form.Font = “Segoe UI,12”
$Form.ForeColor = [System.Drawing.Color]::White
$Form.BackColor = [System.Drawing.Color]::LightSteelBlue
#endregion FORM ELEMENT #1 The basic form


$groupConvert = New-Object System.Windows.Forms.GroupBox
$groupConvert.Location = New-Object System.Drawing.Size(10,20)
$groupConvert.size = New-Object System.Drawing.Size(360,170)
$groupConvert.text = “Enter Computer Name:”
#endregion FORM ELEMENT #2 group boxes


$cnlabel = New-Object System.Windows.Forms.Label
$cnlabel.Text = “Enter Computer Name:”
$cnlabel.AutoSize = $True
$cnlabel.Font = “Segoe UI,16”
$cnlabel.Location = “20,20”

$ECN = New-Object System.Windows.Forms.Textbox
$ECN.Size = New-Object System.Drawing.Size(135,100)
$ECN.Font = “Segoe UI,16”
$ECN.Location = “20,60”
$ECN.TextAlign = “Center”

$malabel = New-Object System.Windows.Forms.Label
$malabel.Text = “MAC Address:”
$malabel.AutoSize = $True
$malabel.Font = “Segoe UI,16”
$malabel.Location = “20,120”

$MAC = New-Object System.Windows.Forms.Textbox
$MAC.Size = New-Object System.Drawing.Size(185,100)
$MAC.ReadOnly = $true
$MAC.Font = “Segoe UI,16”
$MAC.Location = “170,120”
$MAC.TextAlign = “Center”

#endregion LABELS and TEXTBOXES


#region BUTTON #1 Create Convert button
$ConvertButton = New-Object System.Windows.Forms.Button
$ConvertButton.Location = New-Object System.Drawing.Size(20,200)
$ConvertButton.Size = New-Object System.Drawing.Size(80,40)
$ConvertButton.Text = “Get MAC”
$ConvertButton.Font = “Segoe UI,12″
$ConvertButton.BackColor = [System.Drawing.Color]::Green
$ConvertButton.Enabled = $True
$Closebutton.Text = “Exit”
$buttonCopyToClipboard.Enabled = $true
#endregion BUTTON #1 Create Convert button

#region BUTTON #2 Create CANCEL button
$Closebutton = New-Object System.Windows.Forms.button
$Closebutton.Location = New-Object System.Drawing.Size(280,200)
$Closebutton.Size = New-Object System.Drawing.Size(80,40)
$Closebutton.Text = “Cancel”
#endregion BUTTON #2 Create CANCEL button

#region BUTTON #3 Create CLIPBOARD button
$buttonCopyToClipboard = New-Object System.Windows.Forms.Button
$buttonCopyToClipboard.Location = New-Object System.Drawing.Size(110,200)
$buttonCopyToClipboard.Size = New-Object System.Drawing.Size(160,40)
$buttonCopyToClipboard.Text = “Copy to clipboard”
#$buttonCopyToClipboard.Font = “Segoe UI,12”
$buttonCopyToClipboard.Enabled = $False
#call the function Clipme to copy the MAC address to the system clipboard
#endregion BUTTON #3 Create CLIPBOARD button

#region BUTTON #4 Create Clear button
$Clearbutton = New-Object System.Windows.Forms.button
$Clearbutton.Location = New-Object System.Drawing.Size(180,59)
$Clearbutton.Size = New-Object System.Drawing.Size(80,40)
$Clearbutton.Text = “Clear”
$buttonCopyToClipboard.Enabled = $False
#clears the text from both fields
#endregion BUTTON #4 Create CLEAR button

#endregion BUTTONS

#endregion FORM

#region SHOW FORM
#endregion SHOW FORM

Script: Close form after certain time

I use this to create time-limited splash screens I then display during various other scripts I run to keep the end user informed. The beauty of this one is that is there is no DialogResult i.e OK, Yes or Cancel when the form is closed, therefore no annoying pop-up messages when the script is run in GUI-only mode (text corrected 09/05/17).
function Splash1 {
$form1 = New-Object ‘System.Windows.forms.form’
$label1 = New-Object ‘System.Windows.Forms.Label’
$timer1 = New-Object ‘System.Windows.Forms.Timer’
$InitialFormWindowState = New-Object ‘System.Windows.Forms.FormWindowState’
$form1_Load = {
$TotalTime = 5 #in seconds
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
#Start the timer
#Use Get-Date for Time Accuracy
[TimeSpan]$span = $script:StartTime – (Get-Date)
#Update the display
if ($span.TotalSeconds -le 0) {
#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
#Remove all event handlers from the controls
catch [Exception]
{ }
# form1
$form1.ControlBox = $false
$form1.StartPosition = “CenterScreen”
$form1.text = “BitLocker Installation”
$form1.Size = New-Object System.Drawing.Size(300,100)
$form1.Font = “Segoe UI,12”
$form1.Topmost = $True
$form1.ForeColor = [System.Drawing.Color]::White
$form1.BackColor = [System.Drawing.Color]::LightSteelBlue
# label1
$label1.Text = “Checking System…”
$label1.AutoSize = $True
$label1.Font = “Segoe UI,18”
$label1.Location = “42,15”
# timer1
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
#Clean up the control events
#Show the Form
return $form1.ShowDialog()
#display form
Splash1| Out-Null
PS – Not my own work, I amended a script I found on the web, I just can’t remember where I found it. If I find the link I’ll post it here. Credit where credit’s due after all…

Locate VMware snapshots with PowerCLI

PowerCLI is a scripting toolkit for vSphere and vCenter within a PowerShell console. You can use it to manage and report on vSphere and vCenter servers.

Find v5 here (requires a My VMware login) or search for latest version. Launch the installer, no reboot is required.

Using PowerCLI
Once installed look for the following icon in the Apps screen.


This will launch the PowerCLI console (run as a user that has admin rights on the VMware environment. In mine LDAP authentication is enabled so I run the PowerCLI as a domain admin account.

PowerCLI Commands

connect-VIServer -server <server-fqdn> (This is required before you can run any commands. It will always try to to use SSO for connection to a vCenter server, not for ESXi hosts)

disconnect-VIServer * or Disconnect-VIServer <server-fqdn>

$global:DefaultVIServer use this command to determine what host you are connected to.

get-datacenter Get the datacenter for the the current connection

get-cluster Get the cluster for the the current connection

get-vm | Get-Snapshot | select VM,Name,Created | sort created Find all VMware snapshots that have been deteled/released once the backup is complete

get-vm | Get-Snapshot | select VM,Name,Created | Where {$ -Like “smvi*”} | sort created Find all NetApp snapshots that have been deteled/released once the backup is complete

Get-VM | Export-CSV -Path <filename.csv> Export data