Showing posts with label WScript.Network. Show all posts
Showing posts with label WScript.Network. Show all posts

Thursday, December 16, 2010

Map Network Drive with a users credentials


Three quick and easy ways to Map a UNC path to a local drive. The samples below (DOS, VBS, PowerShell) map the Q: drive to a network path using different credentials. you can also remove the parameters for credentials and it will create the connection with the current logged-on user.

DOS
NET USE Q: \\MyUnc\Path /USER:MyUser MyPassword

VBS

strDrive = "Q:"
strUNC = "\\MyUnc\Path"
strProfile = "False"   ' Mapping (not) stored in user Profile
strUser = "MyUser"
strPassword = "MyPassword"
Set objNetwork = CreateObject("WScript.Network") 
objNetwork.MapNetworkDrive strDrive, strUNC, strProfile, strUser, strPassword 

Powershell

$Drive = "Q:"
$UNC = "\\MyUnc\Path"
$Profile = "False"
$User = "MyUser"
$Password = "MyPassord"
$Network = New-Object -com WScript.Network; 
$Network.mapnetworkdrive($Drive,$Unc, $Profile, $User, $Password)

The code above is basically the same as done in VBS, You can also make the DOS call in Powershell.

Wednesday, December 30, 2009

Create Local Win NT User, Windows User

Two quick and easy methods of creating local NT users.  The samples (DOS and VBS) below create a user on the local system, the VBS method check to see if the user exists before adding.

DOS:
NET USER DosUser UserPassword /add

VBS:

Set WshNetwork = CreateObject("WScript.Network")
strComputerName = WshNetwork.ComputerName 'getting the machine name
strUserName="VbsUser"
strPassword="UserPassword"
checkuser = 1 
Set objComputer = GetObject("WinNT://" & strComputerName)
objComputer.Filter = Array("User")
For Each objUser In objComputer
    If (objUser.Name = strUserName) And (checkuser = 1) Then 
       checkuser = 0
   End If
Next

If checkuser = 1 Then
   Set colaccounts = GetObject("WinNT://" & strComputerName & ",computer")
   Set objUser = colaccounts.Create("user", strUserName)
   objUser.SetPassword strPassword
   objUser.SetInfo
End If