Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell map persistent sharepoint path

I'm trying to map the path to a SharePoint document library in a persistent way. It's strange that this works fine:

$Sharepoint = '\\domain.net\stuff\Documents\Folders - Permission matrix'
New-PSDrive -Name P -Root $Sharepoint -PSProvider FileSystem -Credential $Credentials

But this doesn't work:

New-PSDrive -Persist -Name P -Root $Sharepoint -PSProvider FileSystem -Credential $Credentials
New-PSDrive : The network resource type is not correct
At line:1 char:1
+ New-PSDrive -Persist -Name P -Root $Sharepoint -PSProvider FileSystem -Credentia ...

The commands are both using the same PSProvider but one is persistent and the other one not. How can I have this persistent without reverting to net use?

like image 534
DarkLite1 Avatar asked Dec 19 '25 14:12

DarkLite1


1 Answers

I ran into this problem a few weeks back in a script which mysteriously stopped worked whilst I was developing it, seems to be a Windows error 66 rather than Powershell as explained here

Here is an alternative to net use which uses credentials

# map drive using credentials
(New-Object -ComObject WScript.Network).MapNetworkDrive("$LocalDrive","\\$computer\$drive",$false,$($credentials.username),$($credentials.GetNetworkCredential().password))

I tend to use PSDrive like this

    # discover and delete
    if (Get-PSDrive -Name Results -ErrorAction SilentlyContinue) { Remove-PSDrive -Name Results -Scope Global | Out-Null }

    # create using credentials
    if ((New-PSDrive -Name Results -PSProvider FileSystem -Root "\\server\share" -Credential $Credentials -Scope Global -ErrorAction SilentlyContinue) -eq $null)  { $WSHShell.popup(“You do not have access to the results repository“,0,””,0) | Out-Null }

    # call from a separate function
    (Get-PSDrive -Name Results).root

It might just be that a reboot will solve the problem because I cannot recreate the issue today.

like image 192
beehaus Avatar answered Dec 24 '25 04:12

beehaus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!