Sharepoint persistent path for PowerShell
I am trying to map the path to a SharePoint document library in a persistent way. The weird thing is that this works great:
$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 teams both use the same PSProvider
, but one is persistent and the other is not. How can I get this permanent without going back to net use
?
+3
source to share
1 answer
I ran into this issue a few weeks ago in a script that mysteriously stopped working while I was developing it, it seems to be a Windows 66 bug, not Powershell as described here
Here is an alternative to using the web using credentials
# map drive using credentials
(New-Object -ComObject WScript.Network).MapNetworkDrive("$LocalDrive","\\$computer\$drive",$false,$($credentials.username),$($credentials.GetNetworkCredential().password))
I usually 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
Maybe just a reboot will fix the problem, because today I can't recreate the problem.
+3
source to share