Monday, May 09, 2016

PowerShell : check script running on nano

If you are authoring scripts targeting Nano server specifically then there are two checks which you can bake into (maybe add them to the default nano authoring snippet in ISE) them.






Check the Operating System SKU


Query the Win32_OperatingSystem CIM class and check if the property named 'OperatingSystemSKU' is 143 (Datacenter) or 144 (Standard). As per the MSFT documentation for the CIM class :


PRODUCT_DATACENTER_NANO_SERVER (143)
Windows Server Datacenter Edition (Nano Server installation)
PRODUCT_STANDARD_NANO_SERVER (144)
Windows Server Standard Edition (Nano Server installation)

Something like below, you can be more creative with it :

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
#region Nano server check
Switch -Exact ($(Get-CimInstance -ClassName Win32_OperatingSystem).OperatingSystemSKU) {
    143 {
        Write-Verbose -Message 'Script Running on Windows Server Datacenter Edition (Nano Server installation)'
        break;
    }
    144 {
        Write-Verbose -Message 'Windows Server Standard Edition (Nano Server installation)'
        break;
    }
    default {
        Write-Warning -Message 'OperatingSystem SKU does not match Nano server.'  
        throw
    }
}
#enderegion


Check the $PSVersionTable


With the newer WMF 5.1 release, the $PSVersion hash table now has a key named 'PSEdition' added to it. This key's value is 'Core' on Nano server or IoT devices.



1 comment: