add initial windows activator script (#2)

This commit is contained in:
Carlos Sousa 2023-05-31 15:54:01 +02:00 committed by GitHub
parent bbc7a5a811
commit 680c3af9a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,24 @@
# Windows Activation Script
Read lists of targets and an activation key from json files, then for each target change the key and re-enable Windows activation.
## Note
Check the json files before running the script. Make sure you have a valid key for the targets you want to apply the script to.
### Error handling
At the beginning, the script requests the credentials to be used. If an error occurs (command invalid, not enough permissions, WinRM not enabled on the target computer, etc.), the script aborts to avoid locking out a user due to too many failed attempts.
### json file example
```
{
"windows_key" : [ 12345-12345-12345-12345 ],
"targets" : [
"pc1.domain.tld",
"pc2.domain.tld",
]
}
```

View File

@ -0,0 +1,6 @@
{
"windows_key": "12345-12345-12345-12345-12345",
"targets": [
"pc01.domain.tld"
]
}

View File

@ -0,0 +1,8 @@
{
"windows_key": "12345-12345-12345-12345-12345",
"targets": [
"pc01.domain.tld",
"pc02.domain.tld",
"pc03.domain.tld"
]
}

View File

@ -0,0 +1,36 @@
cls
# Load JSON Configuration
$jsonConfig = Get-Content -Path .\jobs\*.json -Raw | ConvertFrom-Json
$credential = Get-Credential
foreach ($input_file in $jsonConfig){
foreach ($target in $input_file.targets) {
Write-Output "Connecting to $target..."
# Initialize PowerShell Session
try {
$session = New-PSSession -ComputerName $target -Credential $credential
} catch {
Write-Output "Failed to establish a connection to $target. Check if the server is running and accessible. Error: $_"
exit
}
# Execute command and capture the output
try {
$output = Invoke-Command -Session $session -ScriptBlock {
param($new_key)
slmgr -ipk $new_key
sleep 5
slmgr -ato
sleep 5
} -ArgumentList $input_file.windows_key
} catch {
Write-Output "Failed to execute the command on $target. Error: $_"
exit
} finally {
# Close the session
Remove-PSSession -Session $session
}
}
}