脚本中的问题:调用命令“错误:通信中存在错误”

时间:2018-08-27 18:53:22

标签: powershell virtual-machine vmware-workstation

我创建了一个脚本,用于将VM(文件夹)从Host-1复制到Host-2,该脚本是从我的笔记本电脑运行的。 这是调用命令部分:

   Invoke-Command -Computername $Thost -Credential $mycreds
-ScriptBlock {
    #seting a temp drive on Host for simple approch
    New-PSDrive -Name v -PSProvider FileSystem -Root $Using:VMPath -Credential $Using:mycreds
    Write-Host "You Chose that $Using:OSVersion will be copyed to $Using:Thost"
    Copy-Item -Path "v:\$Using:OSVersion" -Destination $using:VMXPath -Recurse
    Get-ChildItem -Path $Using:VMXPath | Rename-Item -NewName { $_.name -Replace $Using:OSVersion,$Using:newVM }
     }

脚本中有2个我不知道的问题:

  1. 即使脚本成功运行,我仍然会收到此错误:Error: There was an error in communication采用写主机格式,而不是错误格式
  2. 运行脚本(再次:成功)并再次尝试运行脚本后,出现错误:a specified logon session does not exist. it may already have been terminated

a specified logon session does not exist. it may already have been terminated

Error: There was an error in communication

请帮助。


编辑问题1-我拆分了invoke命令进行复制,并拆分了另一个invoke命令来更改文件...

编辑问题2-“重新运行”问题没有变化,如果没有大约10分钟的超时时间,我仍然无法使脚本成功运行


im添加完整脚本(希望在此处寻求帮助)

##Functions -Start
function OS
{
     param (
           [string]$Title = 'OS Menu'
     )
     cls
     Write-Host "Welcome to $Title"

     Write-Host "1: Windows"
     Write-Host "2: Linux"
     Write-Host "Q: Press 'Q' to quit."
}

function WVersion
{
     param (
           [string]$Title1 = 'Windows Version Menu'
     )
     Write-Host "Welcome to $Title1"

     Write-Host "1: Windows 10 Pro"
     Write-Host "Q: Press 'Q' to quit."
}
function LVersion
{
     param (
           [string]$Title2 = 'Linux Menu'
     )
     Write-Host "Welcome to $Title2"

     Write-Host "1: Ubuntu 18"


     Write-Host "Q: Press 'Q' to quit."
}

function Host
{
     param (
           [string]$Title = 'Host`s Menu'
     )
     cls
     Write-Host "Welcome to $Title"

     Write-Host "1:  Host-1"
     Write-Host "Q: Press 'Q' to quit."
}

function Drive #Func to select the Drive to move the VM to (on remote Client / Target)
{
     param (
           [string]$Title = 'Driver Menu'
     )
     cls
     Write-Host "Welcome to $Title"

     Write-Host "1: C:\"
     Write-Host "Q: Press 'Q' to quit."
}

#Username and password for COnnection
$Username = "<user>"
$Password = ConvertTo-SecureString "<password>" -AsPlainText -Force
#creating a secure User for connection to the remote Client
$mycreds = New-Object System.Management.Automation.PSCredential($Username, $Password)

##Global VAR -Start
#path to VM's folder
$Repo = "\\host-28\Templets"

##################
##Script --Start##
##################
OS
     $input = Read-Host "Please select The OS"
     switch ($input)
     {
           '1' {
                WVersion
                $OSFolder = "Windows"
                $input2 = Read-Host "Please select The Version"
                switch ($input2)
                {
                    '1' {
                         Write-Host "Windows 10 Pro Selected"
                         $OSVersion = "TMP-w10x64-pro"

                      }'q' {
                        return
                        }
                }
           } '2' {
               LVersion
               $OSFolder = "Linux"
               $input3 = Read-Host "Please select The Version"
               switch ($input3)
               {
                '1' {
                    Write-Host "Ubuntu 18 Selected"
                    $OSVersion = "tmp-u18"

                }'q' {
                    return
                }
               }
           } 'q' {
                return
           }
     }

