WindowsPCでネットワークドライブを追加するスクリプト(VBS)
ひとまず3個のドライブを追加するように作ってあります。
また、既存のドライブが存在する場合はいったん切断するようにしてあります。
緑背景の部分を編集してご利用ください。
Option Explicit
On Error Resume Next
' 変数宣言
Dim WshNetworkObj
Dim NetworkObj
Dim ShellAppsObj
Dim FsoObj
Dim DriveLetter
Dim DrivePath
Dim DriveName
' 初期設定
Set WshNetworkObj = WScript.CreateObject("WScript.Network")
Set NetworkObj = CreateObject( "WScript.Network" )
Set ShellAppsObj = CreateObject( "Shell.Application" )
Set FsoObj = WScript.CreateObject("Scripting.FileSystemObject")
'--------------------------------ドライブ1個目--------------------------------
'初期設定
DriveLetter = "X:" '追加するドライブレターを指定
DrivePath = "\\hogehoge1\Shared" '追加するパスを指定
DriveName = "Xドライブ" '表示名を指定
'ドライブレターが既に存在している場合は切断した後にネットワークドライブを追加する
If FsoObj.DriveExists(DriveLetter) Then
'切断
WshNetworkObj.RemoveNetworkDrive DriveLetter, True, True
WScript.Sleep 1000
'追加
NetworkObj.MapNetworkDrive DriveLetter,DrivePath, True
With ShellAppsObj.NameSpace( DriveLetter ) .Items().Item().Name = DriveName
End With
Else
'追加
NetworkObj.MapNetworkDrive DriveLetter,DrivePath, True
With ShellAppsObj.NameSpace( DriveLetter ) .Items().Item().Name = DriveName
End With
End If
DriveLetter = Nothing
DrivePath = Nothing
DriveName = Nothing
'------------------------------------------------------------------------------
WScript.Sleep 1000
'--------------------------------ドライブ2個目--------------------------------
'初期設定
DriveLetter = "Y:" '追加するドライブレターを指定
DrivePath = "\\hogehoge2\Shared" '追加するパスを指定
DriveName = "Yドライブ" '表示名を指定
'ドライブレターが既に存在している場合は切断した後にネットワークドライブを追加する
If FsoObj.DriveExists(DriveLetter) Then
'切断
WshNetworkObj.RemoveNetworkDrive DriveLetter, True, True
WScript.Sleep 1000
'追加
NetworkObj.MapNetworkDrive DriveLetter,DrivePath, True
With ShellAppsObj.NameSpace( DriveLetter ) .Items().Item().Name = DriveName
End With
Else
'追加
NetworkObj.MapNetworkDrive DriveLetter,DrivePath, True
With ShellAppsObj.NameSpace( DriveLetter ) .Items().Item().Name = DriveName
End With
End If
DriveLetter = Nothing
DrivePath = Nothing
DriveName = Nothing
'------------------------------------------------------------------------------
WScript.Sleep 1000
'--------------------------------ドライブ3個目--------------------------------
'初期設定
DriveLetter = "Z:" '追加するドライブレターを指定
DrivePath = "\\hogehoge3\Shared" '追加するパスを指定
DriveName = "Zドライブ" '表示名を指定
'ドライブレターが既に存在している場合は切断した後にネットワークドライブを追加する
If FsoObj.DriveExists(DriveLetter) Then
'切断
WshNetworkObj.RemoveNetworkDrive DriveLetter, True, True
WScript.Sleep 3000
'追加
NetworkObj.MapNetworkDrive DriveLetter,DrivePath, True
With ShellAppsObj.NameSpace( DriveLetter ) .Items().Item().Name = DriveName
End With
Else
'追加
NetworkObj.MapNetworkDrive DriveLetter,DrivePath, True
With ShellAppsObj.NameSpace( DriveLetter ) .Items().Item().Name = DriveName
End With
End If
DriveLetter = Nothing
DrivePath = Nothing
DriveName = Nothing
'------------------------------------------------------------------------------
Set WshNetworkObj = Nothing
Set NetworkObj = Nothing
Set ShellAppsObj = Nothing
Set FsoObj = Nothing
WScript.Quit(0)