Last active
August 29, 2015 14:22
-
-
Save stknohg/a67c8ec00356b240d620 to your computer and use it in GitHub Desktop.
Remote Desktop ServiceのRDP-Tcp Connectionのアクセス権を設定するスクリプト。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Remote Desktop ServiceのRDP-Tcp Connectionのアクセス権を設定します。 | |
.PARAMETER AccountName | |
対象となるアカウント名を指定します。 | |
.PARAMETER PermissionMask | |
更新する権限を指定します。複数指定可能です。 | |
Query : 情報の照会(WINSTATION_QUERY) | |
Set : 情報の設定(WINSTATION_SET) | |
Logoff : ログオフ(WINSTATION_LOGOFF) | |
Virtual : 仮想チャネル(WINSTATION_VIRTUAL) | |
Shadow : リモート制御(WINSTATION_SHADOW) | |
Logon : ログオン(WINSTATION_LOGON) | |
Reset : リセット(WINSTATION_RESET) | |
Msg : メッセージ(WINSTATION_MSG) | |
Connect : 接続(WINSTATION_CONNECT) | |
Disconnect : 切断(WINSTATION_DISCONNECT) | |
.PARAMETER Allow | |
$trueを指定すると許可の権限を追加します。 | |
$falseを指定すると拒否の権限を追加します。 | |
.PARAMETER TerminalName | |
対象となるターミナル名を設定します。通常は"RDP-Tcp"が指定されます。 | |
.PARAMETER ComputerName | |
対象となるコンピューター名を設定します。未指定の場合は現在のコンピューター名が対象になります。 | |
.PARAMETER Credential | |
リモートコンピュータに対する認証情報を設定します。 | |
String型もしくはPSCredential型の値を設定してください。 | |
.EXAMPLE | |
Set-RDSConnectionPermissions "BUILTIN\Remote Desktop Users" -PermissionMask Query,Set,Connect -Allow $true | |
.NOTES | |
設定する権限は現在の権限に対して追記する形になります。 | |
また、このメソッドを実行するには昇格している必要があります。 | |
.LINK | |
https://technet.microsoft.com/en-us/library/cc753032.aspx | |
https://msdn.microsoft.com/en-us/library/windows/desktop/aa383776 | |
#> | |
Function Set-RDSConnectionPermissions(){ | |
[OutputType('Boolean')] | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] | |
[string]$AccountName = "", | |
[Parameter(Mandatory=$true)] | |
[ValidateSet('Query', 'Set', 'Logoff', 'Virtual', 'Shadow', 'Logon', 'Reset', 'Msg', 'Connect', 'Disconnect')] | |
$PermissionMask, | |
[Parameter(Mandatory=$true)] | |
[Boolean]$Allow = $true, | |
[Parameter(Mandatory=$false)] | |
[string]$TerminalName = "RDP-Tcp", | |
[Parameter(Mandatory=$false)] | |
[string]$ComputerName = $env:COMPUTERNAME, | |
[Parameter(Mandatory=$false)] | |
$Credential = $null | |
) | |
# 更新対象となるWin32_TSAccountアカウントの取得 | |
$Accounts = $null | |
try{ | |
if($Credential -eq $null){ | |
$Accounts = Get-WmiObject -Namespace "Root\CIMv2\TerminalServices" -Class Win32_TSAccount -ComputerName $ComputerName | |
}else{ | |
$Accounts = Get-WmiObject -Namespace "Root\CIMv2\TerminalServices" -Class Win32_TSAccount -ComputerName $ComputerName -Credential $Credential | |
} | |
$Accounts = $Accounts | Where-Object { ` | |
$_.TerminalName.ToUpper() -eq $TerminalName.ToUpper() -and ` | |
$_.AccountName.ToUpper() -eq $AccountName.ToUpper() ` | |
} | |
}catch{ | |
Write-Error $_.Exception | |
return $false | |
} | |
if($Accounts.Count -eq 0){ | |
Write-Warning "更新対象となるアカウントがいませんでした。" | |
return $false | |
} | |
# 権限更新 | |
# 昇格していない場合、Win32_TSAccount.ModifyPermissions()メソッドは実行時エラーになる | |
$PermissionHash = @{ | |
'Query' = 0; | |
'Set' = 1; | |
'Logoff' = 2; | |
'Virtual' = 3; | |
'Shadow' = 4; | |
'Logon' = 5; | |
'Reset' = 6; | |
'Msg' = 7; | |
'Connect' = 8; | |
'Disconnect' = 9; | |
} | |
$Permissions = New-Object 'System.Collections.Generic.List[Int32]' | |
foreach($p in $PermissionHash.Keys){ | |
if($PermissionMask -contains $p){ | |
$Permissions.Add($PermissionHash[$p]) | |
} | |
} | |
foreach($Account in $Accounts){ | |
try{ | |
foreach($Permission in $Permissions){ | |
Write-Verbose ("{0}.{1} : Execute ModifyPermissions({2}, {3})" -F $ComputerName, $Account.AccountName, $Permission, $Allow) | |
$Account.ModifyPermissions($Permission, $Allow) | Out-Null | |
} | |
} | |
catch [System.Management.Automation.MethodInvocationException] { | |
Write-Error "Win32_TSAccount.ModifyPermissions()メソッドの呼び出しに失敗しました。メソッドの呼び出しに必要な権限を確認してください。" | |
return $false | |
} | |
catch{ | |
Write-Error $_.Exception | |
return $false | |
} | |
} | |
return $true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment