• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1728
  • Last Modified:

access denied for users to run Net Use within a batch file to initiate logon scripts.

I have some small scripts that delete all mapped drives and recreate the mapped drives using the Net Use command.
Net Use * \Delete /yes
Net Use \\computer\share /yes

These run fine for the administrator, but not the user

Also, the users each have another mapped network drive that is used for their home folder. This has been created using their active directory profile. I don't want to delete that folder.

Can anyone help me come up with a good way of doing this?

I stink at creating these batch files and scripts.
0
ChiefIT
Asked:
ChiefIT
3 Solutions
 
sharkbot221984Commented:
Place the script that you want to run to map the drives on here: \\domaincontroller\netlogon\filename.bat.  Then in the user's profile in AD users and computers under the profile tab, put filename.bat in the logon script.  When the user logs in to their machine the logon script will execute.
0
 
Darius GhassemCommented:
Hi Chief,

Instead of using Net Use * \Delete /yes why not use Net Use K \Delete /yes. K= drive letters that already exist. So put this line in for each drive letter which I have done at most sites.
0
 
Donald StewartNetwork AdministratorCommented:
Hey Chief,

I like this way better(VBS)


'Disconnect ALL mapped drives
dim WshNetwork

Set WshNetwork = CreateObject("WScript.Network")
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
    WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next
'Map the Drive
  WshNetwork.MapNetworkDrive "K:", "\\computer\share",True
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Donald StewartNetwork AdministratorCommented:
Ps their home folder mapping will be recreated since you have defined it in AD profile.
0
 
ChiefITAuthor Commented:
The net use command is the way I was doing things before. They all of a sudden quit working.

So, I went into Joe.Testuser account to watch the command window and found they were getting access denied. Then, I went to the command prompt and tried to manually run net use .... from there with access denied. I think it safe to assume, if you are not the domain or local admin net use is denied from running. That leads me to believe that net use is not the way to go any more.

I do like the above VB sript and would like a bit more info on what it does.. I have up to ten drives to map on some users.
0
 
Donald StewartNetwork AdministratorCommented:
Did you change any ntfs or share permissions on the shares?

Here's a cleaned up version of what I use to map my drives.

There are some other things in the script like updating a 3rd party app. or installing antivirus software.


Dim WshShell, WshNetwork, objDomain, DomainString, UserString, UserObj, exePath
Dim Username, SysDrive
Dim objFSO
 
Set WshNetwork = CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
SysDrive = WshShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%")
SysRoot = WshShell.ExpandEnvironmentStrings ("%SystemRoot%")
UserProfile = WshShell.ExpandEnvironmentStrings ("%UserProfile%")
'Automatically find the domain name
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")
 
'Grab the user name
UserString = Lcase(WshNetwork.UserName)
'wscript.echo UserString
Set objADSysInfo = CreateObject("ADSystemInfo")
'wscript.echo objADSysInfo.UserName
Set objCurrentUser = GetObject("WinNT://" & WshNetwork.UserDomain & "/" & WshNetwork.UserName & ",user")
'wscript.echo objCurrentUser.FullName
 
'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://"& DomainString &"/"& UserString & "",user)
 
'wscript.echo UserObj.groups
'Grab the computer name for use in add-on code later
strComputer = WshNetwork.ComputerName
'************************************************************************************************************************************************
'Check if this is a Server. If this is a server quit
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colItems
    If InStr(1,objItem.Caption,"Server") Then Wscript.Quit
    'If InStr(1,objItem.Caption,"Vista") Then Wscript.Quit
Next
'===========================================================================================================
'Do not run this script on the following machines or servers
        strComputer = WshNetwork.ComputerName
        arrComputers = Array("server1","server2","serverbdc","server3","server5")
    For Each arrayElement in arrComputers
        If arrayElement = strComputer Then
            wscript.Quit
        End If 
    Next
 
 
 
 
'===========================================================================================================
'Disconnect ALL mapped drives
 
 
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
    WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next
ON ERROR GOTO 0
'===========================================================================================================
'THE FOLLOWING CODE WILL DETERMINE THE DEFAULT GATEWAY AND MAP DRIVES ACCORDINGLY
'===========================================================================================================
 
