Закончилась необходимость писать больше, чем я хотел, и пришлось отказаться от попыток запустить его удаленно.Поэтому создал локальный скрипт и хотел поделиться им с любым, кто сталкивается с той же проблемой, редко, как это, вероятно, будет и должно быть.
Возможно, есть лучшие способы написать его, но он работает на данный момент.
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" }