首页 文章

RDS用户注销脚本慢

提问于
浏览
0

在几篇在线文章的帮助下,我能够编译一个powershell脚本,该脚本会为每个RD会话主机注销所有用户 . 我希望能够非常温和地注销用户并将配置文件写回存储系统上的漫游配置文件位置 . 但是,这太温和了,需要大约四个小时来完成我拥有的用户和RDS服务器的数量 .

此脚本旨在设置每个RDS服务器耗尽,但如果服务器可用,则允许重定向,因此围绕这一点的想法是在前15分钟内我将为用户准备好前几个服务器 .

所有这些都有效,但我想看看是否有任何关于加快这一点的建议 .

以下是遍历每个服务器并将用户注销的循环,然后将服务器登录模式设置为启用:

ForEach ($rdsserver in $rdsservers){
    try {
        query user /server:$rdsserver 2>&1 | select -skip 1 | ? {($_ -split "\s+")[-5]} | % {logoff ($_ -split "\s+")[-6] /server:$rdsserver /V}


        Write-Host "Giving the RDS Server time"
        Write-Progress "Pausing Script" -status "Giving $rdsserver time to settle" -perc (5/(5/100))
        Start-Sleep -Seconds 5




        $RDSH=Get-WmiObject -Class "Win32_TerminalServiceSetting" -Namespace "root\CIMV2\terminalservices" -ComputerName $rdsserver -Authentication PacketPrivacy -Impersonation Impersonate
        $RDSH.SessionBrokerDrainMode=0
        $RDSH.put() > $null
        Write-Host "$rdsserver is set to:"
        switch ($RDSH.SessionBrokerDrainMode) {
            0 {"Allow all connections."}
            1 {"Allow incoming reconnections but until reboot prohibit new connections."}
            2 {"Allow incoming reconnections but prohibit new connections."}
        default {"The user logon state cannot be determined."}
        }
    } 
    catch {}
}

2 回答

  • 0

    不确定你有多少台服务器但如果它少于50左右你可以与PSJobs并行执行此操作 . 你这样做时可以使用Write-Host,但是我会解析你的代码来收集你的服务器列表,但我会假设它有效,你可以让它将格式化列表返回给变量 $rdsservers . 你'll probably also want to modify the messages a bit so you can tell which server is which in the log file, or do different logs for each server. If you want anything other than the names of jobs to hit the console you'必须用 Write-Outputreturn 语句输出它 .

    $SB = {
    param($rdsserver)
    Start-Sleep -Seconds 5
    
    $RDSH=Get-WmiObject -Class "Win32_TerminalServiceSetting" -Namespace "root\CIMV2\terminalservices" -ComputerName $rdsserver -Authentication PacketPrivacy -Impersonation Impersonate
    $RDSH.SessionBrokerDrainMode=0
    $RDSH.put() > $null
    "$rdsserver is set to:" | out-file $LogPath #Set this to whatever you want
    switch ($RDSH.SessionBrokerDrainMode) {
        0 {"Allow all connections." | out-file $LogPath}
        1 {"Allow incoming reconnections but until reboot prohibit new connections." | out-file $LogPath}
        2 {"Allow incoming reconnections but prohibit new connections." | out-file $LogPath}
        default {"The user logon state cannot be determined." | out-file $LogPath}
    }
    
    foreach ($server in $rdsservers){
      Start-Job -Scriptblock -ArgumentList $server
    }
    
    Get-Job | Wait-Job | Receive-Job
    

    foreach循环启动作业,然后在获取任何输出数据之前,最后一行等待所有这些作业完成 . 如果脚本有可能永远不会完成,您还可以在等待时设置超时 . 如果你有大量的盒子,你可能想要查看作业的运行空间,因为它们具有更好的性能但需要更多的工作才能使用 . This Link可以帮助你,如果你决定这样做 . 我不知道我能做些什么 .

  • 0

    我已准备好进行测试,但它可能会破坏性地进行测试 . 你向外看的巫师可能会看到这个并笑 . 如果我做错了,请告诉我 .

    $Serverperbatch = 2
    $job = 0
    $job = $Serverperbatch - 1 
    $batch = 1
    
    While ($job -lt $rdsservers.count) {
        $ServerBatch = $rdsservers[$job .. $job]
        $jobname = "batch$batch"
    Start-job -Name $jobname -ScriptBlock {
        param ([string[]]$rdsservers)
        Foreach ($rdsserver in $rdsservers) {
            try {
        query user /server:$rdsserver 2>&1 | select -skip 1 | ? {($_ -split "\s+")[-5]} | % {logoff ($_ -split "\s+")[-6] /server:$rdsserver /V}
        $RDSH=Get-WmiObject -Class "Win32_TerminalServiceSetting" -Namespace "root\CIMV2\terminalservices" -ComputerName $rdsserver -Authentication PacketPrivacy -Impersonation Impersonate
        $RDSH.SessionBrokerDrainMode=0
        $RDSH.put() > $null
       }
         catch {}
    
    } -ArgumentList (.$serverbatch)
        $batch += 1
        $Job = $job + 1
        $job += $serverperbatch
    
        If ($Job -gt $rdsservers.Count) {$Job = $rdsservers.Count}
        If ($Job -gt $rdsservers.Count) {$Job = $rdsservers.Count}
      }
    }
    Get-Job | Wait-Job | Receive-Job
    

相关问题