Gagan
Gagan

Reputation: 49

Copy file from remote computer to local computer within an Invoke-command session in PowerShell

I use a pwsh code to connect to a remote computer to generate a CSV file.

  1. I do this by Invoke-command, the code works perfectly, generates a CSV file on the server.
  2. The name of the CSV file is generated dynamically.

However, I'm unable to copy that file from the remote computer to local computer.

Is there a way to use copy-item within Invoke-command?

Please advise/guide.

The snippet of the code is given below.


#   Target Server
$TargetServer = "xxx.xxx.xxx.xxx"

#   Capture the VM credentials
$creds = Get-Credential -Title "Enter admin Password" -UserName admin

#   Create session
$session = New-PSSession -ComputerName $TargetServer -Credential $creds

$scriptBlock = {
    #   Attempt Install
    Install-Module -Name Join-Object

    #   Attempt Import
    Import-Module -Name Join-Object 

    #   IP Address
    $ipAdress = (Get-NetIPAddress -AddressFamily IPV4).IPAddress[0]

    #   Set the CSV file name
    $lastLogonReportName = "LastLogonReport__" + $ipAdress + "__" + (get-date -Format "dd MMM yyyy_dddd") + ".csv"

    ... ...
    ... ...
    ... ...
    ... ...

    $Output

    #   Set Location to user's Downloads folder
    Set-Location -Path $HOME\Downloads
    
    $Output | Export-Csv -Path ./$lastLogonReportName

    # Copy-Item $lastLogonReportName -Destination "D:\" -FromSession $Session
 }

Invoke-Command -ComputerName $TargetServer -Credential $creds -ScriptBlock $scriptBlock

Upvotes: 2

Views: 828

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60045

You can definitely use Copy-Item for this, but there is an easier way:

#   Create session
$session = New-PSSession -ComputerName $TargetServer -Credential $creds

$scriptBlock = {
    #   Set the CSV file name
    $date = Get-Date -Format "dd MMM yyyy"

    #   IP Address
    $ipAdress = (Get-NetIPAddress -AddressFamily IPV4).IPAddress[0]

    $lastLogonReportName = "LastLogonReport__${ipAdress}__$date.csv"

    #   Destination
    $destination = Join-Path "$HOME\Downloads" -ChildPath $lastLogonReportName
    $Output | Export-Csv -Path $destination -NoTypeInformation
    $Output # => Send this Object to be captured on locahost
}

$params = @{
    Session = $session
    ScriptBlock = $scriptBlock
}

$captureThisObject = Invoke-Command @params

PS /> $captureThisObject # => Is your CSV on localhost

If you wanted to use Copy-Item instead, have Invoke-Command return the path where the CSV was stored on the host and then (from outside the invocation) you call Copy-Item -FromSession:

#   Create session
$session = New-PSSession -ComputerName $TargetServer -Credential $creds

$scriptBlock = {
    #   Set the CSV file name
    $date = Get-Date -Format "dd MMM yyyy"

    #   IP Address
    $ipAdress = (Get-NetIPAddress -AddressFamily IPV4).IPAddress[0]

    $lastLogonReportName = "LastLogonReport__${ipAdress}__$date.csv"

    #   Destination
    $destination = Join-Path "$HOME\Downloads" -ChildPath $lastLogonReportName
    $Output | Export-Csv -Path $destination -NoTypeInformation
    
    $destination # => Send this Path to be captured on locahost
}

$params = @{
    Session = $session
    ScriptBlock = $scriptBlock
}

$remotePath = Invoke-Command @params
Copy-Item -Path $remotePath -Destination path/to/csvHere.csv -FromSession $session

Upvotes: 2

Related Questions