使用 PowerShell 2.0 复制/传输文件 [英] Copy/transfer file using PowerShell 2.0

查看:57
本文介绍了使用 PowerShell 2.0 复制/传输文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 PowerShell 脚本,以使用其 IP 地址将文件从我的本地计算机复制到远程计算机.

I'm trying to create a PowerShell script to copy a file from my local computer to remote computer using its IP address.

我使用以下命令测试了我的客户端-服务器连接:

I tested my client-server connection by using this command:

Invoke Command -ComputerName <IP Address> -ScriptBlock {ipconfig} -Credential $credential

(在此命令之前输入了 $credential).

(where $credential had been entered just before this command).

我尝试使用 Copy-ItemRobocopy 命令,但我不清楚它是否会获取我的凭据,然后让我从本地复制文件到远程机器.具体来说,这些甚至支持本地到远程文件传输吗?

I tried using the Copy-Item and Robocopy commands but I'm not clear if it will even take my credentials and then let me copy a file from a local to a remote machine. To be specific, would these even support local to remote file transfer?

很多时候我都遇到过这样的错误:错误的用户名和密码,源路径不存在或目标路径不存在.但我仍然想确定我是否在正确的轨道上并使用正确的命令来实现我想要的,或者是否还有其他我应该考虑使用的东西.我该如何解决这个问题?

A lot of times I faced errors like: Bad username and password, source path does not exist or destination path does not exist. But I still wanted to be sure if I was on right track and using the right commands to implement what I want to or if there is something else which I should consider using. How can I fix this problem?

推荐答案

您似乎正在尝试使用 PowerShell 远程处理复制文件.正如其他答案中发布的那样,使用 Copy-Item 和/或 Robocopy 从源复制到目标计算机上的共享会更简单.

It looks like you're trying to copy a file using PowerShell remoting. As posted in other answers, it would be simpler to use Copy-Item and/or Robocopy to copy from the source to a share on the destination computer.

如果您想使用 PowerShell 远程复制文件,您可以将文件放入一个变量中并在远程脚本块中使用它.类似的东西:

If you want to copy the file using PowerShell remoting, you can slurp the file into a variable and use it in the remote script block. Something like:

$contents = [IO.File]::ReadAllBytes( $localPath )
Invoke-Command -ComputerName <IP Address> `
               -Credential $credential  `
               -ScriptBlock { [IO.File]::WriteAllBytes( 'C:\remotepath', $using:contents ) }

当然,如果您正在读取的文件非常大,这可能会导致远程连接内存不足(默认情况下,PowerShell 将远程连接限制在 128MB 左右).

Of course, if the file you're reading is really big, this could cause the remote connection to run out of memory (by default, PowerShell limits remote connections to around 128MB).

如果您在使用 PowerShell 2 时遇到困难,则需要将字节作为脚本块参数传递:

If you're stuck using PowerShell 2, you'll need to pass the bytes as a script block parameter:

$contents = [IO.File]::ReadAllBytes( $localPath )
Invoke-Command -ComputerName <IP Address> `
               -Credential $credential  `
               -ScriptBlock { 
                   param(
                       [byte[]]
                       $Contents
                   )
                   [IO.File]::WriteAllBytes( 'C:\remotepath', $Contents) 
               } `
               -ArgumentList @( ,$contents )

是的,当将 $contents 作为值传递给 -ArgumentList 参数时,您必须将它包装在一个数组中并且它必须是以逗号运算符 , 为前缀,否则您的远程脚本块将只接收第一个字节.

And yes, you must wrap $contents in an array when passing it as the value to the -ArgumentList parameter and it must be prefixed with the comma operator ,, otherwise your remote script block will only receive the first byte.

这篇关于使用 PowerShell 2.0 复制/传输文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