首页 > 解决方案 > 如何在PowerShell脚本中访问远程计算机?

问题描述

我正在尝试使用 powershell 脚本将文件从本地计算机复制到远程计算机。

我的脚本:

$Session = New-PSSession -ComputerName "\\10.10.X.X" -Credential "username\password"
Copy-Item "D:\Test.txt" -Destination "D:\Self Study\" -ToSession $Session

我得到了以下错误:

New-PSSession : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri
parameter, or pass URI objects instead of strings.
At D:\PS_Test_script.ps1:1 char:12
+ $Session = New-PSSession -ComputerName "\\10.10.17.162" -Credential " ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (System.String[]:String[]) [New-PSSession], ArgumentException
    + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.NewPSSessionCommand

Copy-Item : Cannot validate argument on parameter 'ToSession'. The argument is null or empty. Provide an argument that
is not null or empty, and then try the command again.
At D:\PS_Test_script.ps1:2 char:66
+ ... -Item "D:\Test.txt" -Destination "D:\Self Study\" -ToSession $Session
+                                                                  ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.CopyItemCommand

如何使用计算机名称访问远程计算机?在这里使用IP地址是错误的吗?

标签: powershellremote-accessremote-desktop

解决方案


推荐阅读