PowerShell script exchange
Post Reply
DaKernel
Enthusiast
Posts: 28
Liked: 3 times
Joined: Dec 10, 2019 5:37 pm
Full Name: Larry Heintz
Contact:

Powershell to install veeam agent

Post by DaKernel »

Hello,

I am finally getting around to trying to use powershell to push out agent install and creating iso but I am running into an issue with trying to pass arguments using Start-Process.
The $strLocalFile is set to where the agent exe is. When I try to run the code below via Visual Studio Code there is a popup window for the veeam agent install, which the /silent it suppose to handle.

Code: Select all

$veeamInstallArgumentList = @('/silent', '/accepteula', '/acceptthirdpartylicenses')
Start-Process -Wait -verb runas -FilePath $strLocalFile -ArgumentList $veeamInstallArgumentList -PassThru -NoNewWindow
If I run the cmd line code below that the above code is based on, the cmd line runs fine

Code: Select all

C:\utility\VeeamAgentWindows_5.0.3.4708.exe /silent /accepteula /acceptthirdpartylicenses
I have also tried passing the install arguments like below and still same issue

Code: Select all

Start-Process -Wait -verb runas -FilePath $strLocalFile -ArgumentList /silent', '/accepteula', '/acceptthirdpartylicenses' -PassThru -NoNewWindow
I know it is not the veeam agent install causing the issue, its how I am trying to pass the install arguments that is causing the issue.
First time trying to do the install via powershell, so any help would be greatful!

Larry
Egor Yakovlev
Product Manager
Posts: 2580
Liked: 708 times
Joined: Jun 14, 2013 9:30 am
Full Name: Egor Yakovlev
Location: Prague, Czech Republic
Contact:

Re: Powershell to install veeam agent

Post by Egor Yakovlev »

Hi Larry.

That code works like a charm to me:

Code: Select all

Start-Process -FilePath "C:\Temp\VeeamAgentWindows_5.0.3.4708.exe" -ArgumentList "/silent","/accepteula","/acceptthirdpartylicenses" -NoNewWindow -PassThru
Differences I see:
- launching from PowerShell, not from VSC
- launching from elevated console, instead of elevating inline -Verb RunAs
- using double quotes?(*shrug*)

Remember that each attempt to install is logged under C:\ProgramData\Veeam\Setup\Temp\VeeamEPBootstrap_{timestamp}.

/Cheers!
DaKernel
Enthusiast
Posts: 28
Liked: 3 times
Joined: Dec 10, 2019 5:37 pm
Full Name: Larry Heintz
Contact:

Re: Powershell to install veeam agent

Post by DaKernel »

Egor, thank you for the point in the right direction!! I normally use Powershell ISE for script development, but switched to VSC recently.
I ran the script from elevated Powershell ISE and it ran after removing the -verb runas as well as the -wait flags.
I should have the script fully completed with logging next week to share.

I hope it helps others that are using community edition where agent upgrades can not be done via the B/R Console.
DaKernel
Enthusiast
Posts: 28
Liked: 3 times
Joined: Dec 10, 2019 5:37 pm
Full Name: Larry Heintz
Contact:

Re: Powershell to install veeam agent

Post by DaKernel » 2 people like this post

Below is the code for the powershell script. I have tested it in our environment and we use UNC Paths for where we store the agent file and where we log to. I also tend to do alot of logging, so you can comment out the logging line by line if you wish Those can be changed in the variables to point to local directories without any issues (I hope). The ISO that is created will have the agent version in the name as well.

Hopefully, all should work.

Code: Select all

<#
.SYNOPSIS

This script will perform a silent Windows Veeam Agent install/upgrade and create a new boot ISO

.DESCRIPTION

The script will copy the agent exe from one location either remote or local directory and copy it to a local directory.
If the copy to local directory does not exist it will be created.
The next step is the installation of windows agent install. The install will have 3 arguments passed to it "/silent", "/accepteula", "/acceptthirdpartylicenses",
making it a silent install. The script will not move foreward until the agent has been installed, that is the purpose of the Do, Until loop.
Once the windows agent install is complete it will clean up the agent file that was copied to the local directory.
Last step is to create the new boot iso and copy it to another location. There are 2 arguments passed to that "/createrecoverymediaiso", "/f:$strVeeamISOPath"

.EXAMPLE 

veeamAgentUpgrade.ps1

