从 PSSession 中的 New-PSDrive 复制项目

Copy-Item from New-PSDrive within PSSession

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