2

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
Gagan
  • 49
  • 6

1 Answers1

2

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
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • My bad, I was quick enough to delete my comment. Thanks for the guidance. However, now I'm stuck with a different issue: `Export-Csv: Could not find a part of the path 'C:\Users\admin\Downloads\'.` – Gagan Dec 13 '21 at 19:01
  • @Gagan don't use relative paths for your export, note how I'm using the `Join-Path` call to get the absolute path were the file will be stored. – Santiago Squarzon Dec 13 '21 at 19:03
  • I'm using the same code that you've used. Literally copy-pasted. – Gagan Dec 13 '21 at 19:04
  • @Gagan mmm I've updated my answer, I noticed I didn't included the `$IPAddress` part. Tho I don't think that's causing the issue. Where do you see the CSV getting stored on the remote host? It should be something like `C:\Users\admin\Downloads\LastLogonReport__10.10.10.10__13 Dec 2021.csv` – Santiago Squarzon Dec 13 '21 at 19:24
  • Thanks @santiago, the code is now working. I had to use `$Using:[variable]` to get the `$logonReportName` correctly. However, thank you so much! – Gagan Dec 14 '21 at 19:15
  • @Gagan no problem! i'm not sure how that could work, would you like to edit your question with what you're doing – Santiago Squarzon Dec 14 '21 at 19:22