9bpraw
Last Updated: July 22, 2016
·
1.076K
· superboum
Avatar1

Windows Services can' t see mapped drives

You may need to start a service on Windows which use a mapped network drive.
The main reason would probably be that you want to avoid this lovely error : "PATH TOO LONG EXCEPTION".

TL;DR

You'll need to map the network drive IN your service. As mapped network drives seems to be only restored on an interactive logon.

An example with Powershell, WinSW and Jenkins

The following service will crash, as jenkins will not find the path:

<service>
  <id>jenkins</id>
  <name>Jenkins</name>
  <description>This service runs Jenkins continuous integration system.</description>
  <env name="JENKINS_HOME" value="z:\"/>
  <executable>java.exe</executable>
  <arguments> -jar C:\jenkins.war</arguments>
  <logmode>rotate</logmode>
</service>

We will mount the network drive inside the service. The solution I found is to delegate this logic to Powershell.
So our updated service will look like this:

<service>
  <id>jenkins</id>
  <name>Jenkins</name>
  <description>This service runs Jenkins continuous integration system.</description>
  <executable>C:\windows\System32\WindowsPowerShell\v1.0\powershell</executable>
  <arguments>-File "C:\jenkins.ps1"</arguments>
  <logmode>rotate</logmode>
</service>

And the jenkins script will mount the network drive, then start Jenkins.

if (-not (Test-Path 'z:')) {
    net use z: \\server\a\random\folder /persistent:yes
}

$env:JENKINS_HOME='z:'
java.exe -jar "C:\ci\jenkins_bin\jenkins.war"

Source

StackOverflow - Map a network drive to be used by a service

Say Thanks
Respond
Filed Under