
PowerShellで
・ネットワークドライブの割り当て
ができます!

コード
ここでは例として
・ネットワークパス上にあるサーバーのパス「\\SERVERHOST\Share」を
・Zドライブに割り当て
をします。
※ここではユーザー「user」を使用します。ユーザー「user」のパスワードは「password」とします。
# 割り当てる「ネットワークパス上にあるサーバーのパス」
$targetPath = "\\SERVERHOST\Share"
# ドライブ名
$driveName = "Z"
# ユーザー名
$userName = "user"
# パスワード
$password ="password"
# 認証情報のインスタンスを生成する
$securePass = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential $userName, $securePass
$ws = New-Object -ComObject Wscript.Shell
try
{
# ネットワークドライブの割り当てを実行
New-PSDrive -Persist -PSProvider FileSystem -Name $driveName -Root $targetPath -Credential $cred -ErrorAction Stop
$ws.popup("割り当てました。")
}
catch
{
$ws.popup("エラー : " + $PSItem)
}
実行結果
ネットワークドライブの割り当てが出来ました。


参考①
ネットワークドライブの切断もできます。
詳細は以下の記事をご確認ください。
参考②
上記のコードで使用した以下の詳細は、公式サイトをご確認ください。
●「New-PSDrive」コマンドレット
●「ConvertTo-SecureString」コマンドレット
●「PSCredential 」クラス