1

We have 2008 R2 and I'm writing a PowerShell script to copy files to a network share. Unfortunately, it appears that there is a PowerShell issue with mapped drives. It looks like someone found a workaround poss fix, but I'm not understanding it, or something I have is different than this example. Has anyone had any luck with this? Our network drive is mapped in My Computer like this:

20xServerBackup (\\usa0xxxx.na.xxx.net)(Z:)

I've tried a few things in my script, but this is how it looks right now:

runas /user:administrator net use Y: "\\usa0xxxx.na.xxx.net"

$ToLocation = "Y:\VMs\name"

$FromLocation = "C:\backup\iceCreamCake.txt"

Copy-Item "$FromLocation" "$ToLocation"

The error I get on the copy line is

Copy-Item: Cannot find drive. A drive with the name 'Y' does not exist.

I'm not sure where to find what version of powerShell I have, if that makes a difference. But, I'm pretty sure I upgraded to version 3.0 last week to use the hyperV get-vm/start/stop vm functionality.

It sounds like the Microsoft website lists this as an active error/problem, but it sounds like someone had a workaround on this (stackoverflow) website, link above, but I'm not sure why it's not working for me. When I first mapped the drive yesterday, my script was running and copying files to the network share. Today it isn't, even after a reboot. I tried re-mapping the drive with a new name and it didn't work any different. I've tried using the Z drive in my code instead of the Y drive as I created in my script above, and it didn't work any differently. It still had the same error message.

I also tried

New-PSDrive -Name YY -PSProvider FileSystem -root "\\usa0xxxx.na.xxx.net"

at the powerShell command line. I'm not sure if I got everything right in that command. It said this error

New-PSDrive: Drive root "\usa0xxxx.na.xxx.net" does not exist or it is not a folder.

Community
  • 1
  • 1
Michele
  • 3,617
  • 12
  • 47
  • 81
  • 2
    Why use a mapped drive? Can you use the UNC path? Why is it important to use the mapped drive? I ask because I really want to understand why anyone prefers to use mapped drives over the UNC paths. – Bobort Feb 11 '14 at 20:00
  • Can you clarify the UNC path? We weren't given the IP address for the network share we're trying to copy to, in case that's what you mean. Do you have an example? – Michele Feb 11 '14 at 20:01
  • 1
    Oh, I mean the path with the two-back slashes "\\". So maybe something like this: `Copy-Item "C:\backup\iceCreamCake.txt" "\\usa0xxxx.na.xxx.net\VMs\name"`. – Bobort Feb 11 '14 at 20:08
  • I typed that at the PowerShell command line and the error is "The network name cannot be found." – Michele Feb 11 '14 at 20:12
  • I can open the mapped network directory from myComputer, so it's accessible. – Michele Feb 11 '14 at 20:15
  • @Bobort - I have no preference over UNC vs mapped drive. I'm trying to use examples I find online and found more with mapped drives. – Michele Feb 11 '14 at 20:24

1 Answers1

6

The problem is that you need to specify a folder when you map drives. In your code samples (both the net use sample and the New-PSDrive sample) you only specify which computer the share exists on, but not the share name. I'm guessing the shared folder name is "20xServerBackup" which means that in order to map a psdrive to it you should do:

New-PSDrive -Name YY -PSProvider FileSystem -root "\\usa0xxxx.na.xxx.net\20xServerBackup"

The same goes for net use.

I do, however, agree with @Bobort from his/her comment above that I would probably use the UNC path instead of mapping a drive. Just do:

Copy-Item "C:\backup\iceCreamCake.txt" "\\usa0xxxx.na.xxx.net\20xServerBackup\VMs\name"
Robert Westerlund
  • 4,750
  • 1
  • 20
  • 32
  • I will give that a try soon, but right now we decided to put this part off until march. Thanks! – Michele Feb 13 '14 at 15:03
  • It worked! Instead of using Y or Z, I used "\\usa0xxxx.na.xxx.net\20xServerBackup\VMs\". Very good idea! – Michele Feb 14 '14 at 19:06
  • You can use the UNC path unless the reason you are mapping the drive is to shorten the path because of MAXPATH issues. Same goes for using subst to shorten local paths as well. – Kory Gill Jul 21 '16 at 16:59