#author("2018-09-29T20:37:44+09:00","","") #author("2023-11-01T09:48:26+09:00","","") #navi(../) * PowerShellを使ってネットワークドライブの接続・切断する方法 [#c85709cf] PowerShellのコマンドレットを使用して、ネットワークドライブの接続、切断する方法を以下に記します。~ 使用したPowerShellのバージョンは以下の通りです。 PS C:\> $PSVersionTable.PSVersion.ToString() 5.1.17134.228 #contents #htmlinsertpcsp(win-top.html,win-sp.html) * 関連記事 [#m9d6e829] -[[コマンドプロンプトでネットワークドライブの割り当て・切断をする方法・net use>コマンドプロンプト/ネットワークドライブの割り当て・切断をする方法・net use]] * ネットワークドライブ接続 [#mac26471] New-PSDriveコマンドレットを使用することにより、ネットワークドライブとして接続することができます。~ 構文は以下の通りです。 -PowerShellのセッションのみネットワークドライブとして認識 New-PSDrive -Name <ドライブ文字> -PSProvider FileSystem -Root <共有フォルダのパス> -永続的にネットワークドライブとして認識させる(エクスプローラーなどGUIでも認識できる) New-PSDrive -Persist -Name <ドライブ文字> -PSProvider FileSystem -Root <共有フォルダのパス> 以下、実際にNew-PSDriveコマンドレットを使用してネットワークドライブとして接続してみます。 #br + Get-PSDriveコマンドレットの出力です。 PS C:\> Get-PSDrive | Format-Table -AutoSize Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- Alias Alias C 50.35 68.29 FileSystem C:\ Cert Certificate \ D 0.97 27.67 FileSystem D:\ Env Environment Function Function HKCU Registry HKEY_CURRENT_USER HKLM Registry HKEY_LOCAL_MACHINE Variable Variable WSMan WSMan + 共有フォルダをネットワークドライブとして接続してみます。~ -- ネットワークドライブ名: P: -- 共有フォルダのパス: \\raspberrypi\pi~ #br 以下、New-PSDriveコマンドレットを実行したときの出力です。 PS C:\> New-PSDrive -Name P -PSProvider FileSystem -Root \\raspberrypi\pi Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- P FileSystem \\raspberrypi\pi PS C:\> p: PS P:\> c: 以下は、Pドライブを削除し確認しています。 PS C:\> Remove-PSDrive -Name P PS C:\> Get-PSDrive Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- Alias Alias C 50.39 68.25 FileSystem C:\ Cert Certificate \ D 0.97 27.67 FileSystem D:\ Env Environment Function Function HKCU Registry HKEY_CURRENT_USER HKLM Registry HKEY_LOCAL_MACHINE Variable Variable WSMan WSMan #br &color(red){注意:-Persistオプションをつけていないため、GUIによる確認はできません。};~ このPowerShellのセッションのみネットワークドライブとして認識します。~ ただし、利点としてドライブレターではなく、わかりやすい名称にすることができます。~ #br ドライブ名をRaspberryPiとしてみましょう。~ PS C:\> New-PSDrive -Name RaspberryPi -PSProvider FileSystem -Root \\raspberrypi\pi Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- Raspber... FileSystem \\raspberrypi\pi PS C:\> cd RaspberryPi: PS RaspberryPi:\> 上記の通り、ドライブ名をRaspberryPiにすることができました。(PowerShellセッション内のみです。)~ PowerShellスクリプト上で使えば、わかりずらいドライブレターではなく、意味を持たすことができますね。 #br 以下は、RaspberryPiで接続したドライブを削除(切断)しています。 PS RaspberryPi:\> c: PS C:\> Remove-PSDrive -Name RaspberryPi #br + GUIでも認識するネットワークドライブとしてNew-PSDriveコマンドレットを実行してみます。~ ''-Persist''オプションを追加してあります。 +すでに、Pドライブとしてマップしているので、切断します。 PS P:\> cd PS P:\> cd c: PS C:\> Remove-PSDrive p #br + ''-Persist''オプションを追加してNew-PSDriveコマンドレットを実行します。~ ++コマンド実行前 #ref(01.png) #br ++コマンド実行 PS C:\> New-PSDrive -Persist -Name P -PSProvider FileSystem -Root \\raspberrypi\pi Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- P 60.89 54.77 FileSystem \\raspberrypi\pi #br #ref(02.png) ++ 切断します。 PS C:\> Remove-PSDrive -Name P #br #ref(03.png) 上記では、PowerShellセッション内のみのネットワークドライブ(マップドライブ)および~ コマンドプロンプトで使用するnet use同等の操作手順でした。 * 資格情報(ユーザアカウント、パスワード)が必要なフォルダにマウントする方法 [#h6128f64] 上記ではユーザアカウント、パスワードが不要な場合の接続でした。~ 以下、ユーザID、パスワードの入力を必要とするネットワークフォルダに接続するサンプルコードになります。 $uid = 'ユーザアカウント' $pwd = 'ユーザパスワード' $serverNameOrIP = 'サーバ名 or IP' $mountDriveName = 'NetDrive' # NetDriveという名称でマウント $mountPoint = 'マウントするフォルダ名' # \\server\folder の場合は folder を指定する $securePassword = ConvertTo-SecureString $pwd -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential "$serverNameOrIP\$uid", $securePassword New-PSDrive -Name $mountDriveName -PSProvider FileSystem -Root "\\$serverNameOrIP\$mountPoint" -Credential $credential * ドライブ一覧の確認 [#edd5d42b] 上記の操作例で既に使用していますが、Get-PSDriveによりドライブの一覧を取得することができます。 PS C:\> Get-PSDrive Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- Alias Alias C 50.46 68.18 FileSystem C:\ Cert Certificate \ D 0.97 27.67 FileSystem D:\ Env Environment Function Function HKCU Registry HKEY_CURRENT_USER HKLM Registry HKEY_LOCAL_MACHINE Variable Variable WSMan WSMan * ネットワークドライブ(マップドライブ)の切断(削除) [#ada710e6] 上記の操作例で既に使用していますが、Remove-PSDriveで削除(切断)することができます。 +Pドライブとして接続 PS C:\> New-PSDrive -Persist -Name P -PSProvider FileSystem -Root \\raspberrypi\pi Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- P 60.89 54.77 FileSystem \\raspberrypi\pi +ドライブ一覧の表示(Pドライブが存在します) PS C:\> Get-PSDrive Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- Alias Alias C 50.46 68.18 FileSystem C:\ Cert Certificate \ D 0.97 27.67 FileSystem D:\ Env Environment Function Function HKCU Registry HKEY_CURRENT_USER HKLM Registry HKEY_LOCAL_MACHINE P 60.89 54.77 FileSystem \\raspberrypi\pi Variable Variable WSMan WSMan +Pドライブの削除(切断) PS C:\> Remove-PSDrive P +ドライブ一覧の表示(Pドライブが消えています) PS C:\> Get-PSDrive Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- Alias Alias C 50.46 68.18 FileSystem C:\ Cert Certificate \ D 0.97 27.67 FileSystem D:\ Env Environment Function Function HKCU Registry HKEY_CURRENT_USER HKLM Registry HKEY_LOCAL_MACHINE Variable Variable WSMan WSMan 以上、PowerShellでネットワークドライブ、マップドライブについて一覧表示、接続、切断(削除)の操作例でした。 #htmlinsertpcsp(win-btm.html,win-sp.html)