通过PowerShell将文件移动到远程计算机

时间:2018-01-30 10:58:10

标签: powershell

我目前正在尝试将文件从本地目的地移动到网络上的远程Pc。我希望将此扩展到多个用户,但同时我只想将一个文件移动到一台机器上。

我目前使用的脚本位于

之下
# sets the varible for the file location
$file = "c:\Powershell\testfile.txt"

$getcred = Get-Credential 'user'
New-PSDrive -Name Oli -PSProvider FileSystem -Root \\uint140\c$ -Credential $using:getcred #\Users\oliverturner\Desktop

# displays the computer names on screen
Get-Content $file | foreach {Copy-Item "C:\Powershell\testfile.txt" -Destination Oli}

当我运行时,我收到以下错误

The provider does not support the use of credentials. Perform the operation again without specifying credentials.
At C:\Powershell\movefile2.ps1:8 char:1
+  <<<< New-PSDrive -Name Oli -PSProvider FileSystem -Root \\uint140\c$ -Credential $using:getcred #\Users\oliverturner
\Desktop #-Credential jamesfegan\invateltd.local -Persist
    + CategoryInfo          : NotImplemented: (:) [], PSNotSupportedException
    + FullyQualifiedErrorId : NotSupported

我试图删除-Credential然后$ getcred但是我收到了

New-PSDrive: Drive root "\\uint140\c$" does not exist or it's not a folder 

这是使用New-PSDrive的问题还是有另一个更好的命令?

1 个答案:

答案 0 :(得分:1)

#Sessions stuff
$usr = "Domain\User"
$pw = convertto-securestring -AsPlainText -Force -String <insert pw here>
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "$usr",$pw
$session = new-pssession -computername <computer> -credential $cred

# sets the variable for the file location
$file = "c:\Powershell\testfile.txt"
$Dest = "c:\temp"

# Lets copy!
Copy-Item -ToSession $session -Path $File -Destination $Dest

尝试这样的事情。它创建一个到远程服务器的会话,并使用该会话发送文件。