我使用 pwsh 代码连接到远程计算机以生成 CSV 文件。
- 我通过调用命令执行此操作,代码运行良好,在服务器上生成一个 CSV 文件。
- CSV 文件的名称是动态生成的。
但是,我无法将该文件从远程计算机复制到本地计算机。
有没有办法在调用命令中使用复制项?
请建议/指导。
代码片段如下所示。
# 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