Is there a way to activate windows remotely via powershell with smart card?

0

For some reason our domain doesn't have a KMS server setup. So we are forced to activate windows manually. This only works for 180 days however users start seeing the popups regarding windows is going to expire a couple weeks before that.

ICM -CN $CN {slmgr /fta <thumbprint> <pin>}    

I'm trying to find a way to remotely push this one liner that will activate windows however I'm receiving an error 0x8010000C which basically is just complaining the smart card isn't inserted in the remote computer.

So I'm about out of ideas other then getting a digital copy of the certificate used and installing it on every computer hoping it pulls from that and not from my card.

powershell
activation
asked on Stack Overflow Feb 6, 2019 by Wafflez19

1 Answer

0

Ended up having to write more than I wanted and had to give up trying to run it remotely. So created a local script and wanted to share this for anyone who runs into the same issue, rare as this will probably be and should be.

There are probably better ways to write it but it's working as of now.

function msgbox {
param (
    [string]$Message = 'Windows will expire soon. Would you like to renew the license key?',
    [string]$Title = 'Windows Activation Script',   
    [string]$buttons = 'YesNo'
)
# This function displays a message box by calling the .Net Windows.Forms (MessageBox class)

# Load the assembly
Add-Type -AssemblyName System.Windows.Forms | Out-Null

# Define the button types
switch ($buttons) {
   'ok' {$btn = [System.Windows.Forms.MessageBoxButtons]::OK; break}
   'okcancel' {$btn = [System.Windows.Forms.MessageBoxButtons]::OKCancel; break}
   'AbortRetryIgnore' {$btn = [System.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore; break}
   'YesNoCancel' {$btn = [System.Windows.Forms.MessageBoxButtons]::YesNoCancel; break}
   'YesNo' {$btn = [System.Windows.Forms.MessageBoxButtons]::yesno; break}
   'RetryCancel'{$btn = [System.Windows.Forms.MessageBoxButtons]::RetryCancel; break}
   default {$btn = [System.Windows.Forms.MessageBoxButtons]::RetryCancel; break}
}
  # Display the message box
  $script:Return=[System.Windows.Forms.MessageBox]::Show($Message,$Title,$btn)
}

function msgbox2 {
param (
    [string]$Message = "Windows has expired. The script will now attempt to activate Windows.`n`nPlease click ok to continue.",
    [string]$Title = 'Windows Activation Script',   
    [string]$buttons = 'ok'
)
Add-Type -AssemblyName System.Windows.Forms | Out-Null
switch ($buttons) {
   'ok' {$btn = [System.Windows.Forms.MessageBoxButtons]::OK; break}
}
  $script:Return=[System.Windows.Forms.MessageBox]::Show($Message,$Title,$btn)
}

# Grabs Users Email Certificate Thumbprint
function Grab_Thumbprint
{($EmailCert = (Get-ChildItem -path Cert:\CurrentUser\My | Where-Object { $_.FriendlyName -match 'Signature' } | Where-Object { $_.Subject -match ($env:UserName).Substring(0,4) } | Select-Object Thumbprint | ForEach-Object { $_.Thumbprint }))}

# Checks Experation Date of Licence
function Check_date
{($line = (slmgr /xpr | Out-String ))
($line2 = [regex]::Matches($line, '(\d+/\d+/\d\d\d\d)') | Select-Object Value | ForEach-Object { $_.Value })
if($line2 -match '(\d+/\d+/\d\d\d\d)'){
$line2 = Get-Date $line2 -f MM/dd/yyyy
$line2 = (Get-Date $line2).AddMonths(-1)
$script:Expired = (get-date $line2) -lt (get-date)} else {
($line3 = [regex]::Matches($line, 'Windows is in Notification mode') | Select-Object Value | ForEach-Object { $_.Value })
if($line3 -eq 'Windows is in Notification mode'){$script:Expired2 = $true} else {}}}

# Attempts to Activate Windows
function Activate
{Write-Host "This will take 10 seconds to access your CAC" -ForegroundColor Cyan
($test = (slmgr /fta $emailcert | Out-String))
$test2 = [regex]::Matches($test, 'Error: ..........') | Select-Object Value | ForEach-Object { $_.Value }
$test2 = $test2 + ' - Please notify your ITS of any errors listed here.'
$test3 = [regex]::Matches($test, 'Product activated successfully') | Select-Object Value | ForEach-Object { $_.Value }
[System.Windows.Forms.MessageBox]::Show("$test2`n$test3",'Windows Activation Status')}

# Clears any old data

$dateTime    = $null
$line        = $null
$line2       = $null
$line3       = $null
$EmailCert   = $null
$Expired     = $null
$Expired2    = $null
$test        = $null
$test2       = $null
$test3       = $null

# Do Stuff

$EmailCert = Grab_Thumbprint

Check_date

If($Expired -eq $true)    { (msgbox) }
If($Expired2 -eq $true)    { (msgbox2) }
If(($Expired -eq $true -or $Expired2 -eq $true) -and ($Return -eq 'Yes' -or $Return -eq 'OK'))     { (Activate) } else { Write-Host "Exiting" }
answered on Stack Overflow Feb 8, 2019 by Wafflez19 • edited Feb 14, 2019 by Wafflez19

User contributions licensed under CC BY-SA 3.0