【问题标题】:powershell New-PSDrive consistencypowershell New-PSDrive 一致性
【发布时间】:2019-03-25 14:24:50
【问题描述】:

我创建了一个从本地电脑运行的脚本,并使用invoke-command 将文件从 Host-1 复制到 host-2。

相关部分:

$session =New-PSSession -Computername $Thost -Credential $mycreds

Invoke-Command -Session $session -ScriptBlock  {
#seting a temp drive on Host for simple approch
Remove-PSDrive -Name w -Force
New-PSDrive -Name w -PSProvider FileSystem -Root $Using:VMPath -Credential $Using:mycreds -ErrorAction Stop
Write-Host "You Chose that $Using:OSVersion will be copyed to $Using:Thost"
Copy-Item -Path "w:\$Using:OSVersion" -Destination $using:VMXPath   -Recurse
Remove-PSDrive -Name w -Force -ErrorAction Stop
 }

我第一次运行脚本时运行良好! First Run of the Script

第二次说 PSdrive 不存在。 Second Run Error in PowerShell

10 分钟后 +- 脚本再次运行,没有任何问题

【问题讨论】:

    标签: windows powershell vmware vmware-workstation


    【解决方案1】:

    我很惊讶它第一次运行。看起来故障出在最初的 Remove-PSDrive 上。它失败了,因为您已经移除了 w 驱动器。

    采用这两种模式中的一种

    Remove-PSDrive -Name w -ErrorAction SilentlyContinue
    New-PSDrive -Name w -PSProvider FileSystem -Root c:\test
    Remove-PSDrive -Name w -Force
    

    if (Test-Path -Path w:\){
       Remove-PSDrive -Name w -Force
     }
     New-PSDrive -Name w -PSProvider FileSystem -Root c:\test
     Remove-PSDrive -Name w -Force
    

    我会选择第二个选项

    【讨论】:

    • tnx 用于快速回放,我添加了: if (test-path... 它确实更智能... 但它并没有改变结果...
    • 现在修复了我在 qustion 中打印脚本的方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    相关资源
    最近更新 更多