Created
September 3, 2019 11:38
-
-
Save kpatnayakuni/6e99cd8bc1a31a5d3f357b83779a49d6 to your computer and use it in GitHub Desktop.
Get Windows Activation Status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function Get-WinSrvFromInv | |
| { | |
| <# | |
| The purpose of this function is to retrieve the list of servers for which you want to check the Windows Activation status. | |
| Write your piece of code to retirve the servers from your inventory. | |
| for example, Get-Content -Path $InvPath\Server.txt | |
| #> | |
| return @("Srv2K19", "Srv2K16", "Srv2K12") | |
| } | |
| Function Get-WindowsActivation | |
| { | |
| <# | |
| This is a PowerShell advanced function to rerieve the Windows Activation Status using CIM classes. | |
| It takes one or more servers as an input, and it accepts through pipeline as well | |
| This script will check the connectivity, and then checks the activation status | |
| It collects the infor from all the servers and then return the value of error at once. | |
| #> | |
| [CmdLetBinding()] | |
| Param | |
| ( | |
| [Parameter(Mandatory=$true,ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] | |
| [string[]] $ComputerName | |
| ) | |
| Begin | |
| { | |
| $ActivationStatus = @() | |
| } | |
| Process | |
| { | |
| foreach ($CN in $ComputerName) | |
| { | |
| $PingStatus = Test-Connection -ComputerName $CN -ErrorAction Stop -Count 1 -Quiet | |
| $SPL = Get-CimInstance -ClassName SoftwareLicensingProduct -ComputerName $CN -Filter "PartialProductKey IS NOT NULL" -ErrorAction Stop | |
| $WinProduct = $SPL | Where-Object Name -like "Windows*" | |
| $Status = if ($WinProduct.LicenseStatus -eq 1) { "Activated" } else { "Not Activated" } | |
| if ($PingStatus -ne $true) | |
| { | |
| $PingStatus = "No" | |
| $Status = "Error" | |
| } else { $PingStatus = "Yes" } | |
| $ActivationStatus += New-Object -TypeName psobject -Property @{ | |
| ComputerName = $CN | |
| Status = $Status | |
| IsPinging = $PingStatus | |
| } | |
| } | |
| } | |
| End | |
| { | |
| return $($ActivationStatus | Select-Object -Property ComputerName, IsPinging, Status) | |
| } | |
| } | |
| ## Invoke the functions. | |
| Get-WinSrvFromInv | Get-WindowsActivation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment