在powershell中的远程计算机上映射驱动器

在powershell中的远程计算机上映射驱动器,powershell,mapping,net-use,Powershell,Mapping,Net Use,我正在桌面上的远程机器上工作,我有以下脚本: Invoke命令-computername$name-authentication默认值 -凭证$creds1 -scriptblock{net use\share$password2/user:otherdomain\otheruser} 然后我得到一个指定的登录会话不存在。它可能已经被终止了。这件事令人沮丧,因为如果我直接在远程机器上运行net use\\share$password2/user:otherdomain\otheruser,它就可

我正在桌面上的远程机器上工作,我有以下脚本:

Invoke命令-computername$name-authentication默认值 -凭证$creds1 -scriptblock{net use\share$password2/user:otherdomain\otheruser}


然后我得到一个指定的登录会话不存在。它可能已经被终止了。这件事令人沮丧,因为如果我直接在远程机器上运行
net use\\share$password2/user:otherdomain\otheruser
,它就可以正常工作。是否有解决方法或我遗漏了什么?

您需要将变量
$password2
传递到脚本块中,否则其值将为空:

Invoke-Command -Computer $name ... -ScriptBlock {
  net use \\host\share $args[0] /user:otherdomain\otheruser
} -ArgumentList $password2

通过向远程脚本添加net会话,我发现没有条目。有人知道如何手动添加一个吗?