4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RDPユーザーをさっくりとPowerShellで作ったり削除する

Posted at

経緯

・キッティングや端末設計時に、GUIで毎回作成削除するのが大変なため。
・Intuneの検証で頻繁にローカルユーザーを作ったり削除するため。

RDPユーザー作成スクリプト

・新規ユーザーを作成します。
・Remoete Desktop Usersグループに追加します。
・Remoete Desktop Usersグループのメンバーリストを表示します。

CreateRDPUser.ps1
$username = Read-Host "新しいユーザーのユーザー名を入力してください"
$password = Read-Host "新しいユーザーのパスワードを入力してください" -AsSecureString

New-LocalUser -Name $username -Password $password -PasswordNeverExpires:$true
Write-Host "ユーザーアカウント '$username' が正常に作成されました。"

$confirm = Read-Host "'$username' を Remote Desktop Users グループに追加しますか? (Y/N)"
if ($confirm -eq "Y" -or $confirm -eq "y") {
    Add-LocalGroupMember -Group "Remote Desktop Users" -Member $username
    Write-Host "ユーザー '$username' が Remote Desktop Users グループに追加されました。"
} else {
    Write-Host "ユーザー '$username' は Remote Desktop Users グループに追加されませんでした。"
}

$rdpUsers = Get-LocalGroupMember "Remote Desktop Users" | Select-Object -ExpandProperty Name
$rdpUsers = $rdpUsers -replace '.*\\'
$rdpUsersInfo = $rdpUsers | ForEach-Object { [PSCustomObject]@{ Username = $_ } }

Write-Host "リモートデスクトップユーザーのリスト:"
$rdpUsersInfo | Format-Table -Property Username

Pause

実行イメージ

image.png

RDPユーザー削除スクリプト

DeleteRDPUser.ps1
do {
    $rdpUsers = Get-LocalGroupMember "Remote Desktop Users" | Select-Object -ExpandProperty Name
    $rdpUsers = $rdpUsers -replace '.*\\'
    $rdpUsersInfo = $rdpUsers | ForEach-Object { [PSCustomObject]@{ Username = $_ } }

    Write-Host "リモートデスクトップユーザーのリスト:"
    $rdpUsersInfo | Format-Table -Property Username

    $username = Read-Host "削除するユーザーのユーザー名を入力してください(キャンセルする場合は Enter キーを押してください)"
    if ($username -ne "") {
        if ($rdpUsers -contains $username) {
            $confirm = Read-Host "ユーザー $username を削除してもよろしいですか? (Y/N)"

            if ($confirm -eq "Y" -or $confirm -eq "y") {
                Remove-LocalGroupMember -Group "Remote Desktop Users" -Member $username
                Remove-LocalUser -Name $username
                Write-Host "ユーザー $username が正常に削除されました。"
            } else {
                Write-Host "削除プロセスが中止されました。"
            }
        } else {
            Write-Host "ユーザー $username は 'Remote Desktop Users' グループに存在しません。"
        }
    }
    else {
        Write-Host "ユーザー名が入力されていません。ループを終了します。"
    }
    Write-Host
} while ($rdpUsers.Count -gt 0)
Write-Host "'Remote Desktop Users' グループにユーザーが存在しません。スクリプトを終了します。"

実行イメージ

image.png

まとめ

・作成後に自動でログオフして該当ユーザーで再ログインまで自動化しようと思いましたが、サイロ化・属人化しそうなのでやめました。(この判断大事)
・シンプリシティ・リユーサビリティ・リーダビリティの3つは一方通行の開発にならないための指標となります。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?