PowerShellのコマンドレットを使用して、ネットワークドライブの接続、切断する方法を以下に記します。
使用したPowerShellのバージョンは以下の通りです。
PS C:\> $PSVersionTable.PSVersion.ToString() 5.1.17134.228
New-PSDriveコマンドレットを使用することにより、ネットワークドライブとして接続することができます。
構文は以下の通りです。
New-PSDrive -Name <ドライブ文字> -PSProvider FileSystem -Root <共有フォルダのパス>
New-PSDrive -Persist -Name <ドライブ文字> -PSProvider FileSystem -Root <共有フォルダのパス>以下、実際にNew-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
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
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セッション内のみです。)
PS RaspberryPi:\> c: PS C:\> Remove-PSDrive -Name RaspberryPi
PS P:\> cd PS P:\> cd c: PS C:\> 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
PS C:\> Remove-PSDrive -Name P
上記では、PowerShellセッション内のみのネットワークドライブ(マップドライブ)および
コマンドプロンプトで使用するnet use同等の操作手順でした。
上記ではユーザアカウント、パスワードが不要な場合の接続でした。
以下、ユーザ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
上記の操作例で既に使用していますが、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
上記の操作例で既に使用していますが、Remove-PSDriveで削除(切断)することができます。
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
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
PS C:\> Remove-PSDrive 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でネットワークドライブ、マップドライブについて一覧表示、接続、切断(削除)の操作例でした。