2

pwsh コードを使用してリモート コンピューターに接続し、CSV ファイルを生成します。

  1. Invoke-command でこれを行います。コードは完全に機能し、サーバー上に CSV ファイルを生成します。
  2. CSV ファイルの名前は動的に生成されます。

ただし、そのファイルをリモート コンピューターからローカル コンピューターにコピーできません。

Invoke-command 内で copy-item を使用する方法はありますか?

アドバイス/ガイドしてください。

コードのスニペットを以下に示します。


#   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
4

1 に答える 1