這隻程式用來列出目前 RDCB 上記載的 Remote Desktop Session 供選擇 Shadow 控制
可簡化遠端協助的步驟, 並可 Shadow Windows 10 Client
(Windows Server 2012 R2 RDCB 目前以 Server Manager 無法 Shadow 至 Windows 10 Client)
執行的電腦必須安裝有 Remote Desktop Service Power Shell Module
執行本 PowerShell Script 的帳號必須具有連線目的 Client 的本機管理員權限
有關 Prompt User 的相關說明及設定參考以下連結
https://blogs.technet.microsoft.com/askperf/2013/10/22/windows-8-1-windows-server-2012-r2-rds-shadowing-is-back/
RDShadowSelect.ps1
$DomainName = "Contoso.com"
$RDCBServer = "rdcb.$DomainName"
Function AddSpace {
# Position:
# 0 = Front
# else = End
Param(
[String]$String,
[Int]$Length,
[Int]$Position
);
$SpaceToAdd = $Length - $String.Length
for ($i=0;$i -lt $SpaceToAdd;$i++) {
if ($Position = 0) {
$String = " " + $String
} else {
$String = $String + " "
}
};
return $String;
};
Function MainBoard {
$Script:TargetSession = ""
while ($Script:TargetSession -eq "") {
cls
Write-Host ""
Write-Host "`tReading session, please wait..."
$Script:UserName = @()
$Script:ServerName = @()
$Script:SessionId = @()
$Script:SessionState = @()
$RDSessionInfo = Get-RDUserSession -ConnectionBroker $RDCBServer | Select-Object –Property UserName,ServerName,SessionId,SessionState
$Script:UserName = $RDSessionInfo.UserName
$Script:ServerName = $RDSessionInfo.ServerName
$Script:SessionId = $RDSessionInfo.SessionId
$Script:SessionState = $RDSessionInfo.SessionState
Write-Host ""
Write-Host "`tSession List: (ServerName with a * Means it's a RemoteAPP Session)"
Write-Host ""
Write-Host "`t No.`t UserName `t ServerName `t SessionState"
Write-Host "`t----`t----------------------`t--------------------------------`t-------------------"
for ($i=0;$i -lt $Script:UserName.Count;$i++) {
$TrimServerName = $Script:ServerName[$i] -replace ".$DomainName", ""
if ($TrimServerName.Length -ne $Script:ServerName[$i].Length) {
$TrimServerName = "* " + $TrimServerName
};
$Script:ServerName[$i] = $Script:ServerName[$i] -replace ".$DomainName", ""
#if ((($i/5) -is [int]) -and (($i/5) -gt 0)) {Write-Host "`t----`t--------------`t -----------------`t-------------- $i"};
if (($i+1) -lt 10) {$No = AddSpace ($i+1) 3 0;$No = " $No"} else {$No = AddSpace ($i+1) 3 0};
$Script:UserNameDisplay = AddSpace $Script:UserName[$i] 12 1
$Script:ServerNameDisplay = AddSpace $TrimServerName 17 1
Write-Host "`t" $No "`t" $Script:UserNameDisplay "`t " $Script:ServerNameDisplay "`t" $No "`t" $Script:SessionState[$i]
};
Write-Host ""
$Script:ChooseString = "`tChoose 1-" + $Script:UserName.Count + " (Enter to Reload / q to Leave)"
$Script:TargetSession = (Read-Host $Script:ChooseString)
}
write-host ""
for ($i=0;$i -lt $Script:UserName.Count;$i++) {
if ($Script:TargetSession -eq ($i+1)) {
if ($Script:SessionState[$i] -ne "STATE_ACTIVE") {
write-host "`tThe Session is Disconnected, could not connect."
write-host ""
write-host "`tPress any key to leave ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit;
} else {
Write-Host "`tSelect Method:"
write-host ""
Write-Host "`t`t1 - View Only"
Write-Host "`t`t2 - Control"
write-host ""
$ViewOrControl = (Read-Host "`t`tChoose options 1-2 (Default: View Only)") -as [int]
write-host ""
switch($ViewOrControl) {
1 {
$ViewOrControl = ""
}
2 {
$ViewOrControl = " /Control"
}
Default {
$ViewOrControl = ""
}
}
Write-Host "`tSelect Option:"
write-host ""
Write-Host "`t`t1 - Prompt User"
Write-Host "`t`t2 - No Prompt"
write-host ""
Write-Host "`t`tDefault:"
Write-Host "`t`t`tView Only - No Prompt"
Write-Host "`t`t`tControl - Prompt User"
write-host ""
$PromptUser = (Read-Host "`t`tChoose from options 1-2") -as [int]
write-host ""
switch($PromptUser) {
1 {
$PromptUser = ""
}
2 {
$PromptUser = " /noConsentPrompt"
}
Default {
if ($ViewOrControl -eq "") {
$PromptUser = " /noConsentPrompt"
} else {
$PromptUser = ""
}
}
}
$Command = "mstsc /v:" + $Script:ServerName[$i] + "." + $DomainName + " /shadow:" + $Script:SessionId[$i] + $ViewOrControl + $PromptUser
write-host "`tCommand:" $Command
write-host ""
Invoke-Expression $Command
write-host "`tPress any key to reload ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
};
break;
};
};
};
while ( ($Script:TargetSession -ne "q") -and ($Script:TargetSession -ne "Q") ) {
MainBoard;
};
write-host ""
write-host "`tPress any key to leave ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
write-host ""
write-host ""
RDShadowSelect.bat
@echo off
mode con: cols=110 lines=40
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" -command ". 'C:\Command\RDShadowSelect.ps1';"
利用這個 .bat 檔來呼叫上面的 PowerShell 程式
沒有留言:
張貼留言