标签搜索

自动重置windows server远程过期

admin
2025-04-03 / 0 评论 / 3 阅读 / 正在检测是否收录...
## This Script is intended to be used for Querying remaining time and resetting Terminal Server (RDS) Grace Licensing Period to Default 120 Days. 
## Developed by Prakash Kumar (prakash82x@gmail.com) May 28th 2016 
## www.adminthing.blogspot.com 
## Disclaimer: Please test this script in your test environment before executing on any production server. 
## Author will not be responsible for any misuse/damage caused by using it. 
#拷贝到C盘根目录
Write-Host -fore Green ====================================================== 
Write-Host -fore Green '开始自动拷贝到C盘'
$scriptPath = $MyInvocation.MyCommand.Path
Copy-Item -Path $scriptPath -Destination "C:\"
Write-Host -fore Green '拷贝完毕'
Write-Host -fore Green '开始注册定时重置任务'
$taskName = "RemoteDesktopJob"
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($task) {
    Write-Host "计划任务每天重置远程桌面期限存在,已跳过注册。"
} else {
    $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File 'C:\ps.ps1'"
$trigger = New-ScheduledTaskTrigger -Daily -At "10:00 PM"
$settings = New-ScheduledTaskSettingsSet
$principal = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" -RunLevel Highest
Register-ScheduledTask -TaskName "RemoteDesktopJob" -Action $action -Trigger $trigger -Settings $settings -Principal $principal
Write-Host -fore Green '定时重置任务注册完成,将在每天自动重置远程桌面激活期限'
}
$ErrorActionPreference = "SilentlyContinue" 
## Display current Status of remaining days from Grace period. 
$GracePeriod = (Invoke-WmiMethod -PATH (gwmi -namespace root\cimv2\terminalservices -class win32_terminalservicesetting).__PATH -name GetGracePeriodDays).daysleft 
Write-Host -fore Green 'RDS (Terminal Server)宽限期天数为剩余天数' : $GracePeriod 
Write-Host -fore Green ======================================================   
Write-Host 
$definition = @" 
using System; 
using System.Runtime.InteropServices;  
namespace Win32Api 
{ 
    public class NtDll 
    { 
        [DllImport("ntdll.dll", EntryPoint="RtlAdjustPrivilege")] 
        public static extern int RtlAdjustPrivilege(ulong Privilege, bool Enable, bool CurrentThread, ref bool Enabled); 
    } 
} 
"@  
 
Add-Type -TypeDefinition $definition -PassThru 
 
$bEnabled = $false 
 
## Enable SeTakeOwnershipPrivilege 
$res = [Win32Api.NtDll]::RtlAdjustPrivilege(9, $true, $false, [ref]$bEnabled) 
 
## Take Ownership on the Key 
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Terminal Server\RCM\GracePeriod", [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::takeownership) 
$acl = $key.GetAccessControl() 
$acl.SetOwner([System.Security.Principal.NTAccount]"Administrators") 
$key.SetAccessControl($acl) 
 
## Assign Full Controll permissions to Administrators on the key. 
$rule = New-Object System.Security.AccessControl.RegistryAccessRule ("Administrators","FullControl","Allow") 
$acl.SetAccessRule($rule) 
$key.SetAccessControl($acl) 
 
## Finally Delete the key which resets the Grace Period counter to 120 Days. 
Remove-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\RCM\GracePeriod' 
 
write-host 
Write-host -ForegroundColor Red '重置中请稍等...' 
Start-Sleep -Seconds 10  
 
## Display Remaining Days again as final status 
tlsbln.exe 
$GracePost = (Invoke-WmiMethod -PATH (gwmi -namespace root\cimv2\terminalservices -class win32_terminalservicesetting).__PATH -name GetGracePeriodDays).daysleft 
Write-Host -fore Yellow ===================================================== 
Write-Host -fore Yellow '重置成功,RDS (Terminal Server)宽限期天数为剩余天数' : $GracePost 
Write-Host -fore Yellow ===================================================== 
 
## Cleanup of Variables 
Remove-Variable * -ErrorAction SilentlyContinue
Exit
0

评论 (0)

取消