Function DefaultGateway 
        DefaultGateway = Empty 
        Dim oDG, oDGs, WMI 
        Set WMI = GetObject("winmgmts:\\.\root\cimv2")           
        Set oDGs = WMI.ExecQuery _         
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 
        For Each oDG In oDGs 
                If Not IsNull(oDG.DefaultIPGateway) Then 
                If Not oDG.defaultIPGateway(0) = "0.0.0.0" Then 
                DefaultGateway = oDG.DefaultIPGateway(0) 
                        Exit For 
                        End If 
                        End If 
        Next 
End Function 
 
 
Select Case DefaultGateway 
        Case "10.63.106.2" 
                WshNetwork.MapNetworkDrive "V:", "\\server1\share\vision",True 
		
        Case "10.63.106.130" 
                WshNetwork.MapNetworkDrive "V:", "\\server2\share\vision",True
		 
        Case "10.63.106.162" 
                WshNetwork.MapNetworkDrive "V:", "\\server4\share\vision",True
		 
        Case "10.63.106.194" 
                WshNetwork.MapNetworkDrive "V:", "\\server6\share\vision",True
		
        Case "10.63.106.226" 
                WshNetwork.MapNetworkDrive "V:", "\\server7\share\vision",True
		
        Case "10.63.107.2" 
                WshNetwork.MapNetworkDrive "V:", "\\server3\share\vision",True
		
        Case "10.63.107.34" 
                WshNetwork.MapNetworkDrive "V:", "\\server5\share\vision",True
		
        Case "10.63.107.66" 
                WshNetwork.MapNetworkDrive "V:", "\\server8\share\vision",True
		
        Case "10.63.107.98" 
                WshNetwork.MapNetworkDrive "V:", "\\server9\share\vision",True
			
        Case "10.63.107.130" 
                WshNetwork.MapNetworkDrive "V:", "\\server10\share\vision",True
		
        Case "10.63.107.162" 
                WshNetwork.MapNetworkDrive "V:", "\\server11\share\vision",True
		
        Case "10.63.107.194" 
                WshNetwork.MapNetworkDrive "V:", "\\server12\share\vision",True
		
End Select
      Set wshShell = WScript.CreateObject ("WSCript.shell")
      Dim AllProcess 
      Dim Process 
      Dim strFoundProcess
      trendpath="\server1\public\local\"
      strFoundProcess = False 
      Set AllProcess = getobject("winmgmts:") 'create object 
      For Each Process In AllProcess.InstancesOf("Win32_process") 'Get all the processes running in your PC 
      If (Instr (Ucase(Process.Name),"TMLISTEN.EXE") = 1) Then 'Made all uppercase to remove ambiguity.
      strFoundProcess = True 
      Exit for 
    End If 
Next 
      If strFoundProcess = False Then 
         	Set wshShell = WScript.CreateObject ("WSCript.shell")
	msgbox "INSTALLING TREND ANTIVIRUS.....Please Wait" & vbCrLf & vbCrLf & "Installing from this location:" & trendpath
        	 wshshell.run trendpath & "trend090508.exe", 3, false
      End If
 
ON ERROR RESUME NEXT
'The following checks whether or not a 3rd party application is at latest version and updates it
'(you will need psexec.exe located in same folder as this script)
Const ForReading = 1
Dim instname, pwd, exepath
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Program Files\BSIapps\Artalending\Version.ini", ForReading)
exepath = "\server1\public\local\Arta\Setup.exe"
For i = 1 to 2
    objTextFile.ReadLine
Next
 
strLine = objTextFile.ReadLine
if strline="" then
	'end if
Else 
if strLine <>"ReleaseNumber=9.10" then 
	Wscript.Echo "Your Arta Application is not up to date. It will now be updated. Please just use all defaults(clicking on next) and install it."
	wshshell.Run "psexec.exe \\" & strcomputer & " -accepteula -c -f -i " & ExePath,0,False
	'wshshell.run """\server1\public\local\Arta\Setup.exe""",3,false
	end if
end if
objTextFile.Close
'===========================================================================================================
' on error resume next 
          
