Network map with logon script

Hello,

I have a strange issue with my logon script in powershell with some of my users. In my logon script, I want to map a network drive. Here is my code for doing that :

$networkDriverLetter = "N:"

if(-not (Test-Path -Path $networkDriverLetter))
{
	# Unmap network drive if present
	try
	{
		$net = $(New-Object -ComObject WScript.Network)
		$net.RemoveNetworkDrive($networkDriverLetter, $true, $true)
		$msgUnmapNetworkDrive =  "Unmap network drive " + $networkDriverLetter + " for user " + [Environment]::UserName
		Write-EventLog -LogName "Application" -Source "Application" -EventId 9876 -EntryType "Warning" -Message $msgUnmapNetworkDrive -Category 0 -ErrorAction SilentlyContinue
	}
	catch
	{
		# Do nothing, network drive might not be mapped
		$msgDoNothingNetworkDrive =  "Do nothing, network drive might not be mapped for user " + [Environment]::UserName
		Write-EventLog -LogName "Application" -Source "Application" -EventId 9876 -EntryType "Warning" -Message $msgDoNothingNetworkDrive -Category 0 -ErrorAction SilentlyContinue
	}
	
	
	# Map network drive pointing to customer app DB folder
	$mapDestPath = $appSharePath + "\" + $customerNumber + "\DB"
	$net.MapNetworkDrive($networkDriverLetter, $mapDestPath, $true)
	$msgMapNetworkDrive =  "Map network drive " + $networkDriverLetter + " for user " + [Environment]::UserName
	Write-EventLog -LogName "Application" -Source "Application" -EventId 9876 -EntryType "Warning" -Message $msgMapNetworkDrive -Category 0 -ErrorAction SilentlyContinue
}




When the script run, I have this in my log :

Do nothing, network drive might not be mapped for user

.Map network drive N: for user USER01

This log is added after the "$net.MapNetworkDirve..."

But, in the log "Microsoft-Windows-PowerShell/Operational", I have this message :

Error Message = Could not find the drive 'N:\'. The drive might not be ready or might not be mapped.

Provider name = Microsoft.PowerShell.Core\FileSystem


Context:
        Severity = Warning
        Host Name = ConsoleHost
        Host Version = 4.0
        Host ID = bce11ead-1dab-492d-a930-7563f2fed196
        Host Application =  -ExecutionPolicy ByPass -File \\domain.lan\sysvol\domain.lan\scripts\ConfigureAppSession.ps1
        Engine Version = 4.0
        Runspace ID = 0dd628e5-44d4-4f8a-87db-05fcd7ccfbd6
        Pipeline ID = 1
        Command Name = 
        Command Type = 
        Script Name = 
        Command Path = 
        Sequence Number = 22
        User = DOMAIN\USER01
        Shell ID = Microsoft.PowerShell


User Data:

The event log added by my script say the network drive is mapped, but powershell says something goes wrong. The script does not work every time. Sometimes everything is good, and sometimes something not work.

Can you help me ? 



May 26th, 2015 6:32pm

Yes, I know that ;) But, in my situation, I have something like that in my domain. Every OU on first level is a customer. Customer are created with powershell script at any time. For the moment, I have 300 OU under customer.

Customer OU
  0001 OU
    0001.user
  0002 OU
    0002.user
  0003 OU
    0003.user
  ...
  0023 OU
    0023.user


Now, for every user from a OU, I need to map N:\ to \\FILER\OU\Folder, so for 0001.user N: will be \\FILER\0001\Folder.

I don't want to do one GPO by customer OU. It will be quickly out of control. So, for the moment I think a logon script is the best approach. What do you think ?



Free Windows Admin Tool Kit Click here and download it now
May 26th, 2015 7:58pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics