Connect to a network share with different credentials - powershell

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"

Related

PowerShell Access Network Share In Script As Different User

I am trying to access a domain network share in my PowerShell script that is currently running as NETWORK SERVICE. I have a domain user credential configured below.
$secStringPassword = ConvertTo-SecureString "password" -AsPlainText -Force
$shareCredential = New-Object System.Management.Automation.PSCredential ("DOMAIN\Username", $secStringPassword)
I would like to be able to run the following commands in the PowerShell script as the user specified above.
New-Item -Path "\\SERVER\Share\Folder" -ItemType Directory
Get-ChildItem "\\SERVER\Share\Folder"
Running the below is showing as not supported:
New-Item -Path "\\SERVER\Share\Folder" -ItemType Directory -Credential $shareCredential
"The New-Item cmdlet creates a new item and sets its value" If you're trying to connect to a share as a different user I suggest using new-psdrive first to create a mount as that user. This mounts that share as a drive so that it behaves more like a local location than a UNC path
New-PSDrive -Name "ShareNAME" -PSProvider "FileSystem" -Root "\\Server\Share" -Credential $shareCredential
New-Item -Path ShareNAME:\Folder -ItemType Directory -Credential $shareCredential
Get-ChildItem ShareNAME:\Folder -Credential $shareCredential

AUDIT_FAILURE when running Get-PSDrive from remote system

We run a script to connect to a remote server over a VPN and copy a file.
First we created a password file:
$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content C:\Scripts\password.txt
And then we use that password file to map a drive with Get-PSDrive and copy a file:
Get-PSDrive Q | Remove-PSDrive
$password = Get-Content C:\Scripts\password.txt | ConvertTo-SecureString 
$credential = New-Object System.Management.Automation.PsCredential("DOM\user", $password)
New-PSDrive -name "Q" -PSProvider FileSystem -Root \\192.168.1.100\Test -Credential $credential -Persist
Copy Q:\*.* C:\Scripts
This works, but we see AUDIT_FAILUREs in the event log which our monitoring system is flagging. It tries to use local credentials from where the script runs, which aren't valid and therefore causes the AUDIT_FAILURE.
Cannot see why we get this issue.

Powershell Remote Copy fails, locally it works

We have logfile on a server A in a different location (no UNC Path access) and we wish to copy the file to server B. This successfully works with Copy-Item -FromSession (run on Server B) as long as the file is closed. So we can successfully copy the previous day's logs but not today's.
$cred = Get-OurUserCredentials
$sess = New-PSSession -ComputerName $ServerA -Credential $cred -Authentication Negotiate
$LogFile = "D:\log\tomcat\access.20180227.log"
Copy-Item -FromSession $sess $LogFile "D:\logs\tomcat\" -Force
However, we can locally copy the active log of today if we run Copy-Item locally on server A. It's only Copy-Item -FromSession on server B which fails with:
Copy-Item : The process cannot access the file 'D:\log\tomcat\access.20180227.log' because it is being used by another process.
At line:11 char:2
As a workaround we could create a local task on server A to create a local copy but why is this necessary?
Why does Copy-Item behave differently when run remotely and can we "fix" it's behaviour so it copies the log remotely as it would locally.
A version of the answer proposed in the OP but avoiding the need for a scheduled task.
$cred = Get-OurUserCredentials
$sess = New-PSSession -ComputerName $ServerA -Credential $cred -Authentication Negotiate
#ScriptBlock to copy file locally
$SB =
{
#Create variables on the remote machine avoid havin gto pass to scriptblock
$LogFile = "D:\log\tomcat\access.20180227.log"
$TempDes = "temporarylocationhere"
Copy-Item -Path $LogFile -Destination $Des
}
#optional scriptblock to clean up
$SB2 =
{
Remove-Item -Path $TempDes -force
}
#Run the copy file scriptblock
Invoke-Command -Session $sess -ScriptBlock $SB
#Copy file
Copy-Item -FromSession $sess $TempDes "D:\logs\tomcat\" -Force #"
#Run clean up scriptblock
Invoke-Command -Session $sess -ScriptBlock $SB2
Have you considered using a PSDrive to map the remote location and then copy the file to or from that drive?
The New-PSDrive cmdlet creates temporary and persistent drives that
are mapped to or associated with a location in a data store, such as a
network drive, a directory on the local computer, or a registry key,
and persistent Windows mapped network drives that are associated with
a file system location on a remote computer.
Example using your code:
# Change the ServerNameToCopyTo below
New-PSDrive -Name "Remote" -PSProvider "FileSystem" -Root "\\ServerNameToCopyTo\log\tomcat\"
$LogFile = "D:\log\tomcat\access.20180227.log"
Copy-Item $LogFile "Remote:\" -Force

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
}

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?
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.