'Now check for group memberships and map appropriate drives
For Each GroupObj In userobj.Groups
 Select Case Lcase(GroupObj.Name)
 
 
        Case "accounting"
            WshNetwork.MapNetworkDrive "W:", "\\server1\Accounting$",True
              WSHNetwork.RemoveNetworkDrive "V:", True, True 
        	Set objFSO = CreateObject("Scripting.FileSystemObject")
       	    	 strDsk = WshShell.SpecialFolders("Desktop")
       	 	' What is the label for the shortcut?
       	   	  strshortcut = strDsk & "\Accounting.lnk"
       		 If Not objFSO.FileExists(strshortcut) Then
          	  SET oUrlLink = WshShell.CreateShortcut(strshortcut)
        	' What is the path to the shared folder?
           	 oUrlLink.TargetPath = "\\server1\Accounting$\"
           	 oUrlLink.Save
       		 End If
 
        Case "loan officers"
            WshNetwork.MapNetworkDrive "L:", "\\server1\Loan Officer Resources",True
              WSHNetwork.RemoveNetworkDrive "V:", True, True 
        	Set objFSO = CreateObject("Scripting.FileSystemObject")
       	    	 strDsk = WshShell.SpecialFolders("Desktop")
       	   	  strshortcut = strDsk & "\Loan Officer Resources.lnk"
       		 If Not objFSO.FileExists(strshortcut) Then
          	  SET oUrlLink = WshShell.CreateShortcut(strshortcut)
           	 oUrlLink.TargetPath = "\\server1\Loan Officer Resources\"
           	 oUrlLink.Save
       		 End If
 
        Case "loanservice"
            WshNetwork.MapNetworkDrive "G:", "\\server1\Processing Letters",True
              WSHNetwork.RemoveNetworkDrive "V:", True, True 
        	Set objFSO = CreateObject("Scripting.FileSystemObject")
       	    	 strDsk = WshShell.SpecialFolders("Desktop")
       	   	  strshortcut = strDsk & "\Processing Letters.lnk"
       		 If Not objFSO.FileExists(strshortcut) Then
          	  SET oUrlLink = WshShell.CreateShortcut(strshortcut)
           	 oUrlLink.TargetPath = "\\server1\Processing Letters\"
           	 oUrlLink.Save
       		 End If
       
        Case "data processing" 
            WshNetwork.MapNetworkDrive "Q:", "\\server1\data docs$",True
        	Set objFSO = CreateObject("Scripting.FileSystemObject")
       	    	 strDsk = WshShell.SpecialFolders("Desktop")
       	   	  strshortcut = strDsk & "\Data Documents.lnk"
       		 If Not objFSO.FileExists(strshortcut) Then
          	  SET oUrlLink = WshShell.CreateShortcut(strshortcut)
           	 oUrlLink.TargetPath = "\\server1\data docs$\"
           	 oUrlLink.Save
       		 End If
       	   	  strshortcut = strDsk & "\Internal Data Templates.lnk"
       		 If Not objFSO.FileExists(strshortcut) Then
          	  SET oUrlLink = WshShell.CreateShortcut(strshortcut)
           	 oUrlLink.TargetPath = "\\server1\share\ templates\internal data document.html"
           	 oUrlLink.Save
       		 End If
 
 
        Case "tellers"
            WshNetwork.SetDefaultPrinter "HP LaserJet P2015 Series PCL 6"
 
        Case "savings"
            WshNetwork.SetDefaultPrinter "HP LaserJet P2015 Series PCL 6"
            
        Case "it dept" 
            WshNetwork.MapNetworkDrive "P:", "\\server1\logs$",True
            WshNetwork.MapNetworkDrive "Q:", "\\server1\patches",True
	    WshNetwork.MapNetworkDrive "I:", "\\server1\IT Stuff$",True
	    WshNetwork.MapNetworkDrive "R:", "\\server1\audits\GFI REPORTS\Reviewed Reports",True
	    WshNetwork.MapNetworkDrive "S:", "\\server1\audits",True
	    WSHNetwork.RemoveNetworkDrive "V:", True, True
            
            'Below is an example of how to set the default printer
            'WshNetwork.SetDefaultPrinter "\\mycomputer\HP LaserJet 1020"
            'objFSO.CopyFile "\\server1\share\Your Company Name VPN.pbk", UserProfile & "\Desktop\", overwrite = False
 
        Case "marketing"
             WshNetwork.MapNetworkDrive "N:", "\\server1\Marketing",True
	     WSHNetwork.RemoveNetworkDrive "V:", True, True
 
                        
        Case "annuities"
	     WSHNetwork.RemoveNetworkDrive "V:", True, True
                        
        Case "executives"
	     WSHNetwork.RemoveNetworkDrive "V:", True, True
 
        Case "information"
	     WSHNetwork.RemoveNetworkDrive "V:", True, True
 
        Case "insurance"
	     WSHNetwork.RemoveNetworkDrive "V:", True, True
 
	Case "loan processing"
            WshNetwork.MapNetworkDrive "U:", "\\server1\accounting$\Accounting Journals",True
        Set objFSO = CreateObject("Scripting.FileSystemObject")
            strDsk = WshShell.SpecialFolders("Desktop")
            strshortcut = strDsk & "\Accounting Journal.lnk"
        If Not objFSO.FileExists(strshortcut) Then
            SET oUrlLink = WshShell.CreateShortcut(strshortcut)
            oUrlLink.TargetPath = "\\server1\accounting$\Accounting Journals\Journal.xlt"
            oUrlLink.Save
        End If
        
    End Select
