PowerShell输入会话查找路径错误

时间:2017-08-30 09:57:07

标签: powershell remote-access

我有一些自动化脚本,但由于看似有趣的bug,我不得不拆分它们。我把它剥离到下面最简单的形式:

Enter-PSSession [SERVER]
cd D:\

如果我一次运行上述内容,我会收到以下错误

cd : Cannot find drive. A drive with the name 'D' does not exist.

但是,如果我单独运行这些行,它们运行正常。我试过睡了一秒钟,一个暂停线,但仍然没有运气。是否有其他人意识到这一点,以及围绕它的方式?

1 个答案:

答案 0 :(得分:1)

使用Invoke-Command代替enter-pssession

示例:

$ReturnValue = Invoke-Command -ComputerName $Server -ScriptBlock{
    Set-Location D:
    # DO STUFF
    Return $ReturnValue # Return your stuff
}