VMware Cloud Community
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Copying a file onto a virtual machine

Hello,


I have a script which customizes virtual machines from a .csv file (attached).

The logical flow is this:

Modify memory,

modify vcpus,

modify notes,

modify network name (VLAN),

network settings,

set static IP address,

set hard disk size,

and finally:

expand hard disk.

I am using the invoke-script command to extend the disk after it has been expanded through VMware.  To do this I use the following syntax:

xcopy $installdir\extddisk.txt \\$target\c$temp\ /f

invoke-VMscript -VM $VM.name -ScriptText "diskpart /S c:\temp\extdisk.txt" -Hostuser $HostUser -HostPassword $HostPass -GuestUser -GuestUser -GuestPassword $GuestPass

The problem is that since the disk is being expanded as a last step.  If the VM is being deploy onto a VLAN which is not accessible (for example a DMZ VLAN) then I won't be able to copy the extdisk.txt file which the diskpart command depends on.  Is there another way that I can transfer this file onto the VM directly?

The file's contents are extremely simple, it says:

rescan

select volume 2

extend

Since the file is so simple may I could even just create it on the server before deleting it once the command has run successfully?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can use the Copy-VMGuestFile cmdlet to copy the file.

If Invoke-VMScript works, that one should work as well, it uses the same concept and uses the VMware Tools.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

You can use the Copy-VMGuestFile cmdlet to copy the file.

If Invoke-VMScript works, that one should work as well, it uses the same concept and uses the VMware Tools.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Five star answer LucD, I had no idea that command existed.

0 Kudos
c0055599
Contributor
Contributor
Jump to solution

Hi LucD,

I'm trying to copy some files as a part of my script. We are upgrading the OS and need to preserve some files and some folders to copy over to the new system once it is online. I am backing it up and transferring the backup to the new system. The script is zipping the files and unzipping on the new system, but when I go to copy the folders, it will not work using invoke-vmscript and using Powershell's copy-item. This is my code for the folder portion. 

    $WorkSource = $RestorePath + "\work"
    $WorkDest = "C:\Program Files\InfoLink\ILSEngine\work"


    If (Test-Path $WorkSource) {    
        Copy-Item -Path $WorkSource -Destination $WorkDest -Recurse -Force -Confirm:$false -Verbose -ErrorAction SilentlyContinue 
    }

When I run this code on the server locally, it works with no issue. Any suggestions why it won't work in my invoke-vmscript block?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would need to see how you pass that code via Invoke-VMScript.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
c0055599
Contributor
Contributor
Jump to solution

#region Restore Config and Start Services
Log "Start copy of Config file"
$i = 0
$success = $false
Do {
$i += 1
Copy-VMGuestFile -VM $NewVM -Source $BackupDestination -Destination "C:\Temp\$BackupFileName" -LocalToGuest -GuestCredential $guestcred -Force -ErrorAction Continue
$success = $?
if($success -eq $false){ start-sleep -Seconds 10 }
}
Until ($i -ge 10 -or $success -eq $true)
If ($i -ge 10) {
    log "Failed to copy config file, tried $i times" 
    log "PLEASE NOTE: Falling back to old 2012 VM"
    DeleteVM
    DeleteADObject
    ChangeNetworkAdapter
    PowerOnVM
    RenameVM
    RenameADComputerBack
    Throw-Fatal
    }

    Try {
        $ConfigFileSource = "C:\Temp\ConfigBackup_" + $computer + ".zip"
        Log "Start unpack of config file."
        Invoke-VMScript -VM $NewVM -ScriptType Powershell -GuestCredential $guestcred -Verbose -ScriptText @'
    $RestoreBasePath = "C:\Temp"
    $RestorePath = $RestoreBasePath + "\ConfigBackup_" + $env:COMPUTERNAME
    $RestorePathZip = $RestorePath + ".zip"
    if ((Test-Path -Path $RestorePath)){Remove-Item -Path $RestorePath -Recurse -Force -Confirm:$false}
    if ((Test-Path -Path $RestorePathZip)){
        TRY{
            Add-Type -AssemblyName "system.io.compression.filesystem"
            [System.IO.Compression.ZipFile]::ExtractToDirectory($RestorePathZip, $RestorePath)
            $source = ("C:\Program Files\InfoLink\COMServer\module-conf\module-server.properties.local",
                        "C:\Program Files\InfoLink\AppServer\standalone\configuration\standalone-full.xml",
                        "C:\program files\infolink\company_logo.gif",
                        "C:\Program Files\InfoLink\ILSEngine\conf\ilsengine.properties",
                        "C:\Program Files\InfoLink\ILMServer\conf\infolink.properties",
                        "C:\Program Files\InfoLink\ILMServer\conf\jndi.properties",
                        "C:\Program Files\InfoLink\ILMServer\conf\sserver.properties",
                        "C:\Program Files\InfoLink\ILMServer\conf\infolink.properties.local",
                        "C:\Program Files\InfoLink\COMServer\module-conf\wrapper.conf",
                        "C:\Program Files\InfoLink\RoutingServer\conf\protocol-routing-server.properties.local",
                        "C:\Program Files\InfoLink\ILSEngine\conf\ilsengine.properties.local",
                        "C:\Program Files\InfoLink\ILSEngine\lib\infolinkApp.ear",
                        "C:\Program Files\InfoLink\ILSEngine\lib\access-point-mapping*.war")
            Foreach ($file in $source) {
                $item = $file | Split-Path -Leaf
                $itempath = $RestorePath + "\" + $item
                Copy-Item -Path $itempath -Destination $file -Force -Confirm:$false -Recurse -Verbose -ErrorAction SilentlyContinue
    }

    #Path Variables for C:\Program Files\InfoLink\ILSEngine\work directory

    $WorkSource = $RestorePath + "\work"
    $WorkDest = "C:\Program Files\InfoLink\ILSEngine\work"


    #If Statement to test-path and copy work directory
    If (Test-Path $WorkSource) {    
        Copy-Item -Path $WorkSource -Destination $WorkDest -Recurse -Force -Confirm:$false -Verbose -ErrorAction SilentlyContinue 
    }


            # Copy upgrade log files
            Copy-Item -Path "$RestorePath\InfoLink*_upgrade.log" -Destination "C:\Program Files\InfoLink" -Force -Confirm:$false -Verbose -ErrorAction SilentlyContinue
            Write-Host "Sucessfully copied on $env:computername" -BackgroundColor Green

                }
            Catch {$error[0].exception.message}
        }

'@
    }
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I don't think inline text works like that to pass on the ScriptText parameter.
I would do

