コンテンツへスキップ

リモートデスクトップの設定をバッチファイルで行う

リモートデスクトップ有効化とか、ユーザー作成とか、ファイアウォールの設定とか、バッチファイルで一括処理できないかなと。

以下をバッチファイル形式で保存して、任意のコンピューター上で実行する。
ついでに拡張子と隠しファイルを表示するコマンドも入れています。

新規のPCセットアップをRDP で行うための下準備なので、管理者権限とかもつけてます。
ユーザー名とか、パスワードとかは編集してください。

日本語が文字化けするので、ANSI で保存します。

@echo off
echo Enabling Remote Desktop...

:: 管理者権限が必要なコマンド
echo 管理者権限が必要なコマンドを実行中...

:: 管理者権限で再実行するスクリプト
:checkPrivileges
net session >nul 2>&1
if %errorLevel% == 0 (
    goto gotPrivileges
) else (
    goto getPrivileges
)

:getPrivileges
if "%1"=="%~0" (echo 管理者権限で実行できませんでした。 & pause & exit /B)
echo 管理者権限で再実行しています...
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command "Start-Process '%~0' -ArgumentList '%~f0 %*' -Verb runAs"
exit /B

:gotPrivileges
shift
echo 管理者権限で実行中...

@echo off

REM Show file extensions
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v HideFileExt /t REG_DWORD /d 0 /f

REM Show hidden files
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v Hidden /t REG_DWORD /d 1 /f

REM Show protected operating system files
REM reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v ShowSuperHidden /t REG_DWORD /d 1 /f

REM Refresh Explorer to apply changes
taskkill /f /im explorer.exe
start explorer.exe

echo File extensions and hidden files are now visible.

pause

REM Enable Remote Desktop
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f

REM Enable Remote Desktop through Windows Firewall
netsh advfirewall firewall set rule group="リモート デスクトップ" new enable=yes

echo Remote Desktop has been enabled.
pause


REM 節電のスリープモード移行、ハイブリットスリープを無効化。デスクトップ向け
powercfg /change standby-timeout-dc 0
powercfg /hibernate off

pause

powercfg /query

pause

REM Create a new user
net user "新しいユーザー名" "パスワード" /add

REM Add user to the Remote Desktop Users group
net localgroup "Remote Desktop Users" "ユーザー名" /add

REM Add user to the Administrators group
net localgroup Administrators "ユーザー名" /add

echo Remote Desktop has been enabled and user has been added to the group.
pause

echo 設定後のIPアドレスを確認してください...
ipconfig /all

pause

リモートデスクトップ接続できない場合、ファイアウォールとか、アンチウィルスソフトとか、セキュリティ対策により通信やプログラムがブロックされていないかも確認してください。

以下、コマンドの詳細について

BING さんの回答。

リモートデスクトップ接続を有効化する

@echo off
echo Enabling Remote Desktop...

REM Enable Remote Desktop
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f

REM Enable Remote Desktop through Windows Firewall
netsh advfirewall firewall set rule group="remote desktop" new enable=yes

echo Remote Desktop has been enabled.
pause

実行すると、確かにリモート接続が有効化になっているっぽいんだけど、ファイアウォールの規則についての記述が効いていない様子。
設定画面でスイッチがオンになっているかの確認はできないけど、接続受付まではきた。

これを有効化したい。

グループ名に問題があるのでは?ということで、以下を確認。

netsh advfirewall firewall show rule name=all

これかな・・・

規則名:                               リモート デスクトップ - ユーザー モード (TCP 受信)
----------------------------------------------------------------------
有効:                                 はい
方向:                                 入力
プロファイル:                         ドメイン,プライベート,パブリック
グループ:                             リモート デスクトップ
ローカル IP:                          任意
リモート IP:                          任意
プロトコル:                           TCP
ローカル ポート:                      3389
リモート ポート:                      任意
エッジ トラバーサル:                  いいえ
操作:                                 許可

ついでにユーザーも追加するコマンドもいれる。

ユーザーを新規で作成

REM Create a new user
net user "新しいユーザー名" "パスワード" /add

リモートデスクトップ接続のグループに追加

REM Add user to the Remote Desktop Users group
net localgroup "Remote Desktop Users" "ユーザー名" /add

echo Remote Desktop has been enabled and user has been added to the group.
pause

管理者グループに追加

REM Add user to the Administrators group
net localgroup Administrators "ユーザー名" /add

管理者権限で実行するスクリプト

これも追加。

@echo off
:: 管理者権限が必要なコマンド
echo 管理者権限が必要なコマンドを実行中...

:: 管理者権限で再実行するスクリプト
:checkPrivileges
net session >nul 2>&1
if %errorLevel% == 0 (
    goto gotPrivileges
) else (
    goto getPrivileges
)

:getPrivileges
if "%1"=="%~0" (echo 管理者権限で実行できませんでした。 & pause & exit /B)
echo 管理者権限で再実行しています...
powershell -Command "Start-Process '%~0' -ArgumentList '%~1' -Verb runAs"
exit /B

:gotPrivileges
shift
:: ここに管理者権限が必要なコマンドを追加します
echo 管理者権限で実行中...
pause

powershell のところで落ちる場合は、下記に書き換え。
ページトップのサンプルは書き換えてます。

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command "Start-Process '%~0' -ArgumentList '%~f0 %*' -Verb runAs"
exit /B

拡張子と、隠しファイルを表示するコマンド

初期設定の一環なので、こんなコマンドも追加。
システムで非表示にしているファイルはコメントアウトしています。

@echo off

REM Show file extensions
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v HideFileExt /t REG_DWORD /d 0 /f

REM Show hidden files
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v Hidden /t REG_DWORD /d 1 /f

REM Show protected operating system files
REM reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v ShowSuperHidden /t REG_DWORD /d 1 /f

REM Refresh Explorer to apply changes
taskkill /f /im explorer.exe
start explorer.exe

echo File extensions and hidden files are now visible.

pause

RDP を無効化する

これで、無効化されます。

@echo off
echo Disabling Remote Desktop...

REM Disable Remote Desktop
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 1 /f

REM Disable Remote Desktop through Windows Firewall
netsh advfirewall firewall set rule group="リモート デスクトップ" new enable=no

echo Remote Desktop has been disabled.
pause
(Visited 19 times, 1 visits today)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です