我有以下 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)
最佳答案
谢谢@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
}
关于powershell - 从 PSSession 内的 New-PSDrive 复制项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47819004/