I created a script to copy a VM (Folder) from Host-1 to Host-2, the script is run from my laptop. this is the Invoke-command part:
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 }
}
there are 2 Problems in the script that I can't figure out:
- even though the script runs successfully I still get this error:
Error: There was an error in communication
in a write-host format, not error format - after I run the script (again: successfully) and try to run it again I get an error:
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
please help.
Edit Problem #1 - I split the invoke-command to do the copy and a different invoke-command to change the file...
Edit Problem #2 - no change for the "re-run" problem, I still can't get the script to run Succesful without a timeout period of about 10 minutes
im Adding the Full script (hope for some help here)
##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
}