.NOTES

The script must be launched from an elevated powershell prompt
For the $strVeeamISOPath it is set in the script for a UNC Path. You can change to point to a local directory if needed.
There is no error trapping in this script as it should just work if all the variables are set correctly and all paths are valid.

#>

#==========================================================#
$strTab = [char]9
$intSleep = 30
$dtStartTime = $(Get-Date)
$strComputerName = $env:COMPUTERNAME
$strCopyPathFrom = "FULLPATHFILE_INCLUDEFILENAME"
$strCopypathTo = "C:\Utility"
$strLocalFile = $strCopypathTo+"\"+$strCopyPathFrom.Split("\")[7]+".exe"
$strLocalVeeamFile = $strCopyPathFrom.Split("\")[7]+".exe"
$strISOVersion = $strLocalVeeamFile.Split("_")[1].Split("exe")[0]
$strISOVersion = $strISOVersion.Substring(0,$strISOVersion.Length-1)
$strVeeamExe = "C:\Program Files\Veeam\Endpoint Backup\Veeam.EndPoint.Manager.exe"
$strVeeamISOPath = "\\UNCPATHTOMOVEISOTO\$strComputerName"+"_"+$strISOVersion+".iso"
$strResultsLogPath = "\\UNCPATHFORLOGSTOGO\VeeamWindowsAgentUpgrade\$strComputerName.log"

Add-Content $strResultsLogPath "===================================================================================="
Add-Content $strResultsLogPath "$(Get-Date) : Starting script"
Add-Content $strResultsLogPath "$(Get-Date) : strComputerName = $strComputerName"
Add-Content $strResultsLogPath "$(Get-Date) : strCopyPathFrom = $strCopyPathFrom"
Add-Content $strResultsLogPath "$(Get-Date) : strCopypathTo = $strCopypathTo"
Add-Content $strResultsLogPath "$(Get-Date) : strLocalFile = $strLocalFile"
Add-Content $strResultsLogPath "$(Get-Date) : strVeeamExe = $strVeeamExe"
Add-Content $strResultsLogPath "$(Get-Date) : strVeeamISOPath = $strVeeamISOPath"
Add-Content $strResultsLogPath "$(Get-Date) : strLocalVeeamFile = $strLocalVeeamFile"
Add-Content $strResultsLogPath "$(Get-Date) : strISOVersion = $strISOVersion"
Add-Content $strResultsLogPath "$(Get-Date) : strResultsLogPath = $strResultsLogPath"

write-host "Starting Windows Veeam Agent install"
#lets see if path exists
#if it does, copy file
#if it does not, make folder and copy path
if((Test-Path -path $strCopypathTo) -eq $true){
    Copy-Item  $strCopyPathFrom -Destination $strCopypathTo
    Add-Content $strResultsLogPath "$(Get-Date) : $strTab File has been copied from $strCopyPathFrom to $strCopypathTo"
    write-host "File has been copied from $strCopyPathFrom to $strCopypathTo"
}
else {
    New-Item -Path "C:\" -Name $strCopypathTo.Split("\")[1] -ItemType "directory"
    Copy-Item  $strCopyPathFrom -Destination $strCopypathTo
    Add-Content $strResultsLogPath "$(Get-Date) : $strTab path $strCopypathTo does not exist, creating it"
    Add-Content $strResultsLogPath "$(Get-Date) : $strTab File has been copied from $strCopyPathFrom to $strCopypathTo"
    write-host "creating path $strCopypathTo.Split("\")[1]"
    write-host "File has been copied from $strCopyPathFrom to $strCopypathTo"
}

write-host "STARTING INSTALL"
Add-Content $strResultsLogPath "$(Get-Date) : [STARTING INSTALL]"
$veeamInstallArgumentList = @("/silent", "/accepteula", "/acceptthirdpartylicenses")
Add-Content $strResultsLogPath "$(Get-Date) : $strTab veeamInstallArgumentList = $veeamInstallArgumentList"
Add-Content $strResultsLogPath "$(Get-Date) : $strTab Start-Process -FilePath $strLocalFile -ArgumentList $veeamInstallArgumentList -PassThru -NoNewWindow"
#Actual install process
Start-Process -FilePath $strLocalFile -ArgumentList $veeamInstallArgumentList -PassThru -NoNewWindow


#The ProcessList holds the veeam agent exe file name
$ProcessList = @($strCopyPathFrom.Split("\")[7])
Add-Content $strResultsLogPath "$(Get-Date) : $strTab ProcessList = $ProcessList"

#Now we loop until the install is done
Add-Content $strResultsLogPath "$(Get-Date) : $strTab entering Do, Until loop for Veeam Windows Agent install"
Add-Content $strResultsLogPath "$(Get-Date) : $strTab will stay here until install is done"
Do {  
    $ProcessesFound = Get-Process | Where-Object {$ProcessList -contains $_.Name} | Select-Object -ExpandProperty Name
    If ($ProcessesFound) {
        Write-Host "Install still running: $($ProcessesFound) sleeping for $intSleep seconds"
        Add-Content $strResultsLogPath "$(Get-Date) : $strTab Install still running: $($ProcessesFound) sleeping for $intSleep seconds"
        Start-Sleep $intSleep #sleeps for $intSleep seconds then runs again
    }
} Until (!$ProcessesFound) #write-host "is down Jim"
#Lets clean these up need to reuse them
Clear-Variable -Name "ProcessesFound"
Clear-Variable -Name "ProcessList"


write-host "INSTALL COMPLETE"
Add-Content $strResultsLogPath "$(Get-Date) : [INSTALL COMPLETE]"

#Lets delete veeam agent file
#If file count is 1, we just created the utility folder, delete it
#if file count is more that 1, just delete agent install file
write-host "Deleting $strLocalVeeamFile"
Add-Content $strResultsLogPath "$(Get-Date) : $strTab Deleting $strLocalVeeamFile"
if((Get-ChildItem -Path $strCopypathTo -File | Measure-Object).Count -eq 1){
    Remove-Item -Path $strCopypathTo -Recurse -Force
}
else {
    #Now lets delete the veeam agent install file
    Remove-Item -Path $strLocalFile 
}

#Now we need to create the boot disk ISO
write-host "STARTING TO CREATE VEEAM BOOT ISO"
Add-Content $strResultsLogPath "$(Get-Date) : [STARTING TO CREATE VEEAM BOOT ISO]"
$veeamISOArgumentList = @("/createrecoverymediaiso", "/f:$strVeeamISOPath")
Add-Content $strResultsLogPath "$(Get-Date) : $strTab veeamISOArgumentList = $veeamISOArgumentList"
Add-Content $strResultsLogPath "$(Get-Date) : $strTab Start-Process -FilePath $strVeeamExe -ArgumentList $veeamISOArgumentList -PassThru -NoNewWindow"
#Actual running process
Start-Process -Wait -FilePath $strVeeamExe -ArgumentList $veeamISOArgumentList -PassThru

write-host "FINISHED CREATING VEEAM BOOT ISO"
Add-Content $strResultsLogPath "$(Get-Date) : [FINISHED CREATING VEEAM BOOT ISO]"

write-host "The installation and boot disk process has been completed"
write-host "Go here to look at the log file, $strResultsLogPath"

#set end time
$dtEndTime = $(Get-Date)
Add-Content $strResultsLogPath "$(Get-Date) : $strTab dtEndTime = $dtEndTime"
#Lets see how long it took the script to run
$dtDifference = $dtEndTime - $dtStartTime

Add-Content $strResultsLogPath "$(Get-Date) : Stopping script"
#Log script run time
Add-Content $strResultsLogPath "$(Get-Date) : $strTab Script ran for $dtDifference (Hour:Minute:Second:Millisecond)"

#Now lets clean up
Clear-Variable -Name "strResultsLogPath"
Clear-Variable -Name "dtStartTime"
Clear-Variable -Name "dtEndTime"
Clear-Variable -Name "dtDifference"
Clear-Variable -Name "veeamISOArgumentList"
Clear-Variable -Name "ProcessesFound"
Clear-Variable -Name "ProcessList"
Clear-Variable -Name "veeamInstallArgumentList"
Clear-Variable -Name "strResultsLogPath"
Clear-Variable -Name "strVeeamISOPath"
Clear-Variable -Name "strVeeamExe"
Clear-Variable -Name "strLocalFile"
Clear-Variable -Name "strCopypathTo"
Clear-Variable -Name "strCopyPathFrom"
Clear-Variable -Name "strComputerName"
Clear-Variable -Name "strTab"
Clear-Variable -Name "strLocalVeeamFile"
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests