PowerShell map persistent sharepoint path - powershell

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?

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.

I had a similar issue, turns out Windows 2016 and 2019 needs WEBDav Redirection installed.
I was getting error 53, when trying to map a SharePoint library.

Related

Connect to a network share with different credentials

I have a .pptx file on a network share, where user 1 doesn't have access to.
Now I want to open this file with different credentials from a domain user who has access to.
How can i achieve that?
This is what I have
$cred = Get-Credential
New-PSDrive -Name "z" -Root "\\server1\folder1" -PSProvider "FileSystem" -Persist:$true -credential $cred -ea SilentlyContinue
start "\\server1\folder1\News-Ticker.pptx"
Remove-PSDrive -name "z"

Copy-Item from New-PSDrive within PSSession

I have the following Powershell code:
Enter-PSSession -ComputerName test01
New-PSDrive -Name Source -PSProvider FileSystem -Root \\test02\SMBTest -Credential test\Administrator
Copy-Item Source:\Test.txt -Destination C:\Temp
Remove-PSDrive Source
Exit-PSSession
When I execute each line on it's own it works, but when I save it and run it as a ps1 file it doesn't do anything.
Can anyone help explain why (I'm using Powershell --version 5.1)
Thanks #theincorrigible1 - I've modified it to the below and it is now working.
$s = New-PSSession -ComputerName test01
Invoke-Command -Session $s -ScriptBlock {
New-PSDrive -Name Source -PSProvider FileSystem -Root \\test02\SMBTest -
Credential test\Administrator
Copy-Item Source:\Test.txt -Destination C:\Temp
Remove-PSDrive Source
}

Configuring HKEY_CURRENT_USER for all Users with DSC

I'd like to overwrite an existing registry key in HKCU for all users on this device with Desired State Configuration.
I already found, that I could use the users credentials(Configuring HKEY_CURRENT_USER with DSC Resource actually updates HKEY_USERS\.DEFAULT), but I do not have them.
Any ideas?
Technically if your administrator on the Box you can still do this,
Load their Registery Hive and Change a Key.
$Username = "TestUser"
reg load hku\$Username C:\Users\$Username\NTUSER.DAT
New-PSDrive -Name $Username -PSProvider Registry -Root HKU\$Username
Set-Location "$($Username):\Control Panel\desktop\colors"
Get-ItemProperty -Path. -Name ActiveBorder
Set-ItemProperty -Path. -Name ActiveBorder -Value "218 208 200"
Get-ItemProperty -Path. -Name ActiveBorder
Remove-PSDrive -Name $Username -Force

using new-psdrive name as source variable for msdeploy source parameter does not work

// Create a new powershell drive
$psw = ConvertTo-SecureString %deployment.password% -AsPlainText -force
$cred = new-object -TypeName System.Management.Automation.PSCredential -ArgumentList "MyUser, $psw
New-PSDrive -Credential $cred -Name ImportFolder -PSProvider FileSystem -Root \\MyShare\Exchange\MyFolder
// Create a full source path
$sourcePath = ImportFolder:\$versionPath\audio
// Deploy with msdeploy and the $sourcePath
$path = "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe";
$verb = "-verb:sync";
$src = "-source:contentPath=`"$sourcePath`"";
$dest = "-dest:contentPath=%TargetIISPath%,wmsvc='%Computername%:%deployment.iis.port%/msdeploy.axd',username=%system.Username%,password=%system.Password%";
Invoke-Expression "&'$path' --% $verb $src $dest -verbose -allowUntrusted";
Remove-PSDrive ImportFolder
I get the error:
The term 'ImportFolder:\$versionPath\audio' is not recognized as the name of a
[21:07:32][Step 6/7] cmdlet, function, script file, or operable program. Check the spelling of the
[21:07:32][Step 6/7] name, or if a path was included, verify that the path is correct and try
[21:07:32][Step 6/7] again.At
The problem is that msdeploy assumes the sourcePath ImportFolder is literally named "ImportFolder" but actually it is a powershell drive with a mapped path...
How can I fix that?
I do NOT want to use "net use X:\ path" because its buggy in my environment.
This might be a bit old, but you can just use a Name with a letter for your PSDrive, and then you can use that drive letter with msdeploy
New-PSDrive -Credential $cred -Name "X" -PSProvider FileSystem -Root \\MyShare\Exchange\MyFolder
$sourcePath = "X:\$versionPath\audio"

How to use PowerShell and robocopy to backup files to a server?

I am just starting with PowerShell, so please be kind.
All I want to do is backup my directories and files from my laptop to the desktop computer, i.e. "server", using PowerShell and robocopy. I am the administrator to both machines (Windows 7).
This fails with access denied on the "server", i.e., desktop, despite the permissions being set for "Everybody" to do everything.
Any help (or better way) is really appreciated! Thanks.
$cred=get-credential
$sourcepath = ("\\localhost\C$\nova5");
$TargetPath = ("\\library\E$\nova5");
New-PSDrive -Name source -PSProvider FileSystem -Root $SourcePath
New-PSDrive -Name target -PSProvider FileSystem -Root $TargetPath -Credential $cred
robocopy source target /e;
return;
Psdrive is a feature for powershell cmdlet not for extrrnal command , change this line:
robocopy "\\localhost\C$\nova5" "$TargetPath" /e
You could try this:
$cred=get-credential
$sourcepath = C:\nova5 ;
$TargetPath = "\\library\E$\nova5"
New-PSDrive -Name target -PSProvider FileSystem -Root $TargetPath -Credential $cred
.\robocopy.exe $source $target "/e"