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 ?
- Edited by Sebastien Girard Tuesday, May 26, 2015 6:33 PM