PSSession中New-PSDrive的Copy-Item

时间:2017-12-14 17:26:51

标签: powershell copy-item

我有以下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)

1 个答案:

答案 0 :(得分: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
}