Question

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.

Was it helpful?

Solution

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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top