1

I have to get the particular file from one remote desktop to local machine or another server.How to pass a variable in get-content to fetch the file from remote desktop connection?

I store the file path as variable and try to pass it in get-content.

Invoke-Command -Computername $Server -ScriptBlock{get-content -path $file }
Invoke-Command -Computername $Server -ScriptBlock{get-content -path ${file} }

$file="C:\Users\Documents\new DB_connection\\log2.txt"

 $Server="servername"

 $answer=  Invoke-Command -Computername $Server -ScriptBlock{get-content -path $file } 
write-output $answer

Cannot bind argument to parameter 'Path' because it is null. + CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand

2
  • i tried this code "Invoke-Command -Computername $Server -ScriptBlock{get-content -path $file } but it again showing a same error Commented Aug 26, 2019 at 5:27
  • 1
    Once you assign a value to $file, then simply use $using:file inside of the script block. You can’t pass it into the script block until it is defined Commented Aug 26, 2019 at 5:57

2 Answers 2

0

You can copy the file over a PSSession instead of using Invoke-Command. To copy from a remote server to a local path or different remote server:

 $session = New-PSSession -ComputerName server.domain.tld
 Copy-Item -Source $originatingServerFilePath -Destination $localOrUNCFilePath -FromSession $session

If you need to copy a local file to a destination server:

Copy-Item -Source $localFilePath -Destination $destinationServerFilePath -ToSession $session

This has the benefit of not double-hopping, though the server you run the command on will need to have access to any remote file paths. If you need to copy a file from one server to another server, but the destination server doesn't have the filepath exposed as a shared folder (or you don't have access to it) you cannot specify -ToSession and -FromSession at the same time, so you'll have to copy the file locally and use two sessions like so:

$sourceSession = New-PSSession -ComputerName source.domain.tld
$destinationSession = New-PSSession -ComputerName destination.domain.tld

# Copy the remote file(s) from the source session to a local path first
Copy-Item -Source $sourceSessionFilePath -Destination $localTempFilePath -FromSession $sourceSession

# Copy the new local files to the destination session from your local path
Copy-Item -Source $localTempFilePath -Destination $destinationSessionFilePath -ToSession $destinationSession
Sign up to request clarification or add additional context in comments.

Comments

0

Try to define the variable inside the ScriptBlock

$Server="servername"
$answer=  Invoke-Command -Computername $Server -ScriptBlock{
$file="C:\Users\Documents\new DB_connection\\log2.txt";
get-content -path $file} 
write-output $answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.