PowerCLI

 View Only
  • 1.  Copying a file onto a virtual machine

    Posted Jun 17, 2015 05:57 PM

    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?



  • 2.  RE: Copying a file onto a virtual machine
    Best Answer

    Posted Jun 17, 2015 08:03 PM

    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.



  • 3.  RE: Copying a file onto a virtual machine

    Posted Jun 22, 2015 01:07 PM

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



  • 4.  RE: Copying a file onto a virtual machine

    Posted Mar 09, 2023 07:03 PM

    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?



  • 5.  RE: Copying a file onto a virtual machine

    Posted Mar 09, 2023 07:41 PM

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



  • 6.  RE: Copying a file onto a virtual machine

    Posted Mar 09, 2023 07:47 PM
    #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}
            }
    
    '@
        }


  • 7.  RE: Copying a file onto a virtual machine

    Posted Mar 09, 2023 07:57 PM

    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


  • 8.  RE: Copying a file onto a virtual machine

    Posted Mar 10, 2023 02:18 PM

    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. 



  • 9.  RE: Copying a file onto a virtual machine

    Posted Mar 10, 2023 02:35 PM

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



  • 10.  RE: Copying a file onto a virtual machine

    Posted Mar 10, 2023 06:03 PM

    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



  • 11.  RE: Copying a file onto a virtual machine

    Posted Mar 10, 2023 06:08 PM

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