PowerShell Enter Session 查找路径错误

PowerShell Enter Session find path bug

我有一些自动化脚本,但由于似乎有一个有趣的错误,我不得不将它们拆分。我已将其简化为以下最简单的形式:

Enter-PSSession [SERVER]
cd D:\

如果我 运行 一次性完成以上操作,我会得到以下错误

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

但是,如果我单独 运行 行,它们 运行 没问题。我试过睡一秒钟,暂停线,但仍然没有运气。是否还有其他人知道这一点以及解决方法?

使用 Invoke-Command 代替 enter-pssession

示例:

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