Powershell - Invoke-Command 脚本块中的 "Copy-item" 命令
Powershell - "Copy-item" command in scriptblock of Invoke-Command
我需要使用 powershell 脚本将文件从一台远程服务器复制到另一台服务器。
我试过的:-
当我使用以下 powershell 命令时,它工作正常。(意味着文件从一台服务器复制到另一台服务器)
但是当我使用下面的脚本时它给出错误“找不到路径...”如下
实际上,该路径中存在文件。
我已经尝试参考以下堆栈溢出问答
- 'Session' Parameter is null or empty in PowerShell script
- powershell remote
- PowerShell Command to Copy File on Remote Machine
我也尝试使用
寻求帮助
Get-Help Invoke-Command
问题:-
- 如何使用脚本(2)中“Invoke-Command(Scriptblock)”中的“Copy-Item”命令来复制文件?
- 有没有更好的方法来实现这个(意味着最佳实践)?
Invoke-Command
具有参数 -ArgumentList
,可用于在远程会话中提供局部变量的值。问题是:它只是变量的值。没有文件!
你能做什么:
对小文件使用 Get-Content -Raw
以将内容保存在变量中。在目标系统上使用该文件的 -Value
创建一个 New-Item
。然而那不是很有效。
示例:
$txt = Get-Content -Raw -Path "C:\test\oldFile.txt"
$Session = New-PSSession 127.0.0.1
Invoke-Command -Session $Session -ScriptBlock { Param($Txt) New-Item -Path c:\test\newFile.txt -Value $txt } -ArgumentList $txt
#Get-PSSession | Remove-PSSession
结果:
Verzeichnis: C:\test # Sry german OS
Mode LastWriteTime Length Name PSComputerName
---- ------------- ------ ---- --------------
-a---- 03.09.2020 12:23 658033 newFile.txt 127.0.0.1
你应该做什么:
我认为您使用 Copy-Item -ToSession $Session
是正确的方法。它只是为了您的目的而制作的。缺点是目标目录需要存在。但是无论如何,您都需要为两个 cmdlet 使用 PSSession。因此,您可以将 Invoke-Command
与相同的 PSSession 一起使用。首先创建一个PSSession。使用 Invoke-Command
创建目录。然后使用 Copy-Item
将文件移动到正确的位置。最后你可以使用 Invoke-Command
做一些收尾步骤。完成后别忘了 Remove-PSSession
:
$DestinationPath = "C:\test"
Invoke-Command -Session $Session -ScriptBlock { Param($Destination) New-Item -Path $Destination -ItemType Directory } -ArgumentList $DestinationPath
Copy-Item -Path "C:\test\oldFile.txt" -ToSession $Session -Destination "c:\test\newFile.txt"
Invoke-Command -Session $Session -ScriptBlock { write-host "Do some stuff" }
$Session | Remove-PSSession
我需要使用 powershell 脚本将文件从一台远程服务器复制到另一台服务器。
我试过的:-
当我使用以下 powershell 命令时,它工作正常。(意味着文件从一台服务器复制到另一台服务器)
但是当我使用下面的脚本时它给出错误“找不到路径...”如下
实际上,该路径中存在文件。
我已经尝试参考以下堆栈溢出问答
- 'Session' Parameter is null or empty in PowerShell script
- powershell remote
- PowerShell Command to Copy File on Remote Machine
我也尝试使用
寻求帮助 Get-Help Invoke-Command
问题:-
- 如何使用脚本(2)中“Invoke-Command(Scriptblock)”中的“Copy-Item”命令来复制文件?
- 有没有更好的方法来实现这个(意味着最佳实践)?
Invoke-Command
具有参数 -ArgumentList
,可用于在远程会话中提供局部变量的值。问题是:它只是变量的值。没有文件!
你能做什么:
对小文件使用 Get-Content -Raw
以将内容保存在变量中。在目标系统上使用该文件的 -Value
创建一个 New-Item
。然而那不是很有效。
示例:
$txt = Get-Content -Raw -Path "C:\test\oldFile.txt"
$Session = New-PSSession 127.0.0.1
Invoke-Command -Session $Session -ScriptBlock { Param($Txt) New-Item -Path c:\test\newFile.txt -Value $txt } -ArgumentList $txt
#Get-PSSession | Remove-PSSession
结果:
Verzeichnis: C:\test # Sry german OS
Mode LastWriteTime Length Name PSComputerName
---- ------------- ------ ---- --------------
-a---- 03.09.2020 12:23 658033 newFile.txt 127.0.0.1
你应该做什么:
我认为您使用 Copy-Item -ToSession $Session
是正确的方法。它只是为了您的目的而制作的。缺点是目标目录需要存在。但是无论如何,您都需要为两个 cmdlet 使用 PSSession。因此,您可以将 Invoke-Command
与相同的 PSSession 一起使用。首先创建一个PSSession。使用 Invoke-Command
创建目录。然后使用 Copy-Item
将文件移动到正确的位置。最后你可以使用 Invoke-Command
做一些收尾步骤。完成后别忘了 Remove-PSSession
:
$DestinationPath = "C:\test"
Invoke-Command -Session $Session -ScriptBlock { Param($Destination) New-Item -Path $Destination -ItemType Directory } -ArgumentList $DestinationPath
Copy-Item -Path "C:\test\oldFile.txt" -ToSession $Session -Destination "c:\test\newFile.txt"
Invoke-Command -Session $Session -ScriptBlock { write-host "Do some stuff" }
$Session | Remove-PSSession