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.
This topic is 8 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jramon.r@tv3.cat
Posts: 25
Last visit: Tue Jan 11, 2022 4:23 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
Last visit: Thu Oct 22, 2015 1:10 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Wed Nov 14, 2018 11:39 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: 25
Last visit: Tue Jan 11, 2022 4:23 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Mapping a network drive

Post by jvierra »

What version of PowerShell is selected in Powershell Studio?
User avatar
jramon.r@tv3.cat
Posts: 25
Last visit: Tue Jan 11, 2022 4:23 am

Re: Mapping a network drive

Post by jramon.r@tv3.cat »

The version is 4.2.94.0
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 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: 25
Last visit: Tue Jan 11, 2022 4:23 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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.
This topic is 8 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked