Я создал скрипт для копирования виртуальной машины (папки) с хоста-1 на хост-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 проблемы, которые я не могу понять:
- , несмотря на то, что скрипт выполняется успешно, я все еще получаю эту ошибку:
Error: There was an error in communication
в формате записи хоста, а не в формате ошибки
- после того, как я запустил скрипт (снова: успешно) и попытался запустить его снова, я получил ошибку:
a specified logon session does not exist. it may already have been terminated
указанный сеанс входа не существует. возможно, оно уже было прекращено
Ошибка: произошла ошибка связи
помогите пожалуйста.
Редактировать проблему # 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
}