Next
 
 
'Run Code based on a User, That is map a drive a shortcut etc
 
 
Select Case UserString
'The following will add a shortcut to the desktop for these Users only: user1 
'===========================================================================================================
	Case "user1"
 
            WshNetwork.MapNetworkDrive "U:", "\\server1\accounting$\Accounting Journals",True
        Set objFSO = CreateObject("Scripting.FileSystemObject")
            strDsk = WshShell.SpecialFolders("Desktop")
            strshortcut = strDsk & "\Accounting Journal.lnk"
        If Not objFSO.FileExists(strshortcut) Then
            SET oUrlLink = WshShell.CreateShortcut(strshortcut)
            oUrlLink.TargetPath = "\\server1\accounting$\Accounting Journals\Journal.xlt"
            oUrlLink.Save
        End If
end select
 
'wscript.echo "Drive mapping completed Successfully"
'===========================================================================================================
'Now rename the mapped drives to a meaningful name
'===========================================================================================================
on error resume next
   Dim DriveArray( 10, 1 )
   DriveArray( 0, 0 ) = "V:\"
   DriveArray( 0, 1 ) = "Account Generator"
   DriveArray( 1, 0 ) = "L:\"
   DriveArray( 1, 1 ) = "Loan Officer Resources"
   DriveArray( 2, 0 ) = "N:\"
   DriveArray( 2, 1 ) = "Marketing"
   DriveArray( 3, 0 ) = "I:\"
   DriveArray( 3, 1 ) = "IT Stuff"
   DriveArray( 4, 0 ) = "P:\"
   DriveArray( 4, 1 ) = "User Logons"
   DriveArray( 5, 0 ) = "Q:\"
   DriveArray( 5, 1 ) = "Patches"
   DriveArray( 6, 0 ) = "G:\"
   DriveArray( 6, 1 ) = "Processing Letters"
   DriveArray( 7, 0 ) = "K:\"
   DriveArray( 7, 1 ) = "LPL Private Folder"
   DriveArray( 8, 0 ) = "U:\"
   DriveArray( 8, 1 ) = "Accounting Journal"
   DriveArray( 9, 0 ) = "R:\"
   DriveArray( 9, 1 ) = "Reviewed Reports"
   DriveArray( 10, 0 ) = "S:\"
   DriveArray( 10, 1 ) = "Audits"
 
Set WshShell = CreateObject("Shell.Application")
For remap = 0 to UBound(DriveArray)
 
WshShell.NameSpace(DriveArray(remap,0)).Self.Name = DriveArray(remap,1)
Next

Open in new window

0
 
ChiefITAuthor Commented:
For some folks the net use command might work. I am going to switch to VB scripts and start learning these. So, I really do appreciate the help on netuse that was offered above, but I found the help on the VB script more up my alley.

Well done dstwrtjr:

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now