So today I try to map the drive with vbscript using the the wsrcipt.network object, and it worked fine with no issues... Don't know if it helps but I am doing all of this on a Windows 7 machine.
Option Explicit
On Error Resume Next
Dim strComputer, strServer
Dim objWMI, objIPConfig, objNetwork
Dim colIPConfig
Dim i
strComputer = "."
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objIPConfig = objWMI.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
For Each colIPConfig In objIPConfig
If Not IsNull(colIPConfig.IPAddress) Then
For i=LBound(colIPConfig.IPAddress) To UBound(colIPConfig.IPAddress)
If Not IsNull(colIPConfig.IPAddress(i)) And colIPConfig.IPAddress(i) <> "0.0.0.0" Then
If InStr(colIPConfig.IPAddress(0), "xxx.x.xx.") Then
strserver = "FILE-SERVER01"
ElseIf InStr(colIPConfig.IPAddress(0), "xxx.xx.xxx.") Then
strserver = "FILE-SERVER02"
Else
strserver = "FILE-SERVER03"
End If
End If
Next
End If
Next
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "V:", "\\" + strServer + "\Applications",,"DOMAIN\USER", "password"
But when I call the same object from powershell I get the same resualts from before... NET USE showing that the drive is unavailable.
c:
#Store IPV4 address as string
$ipaddress = ipconfig | findstr "IPv4"
$ipaddress = $ipaddress[0].tostring()
if ($ipaddress -eq " ") { $ipaddress = ipconfig | findstr "IPv4" }
$ipaddress = $ipaddress.tostring().replace("IPv4 Address. . . . . . . . . . . : ", "")
#Determine which application server to map
if ($ipaddress.StartsWith( " xxx.x.xx." )) { $AppServer = "FILE-SERVER1" }
elseif ($ipaddress.StartsWith( " xxx.xx.xxx." )) { $AppServer = "FILE-SERVER2"}
else { $AppServer = "FILE-SERVER3" } #All others
#Set the drive and path information
$Drive = "V:"
$UNC = "\\" + $AppServer + "\Apps"
#Disconnect drive if mapping already exists
if ( Test-Path -path "V:\" ) { net use /delete v: /y }
#Map the drive
(new-object -com WScript.Network).MapNetworkDrive("v:",$UNC,, "swn\altirisro", "You can only read!")