powershell - 从 PSSession 内的 New-PSDrive 复制项目

标签 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)

最佳答案

谢谢@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/

相关文章:

powershell - 如何从powershell中的get-mobiledevicestatistics获取准确的用户名

c# - 从单独的线程更新 cmdlet 进度的最佳方法

ios - swift 3 : Error in copying file with FileManager

powershell - 从 get-childitem 搜索中排除路径

visual-studio - 在 Azure Pipeline 中的 C# 项目上测试 NuGet 包安装

powershell - Powershell Net-Cookie到期

要复制并执行的 Powershell 脚本

powershell - 第二次运行时,Copy-Item 复制目标内的文件夹

ios - 为什么 copyItemAtPath :toPath:userPath failed to copy the file when the file really exists? NSFileNoSuchFileError,Cocoa 错误代码 4

json - Azure 资源管理器模板语言-resourceId() : Unable to evaluate template language function 'resource Id'