Ask Your Question

Best way to map a drive on windows

asked 2014-06-02 16:45:15 -0600

poobert gravatar image

updated 2014-07-16 16:57:08 -0600

I need to have puppet map a drive for a windows user. I saw there was as similar question here but this had very little explanation and the gist is gone now.

I know I could just have an exec in a node definition but that seems very un-puppetlike. I'm thinking I should create a new resource, something like "mapped drive" but being totally new to puppet, I'm kind of at a loss how to accomplish this.

Edit: This turned out to be unnecessary for my use case. However I'm revisiting in an attempt to learn more puppet. I have attempted your suggestion #3 and am stymied. A simple $network.MapNetworkDrive maps the drive fine. AS A USER. Puppet runs as admin though and admin mapped drives are not visible to users. I set EnableLinkedConnections=1 in registry solved this FOR MANUAL runs. When puppet runs my provider code, which is mainly just a Test-Path in exists?, then MapNetworkDrive and correspondign remove in destroy. I thought it should be as simple as that however it seems puppet running those same commands does not update the drives existence in explorer as when I run the exact same command from a admin powershell prompt. A more disconcerting problem is when the drive is removed by puppet. The drive remains in explorer as disconnected and unremovable and "nonexistent." I have tracked down many posts and discussions on these shortcomings on windows and am beginning to see why puppet has not implemented this functionality yet.

What is this discrepancy in how puppet is running the command?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2014-06-03 10:23:20 -0600

updated 2014-06-03 10:31:32 -0600

Outside of puppet it's as simple as


$exitcode = 0
$network = (New-Object -Com WScript.Network)

# See network drives
#$networkDrives = $network.EnumNetworkDrives()

# Remove a Drive
# $network.RemoveNetworkDrive("x:")

# Add a Drive
$network.MapNetworkDrive("x:","\\vmware-host\Shared Folders\somefolder", $true)

if (! $?) {
  #function failed
  $exitcode = 1
Write-Host "Exiting with exit code $exitcode"
exit $exitcode

Reference to for an example implementation.

I don't believe we have a custom type/provider in Puppet yet to do this, but one could easily adapt the above and take advantage of the PowerShell provider to use the above to create a simple type and provider.

I also exported the original Redmine ticket to JIRA asking if this is still an issue we'd like to pursue a custom type/provider for.

edit flag offensive delete link more


Has there been any progress on this regard? Manage network shares mappings is vital for any Windows Administrator nowdays, so i think we are all looking forward to seeing a module/provider that help us with it.

Yeayu gravatar imageYeayu ( 2015-12-05 09:50:18 -0600 )edit

Vote the JIRA ticket up/comment, we prioritize and track work in our ticketing system. Thanks!

rob gravatar imagerob ( 2016-06-17 11:27:11 -0600 )edit

answered 2014-10-23 06:04:25 -0600

PuppetGossin gravatar image

A simple way to make net use idempotent is to use the unless attribute, e.g this is what I use (on 2012r2):

exec { 'winmountscratch':
    command => 'C:\Windows\System32\net.exe use S: \\files01\scratch /USER:Guest',
    unless  => 'C:\Windows\System32\cmd.exe /c "if exist \\files01\scratch (exit 0) else (exit 1)"',
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2014-06-02 16:45:15 -0600

Seen: 4,740 times

Last updated: Oct 23 '14