Host
$input3 = Read-Host "Please select Target Host"
switch ($input3)
    {
      '1' {
           Write-Host "Host-1"
           $VMHost = "\\host-1\<Windows_VM_Folder>"

     }'q' {
           return
     }
    }

Drive
$input4 = Read-Host "Please select The Target Driver"
               switch ($input4)
               {
                '1' {
                    Write-Host "C:\ Selected"
                    $cVMfolder = 'VMs'
                    $VMHost = "\\${Thost}" + "\${cVMfolder}"
                    $destination = 'C:\VMs'

                }'q' {
                    return
                    }
                }

#the Folder name (need to be the same as the VM hostname)
$newVM = Read-Host -Prompt 'Set New HostName to VM:'
# commbined path+folder name 
$copyedFolder = "${VMHost}" + "\${newVM}"
#Full Path To selected VM Folder
$VMPath = "${Repo}" + "\${OSFolder}" # + "\${OSVersion}"
$VMXPath = "${destination}" + "\${newVM}"

# test for cahnging file name (after copy comand)
Invoke-Command -Computername $Thost -Credential $mycreds -ScriptBlock {
    #seting a temp drive on Host for simple approch
    new-PSDrive -Name w -PSProvider FileSystem -Root $Using:VMPath -Credential $Using:mycreds
    Write-Host "You Chose that $Using:OSVersion will be copyed to $Using:Thost"
    Copy-Item -Path "w:\$Using:OSVersion" -Destination $using:VMXPath -Recurse
     }

Invoke-Command -Computername $Thost -Credential $mycreds -ScriptBlock {
    #seting a temp drive on Host for simple approch
    Get-ChildItem -Path $Using:VMXPath | Rename-Item -NewName { $_.name -Replace $Using:OSVersion,$Using:newVM }
    Remove-PSDrive -Name w -PSProvider FileSystem
    Restart-Service winrm
     }
        }

    Drive
    $input4 = Read-Host "Please select The Target Driver"
                   switch ($input4)
                   {
                    '1' {
                        Write-Host "C:\ Selected"
                        $cVMfolder = 'VMs'
                        $VMHost = "\\${Thost}" + "\${cVMfolder}"
                        $destination = 'C:\VMs'
                    }'2' {
                       Write-Host "D:\ Selected"
                        $cVMfolder = 'VMs2'
                        $VMHost = "\\${Thost}" + "\${cVMfolder}"
                        $destination = 'D:\VMs'
                    }'3' {
                        Write-Host "E:\ Selected"
                        $cVMfolder = 'VMs3'
                        $VMHost = "\\${Thost}" + "\${cVMfolder}"
                        $destination = 'E:\VMs'
                    }'4' {
                        Write-Host "F:\ Selected"
                        $cVMfolder = 'VMs4'
                        $VMHost = "\\${Thost}" + "\${cVMfolder}"
                        $destination = 'F:\VMs'
                    }'q' {
                        return
                        }
                    }

    #the Folder name (need to be the same as the VM hostname)
    $newVM = Read-Host -Prompt 'Set New HostName to VM:'
    # commbined path+folder name 
    $copyedFolder = "${VMHost}" + "\${newVM}"
    #Full Path To selected VM Folder
    $VMPath = "${Repo}" + "\${OSFolder}" # + "\${OSVersion}"
    $VMXPath = "${destination}" + "\${newVM}"

    # test for cahnging file name (after copy comand)
    Invoke-Command -Computername $Thost -Credential $mycreds -ScriptBlock {
        #seting a temp drive on Host for simple approch
        new-PSDrive -Name w -PSProvider FileSystem -Root $Using:VMPath -Credential $Using:mycreds
        Write-Host "You Chose that $Using:OSVersion will be copyed to $Using:Thost"
        Copy-Item -Path "w:\$Using:OSVersion" -Destination $using:VMXPath -Recurse
         }

    Invoke-Command -Computername $Thost -Credential $mycreds -ScriptBlock {
        #seting a temp drive on Host for simple approch
        Get-ChildItem -Path $Using:VMXPath | Rename-Item -NewName { $_.name -Replace $Using:OSVersion,$Using:newVM }
        Remove-PSDrive -Name w -PSProvider FileSystem
        Restart-Service winrm
         }

0 个答案:

没有答案