I use a pwsh code to connect to a remote computer to generate a CSV file.
- I do this by Invoke-command, the code works perfectly, generates a CSV file on the server.
- 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