# Best way to map a drive on windows

I need to have puppet map a drive for a windows user. I saw there was as similar question here https://ask.puppetlabs.com/question/4... 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 close merge delete ## 2 Answers Sort by » oldest newest most voted Outside of puppet it's as simple as • net use x: \\networkdrive\share /P:YES - Reference http://www.howtogeek.com/118452/how-t... - the problem is that it is not very idempotent, so you get errorlevel 2 if it can't map it, and you get errorlevel 2 if it has already been mapped. :( • One could use PowerShell, but be aware that the New-PSDrive is not persistent, and doesn't show up outside of the POSH session. Unless you are in Powershell v3, in which case you can add -Persist and credentials to the drive, e.g. New-PSDrive –Name “K” –PSProvider FileSystem –Root “\\touchsmart\share” –Persist - Reference http://technet.microsoft.com/en-us/library/hh849829(v=wps.620).aspx & http://www.howtogeek.com/132354/how-to-map-network-drives-using-powershell/ • However there is another way, which is to use PowerShell to get to a COM object, that only errors when it is supposed to error (this works all the way back to Posh v2): networkdrive.ps1 $exitcode = 0
$network = (New-Object -Com WScript.Network) # See network drives #$networkDrives = $network.EnumNetworkDrives() # Remove a Drive #$network.RemoveNetworkDrive("x:")

$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 http://powershell-tips.blogspot.com/2012/05/powershell-script-to-map-network-drive.html 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.

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.

( 2015-12-05 09:50:18 -0500 )edit

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

( 2016-06-17 11:27:11 -0500 )edit

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)"',
}

more