Mapping a network drive

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
jramon.r@tv3.cat
Posts: 21
Joined: Mon Jan 19, 2015 2:32 am

Mapping a network drive

Post by jramon.r@tv3.cat »

I'm trying to map a network drive in a script by two ways but any of them don't work

One is

#New-PSDrive -Name "Z" -PSProvider FileSystem -Root '\\server\share' -Persist

It doesn't show any error but it doesn't appear in the windows explorer

The other way is

$net = $(New-Object -ComObject wscript.Network)
$net.mapnetworkdrive('z:', $path)

This shows me the next error

Exception calling "MapNetworkDrive" with "2" argument(s): "Object reference not set to an instance of an object.

Thanks

User avatar
SAPIEN Support Forums
Posts: 945
Joined: Wed Dec 03, 2014 2:26 pm

Mapping a network drive

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, jramon.r@tv3.cat.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Mapping a network drive

Post by jvierra »

YOu forgot to remove the comment tag '#'

New-PSDrive -Name Z -PSProvider FileSystem -Root '\\server\share' -Persist

No need to quote drive name in this case.

Don't forget to change \\server\share to your names.

User avatar
dan.potter
Posts: 709
Joined: Wed Oct 08, 2014 6:02 am

Re: Mapping a network drive

Post by dan.potter »

jvierra wrote:YOu forgot to remove the comment tag '#'

New-PSDrive -Name Z -PSProvider FileSystem -Root '\\server\share' -Persist

No need to quote drive name in this case.

Don't forget to change \\server\share to your names.
# :P

User avatar
jramon.r@tv3.cat
Posts: 21
Joined: Mon Jan 19, 2015 2:32 am

Re: Mapping a network drive

Post by jramon.r@tv3.cat »

I've removed the tag # and the "" to the letter but it stills doesn't work.

If I run this in the PowerShell ISE it works ok but if I run this in PowerShell Studio doesn't appear the letter in the windows explorer.

What is happening?

Thank you

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Mapping a network drive

Post by jvierra »

What version of PowerShell is selected in Powershell Studio?

User avatar
jramon.r@tv3.cat
Posts: 21
Joined: Mon Jan 19, 2015 2:32 am

Re: Mapping a network drive

Post by jramon.r@tv3.cat »

The version is 4.2.94.0

User avatar
Alexander Riedel
Posts: 7529
Joined: Tue May 29, 2007 4:43 pm
Answers: 1
Been upvoted: 3 times

Re: Mapping a network drive

Post by Alexander Riedel »

That is the PowerShell Studio version and not the selected PowerShell version. Regardless, you did not specify your OS or version as asked in the auto-response post, but I am assuming Windows 7 or above.
So in that case mapping drives requires elevation.
The ISE runs elevated by default (I will not comment on that practice) but PowerShell Studio does not.
Run your script elevated, there is a button on the ribbon to turn that on.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
jramon.r@tv3.cat
Posts: 21
Joined: Mon Jan 19, 2015 2:32 am

Re: Mapping a network drive

Post by jramon.r@tv3.cat »

The version of PowerShell is 4.0 and the O.S is Windows 2008, it's a server.

I've already executed the script elevated.

Thanks

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Mapping a network drive

Post by jvierra »

Mapping drives does not require elevation. Even lowly users can map a drive. Proof is that we do it all of the time in logon scripts.

Group Policy can affect this.

In Vista days there was a bug that would not allow a newly mapped drive to be visible between an elevated and a normal session.

Windows 2008 is now out of support so I have no systems to test on. Try searching for information on this bug. I seem to remember that there was a registry hack to override this behavior.

This is not really a scripting issue. It is more of a system issue.

Locked