I’m new to Powershell, so hoping to gain to experience/knowledge to become better!
I ran the below script to test if a file is on a remote computer, but the results returned as False even though I have verified the file is there through Windows Explorer. I tested the same script on my local computer and it returned as True (both files are on both remote and local computer).
PS C:\WINDOWS\system32> $cred = Get-Credential
$Session = New-PSSession -ComputerName “Computer1” -Credential $cred
(Test-Path “\$Session\c$\temp\test.txt”)
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
False
Additional Notes: I did get a prompt to enter my credentials and it was submitted successfully, which resulted in the False result. I confirmed this by entering bad credentials which resulted in ‘password is incorrect’.
Can you assist in trying to figure out why it is returning as false and how I can change it to it returns True correctly?
New-PSSession creates a PowerShell session using winrm to execute commands on another computer. To test a file you just need Test-Path like below.
Test-Path \\Computer1\C$\temp\test.txt
If credentials is required to connect to that computer you would do it like below
$cred = Get-Credential
# When using Session, you can do multiple invoke commands using the same session (there are other benefits as well)
$Session = New-PSSession -ComputerName "Computer1" -Credential $cred
Invoke-Command -Session $Session -ScriptBlock {Test-Path C:\temp\test.txt}
# Or
# When session is not used, credentials has to be mentioned for each invokes
Invoke-Command -ComputerName 'Computer1' -ScriptBlock {Test-Path C:\temp\test.txt} -Credential $Cred
If you are running your PowerShell session under the same user account that you would use to connect to the remote server the -Credential parameter can be excluded, as it will use that instance’s credentials.
Thanks so far. So now I’m trying to add in if statement. that is, if the file is there (True) then copy a file from my local computer to the network/destination computer (overwriting the destination folder). I have this so far, but it says network path was not found.
My remote server requires different credentials, so how would I either 1) pass alternate credentials, or 2) create a credential parameter into your script?
of course, you can pass a credential via Invoke-Command, but cannot be used with Test-Path and Copy-Item as its not yet supported even though its available for those cmdlets.
If you are in PS v 5.0 or later, you can use Copy-Item with -From/ToSession parameters. You can read more at below link.
You could also just open Powershell ISE or Powershell prompt as the elevated credentials. I test scripts that are going to be running as service accounts all the time by basically opening Powershell ISE and debugging using that account to ensure the account has the appropriate permissions.
I took your advice, along with another script I found online (link below) and ran PS ISE with the remote user credentials and do not have worry about any authentication. With some tweaks to the script in the below article I was able to achieve the desired goal, which is to copy files from the source to multiple servers only if the file exists on the destination server.
I’m still at it. With some help from another online forum and colleagues of mine, I am now using a ForEach statement with Invoke-Command. Good news is the script doesn’t error out, but the bad news is it does not do what I want it to do, which is to copy files from my local computer to a remote computer using alternative credentials. See script below. Can you help by telling me why the script might not be copy files to remote location as expected?
Local variables don’t get passed through into a remote session; you need to explicitly specify the variables in the scriptblock which originate from the local session by prefixing them with $using: e.g., $using:Source or $using:APPServer when referencing them within the $SB scriptblock.