Created
June 8, 2015 07:40
-
-
Save stknohg/4e51bb4719498ee71ba8 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 TerminalName | |
対象となるターミナル名を設定します。通常は"RDP-Tcp"が指定されます。 | |
.PARAMETER ComputerName | |
対象となるコンピューター名を設定します。未指定の場合は現在のコンピューター名が対象になります。 | |
.PARAMETER Credential | |
リモートコンピュータに対する認証情報を設定します。 | |
String型もしくはPSCredential型の値を設定してください。 | |
.EXAMPLE | |
Remove-RDSConnectionPermissions "Server01\User01" | |
.NOTES | |
このメソッドを実行するには昇格している必要があります。 | |
.LINK | |
#> | |
Function Remove-RDSConnectionPermissions(){ | |
[OutputType('Boolean')] | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] | |
[string]$AccountName = "", | |
[Parameter(Mandatory=$false)] | |
[string]$TerminalName = "RDP-Tcp", | |
[Parameter(Mandatory=$false)] | |
[string]$ComputerName = $env:COMPUTERNAME, | |
[Parameter(Mandatory=$false)] | |
$Credential = $null | |
) | |
# 更新対象となるWin32_TSAccountアカウントの取得 | |
try{ | |
$Accounts = $null | |
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 | |
} | |
# アカウント削除 | |
foreach($Account in $Accounts){ | |
try{ | |
Write-Verbose ("{0}.{1} : Execute Win32_TSAccount.Delete()" -F $ComputerName, $AccountName) | |
$Account.Delete() | Out-Null | |
} | |
catch [System.Management.Automation.MethodInvocationException] { | |
Write-Error "Win32_TSAccount.Delete()メソッドの呼び出しに失敗しました。メソッドの呼び出しに必要な権限を確認してください。" | |
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