$code = @'
    $RestoreBasePath = "C:\Temp"
    $RestorePath = $RestoreBasePath + "\ConfigBackup_" + $env:COMPUTERNAME
    $RestorePathZip = $RestorePath + ".zip"
    if ((Test-Path -Path $RestorePath)){Remove-Item -Path $RestorePath -Recurse -Force -Confirm:$false}
    if ((Test-Path -Path $RestorePathZip)){
        TRY{
            Add-Type -AssemblyName "system.io.compression.filesystem"
            [System.IO.Compression.ZipFile]::ExtractToDirectory($RestorePathZip, $RestorePath)
            $source = ("C:\Program Files\InfoLink\COMServer\module-conf\module-server.properties.local",
                        "C:\Program Files\InfoLink\AppServer\standalone\configuration\standalone-full.xml",
                        "C:\program files\infolink\company_logo.gif",
                        "C:\Program Files\InfoLink\ILSEngine\conf\ilsengine.properties",
                        "C:\Program Files\InfoLink\ILMServer\conf\infolink.properties",
                        "C:\Program Files\InfoLink\ILMServer\conf\jndi.properties",
                        "C:\Program Files\InfoLink\ILMServer\conf\sserver.properties",
                        "C:\Program Files\InfoLink\ILMServer\conf\infolink.properties.local",
                        "C:\Program Files\InfoLink\COMServer\module-conf\wrapper.conf",
                        "C:\Program Files\InfoLink\RoutingServer\conf\protocol-routing-server.properties.local",
                        "C:\Program Files\InfoLink\ILSEngine\conf\ilsengine.properties.local",
                        "C:\Program Files\InfoLink\ILSEngine\lib\infolinkApp.ear",
                        "C:\Program Files\InfoLink\ILSEngine\lib\access-point-mapping*.war")
            Foreach ($file in $source) {
                $item = $file | Split-Path -Leaf
                $itempath = $RestorePath + "\" + $item
                Copy-Item -Path $itempath -Destination $file -Force -Confirm:$false -Recurse -Verbose -ErrorAction SilentlyContinue
    }

    #Path Variables for C:\Program Files\InfoLink\ILSEngine\work directory

    $WorkSource = $RestorePath + "\work"
    $WorkDest = "C:\Program Files\InfoLink\ILSEngine\work"


    #If Statement to test-path and copy work directory
    If (Test-Path $WorkSource) {    
        Copy-Item -Path $WorkSource -Destination $WorkDest -Recurse -Force -Confirm:$false -Verbose -ErrorAction SilentlyContinue 
    }


            # Copy upgrade log files
            Copy-Item -Path "$RestorePath\InfoLink*_upgrade.log" -Destination "C:\Program Files\InfoLink" -Force -Confirm:$false -Verbose -ErrorAction SilentlyContinue
            Write-Host "Sucessfully copied on $env:computername" -BackgroundColor Green

                }
            Catch {$error[0].exception.message}
        }
}
'@

        Invoke-VMScript -VM $NewVM -ScriptType Powershell -GuestCredential $guestcred -Verbose -ScriptText $code


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
c0055599
Contributor
Contributor
Jump to solution

The code I provided is working in terms of unzipping the zip file and copying all the files to the new machine, it just won't copy the work folder and its contents over for some reason. 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you remove the ErrorAction and show what errors it is giving?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
c0055599
Contributor
Contributor
Jump to solution

Looks like its returning this error:

Copy-VMGuestFile : 3/10/2023 12:08:20 PM Copy-VMGuestFile A specified parameter was not correct:
At D:\Scripts\Scheduled\Upgrade-InfolinkServer\Upgrade-InfolinkServer.ps1:859 char:1
+ Copy-VMGuestFile -VM $NewVM -Source $BackupDestination -Destination " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-VMGuestFile], InvalidArgument
+ FullyQualifiedErrorId :
Client20_VmGuestServiceImpl_DirectoryExistsInGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyVMGuestFile
Copy-VMGuestFile : 3/10/2023 12:08:20 PM Copy-VMGuestFile A specified parameter was not correct:

At D:\Scripts\Scheduled\Upgrade-InfolinkServer\Upgrade-InfolinkServer.ps1:859 char:1
+ Copy-VMGuestFile -VM $NewVM -Source $BackupDestination -Destination " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-VMGuestFile], InvalidArgument
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_DirectoryExistsInGuest_ViError,VMware.V
imAutomation.ViCore.Cmdlets.Commands.CopyVMGuestFile

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Without knowing the content of the variables you are using on that cmdlet it is hard to determine what might be wrong.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos