【问题标题】:Copy-Item from New-PSDrive within PSSession从 PSSession 中的 New-PSDrive 复制项目
【发布时间】:2017-12-14 17:26:50
【问题描述】:

我有以下 Powershell 代码:

Enter-PSSession -ComputerName test01 
New-PSDrive -Name Source -PSProvider FileSystem -Root \\test02\SMBTest -Credential test\Administrator 
Copy-Item Source:\Test.txt -Destination C:\Temp
Remove-PSDrive Source 
Exit-PSSession

当我自己执行每一行时,它可以工作,但是当我将它保存并作为 ps1 文件运行时,它什么也不做。

谁能帮忙解释一下原因(我使用的是 Powershell --version 5.1)

【问题讨论】:

  • 为什么要在脚本中输入会话?使用Invoke-Command。虽然我认为你遇到了第三跳问题

标签: powershell copy-item


【解决方案1】:

感谢@theincorrigible1 - 我已将其修改为以下内容,现在可以使用了。

$s = New-PSSession -ComputerName test01
Invoke-Command -Session $s -ScriptBlock {
New-PSDrive -Name Source -PSProvider FileSystem -Root \\test02\SMBTest -
Credential test\Administrator
Copy-Item Source:\Test.txt -Destination C:\Temp
Remove-PSDrive Source
}

【讨论】:

    猜你喜欢
    • 2019-03-25
    • 2013-06-06
    • 1970-01-01
    • 2015-11-03
    • 2020-11-25
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多