PowerShell script exchange
Post Reply
tdewin
Veeam Software
Posts: 1775
Liked: 646 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

5 Useful things you can do with Powershell (webinar)

Post by tdewin »

In this topic you will find all the code for all the scripts presented during the webinair
tdewin
Veeam Software
Posts: 1775
Liked: 646 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Create a job

Post by tdewin »

Code: Select all

param (
  [string]$vms=$(Read-Host -prompt "Please give a comma seperated list of vms"),
  [string]$job=$(Read-Host -prompt "Please give a job name"),
  [string]$repository=$(Read-Host -prompt "Please give a repository")
)
if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) {
  Add-PsSnapin VeeamPSSnapIn
}
#Get-Command -Module VeeamPSSnapin | where { $_.name -match "job" } 
#get-help Add-VBRViBackupJob -full
#gcm -module VeeamPSSnapin | where { $_.name -match "vientity" }
#gcm -module VeeamPSSnapin | where { $_.name -match "repository" }

$repositoryObj = Get-VBRBackupRepository -name $repository
$vmarr = @()
$vms.split(",") | %{
  $vm = $_
  $vmObj = Find-VBRViEntity -name $vm
  if($vmObj)
  {
    $vmarr += $vmObj
  } else { Write-Host "Could not find object $vm" }
}
$jobObj = Add-VBRViBackupJob -Name $job -BackupRepository $repositoryObj -Entity $vmarr
tdewin
Veeam Software
Posts: 1775
Liked: 646 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Configure Job Options

Post by tdewin »

Code: Select all

$jobObj = Get-VBRJob -name "cli"; 

#$options.BackupTargetOptions.Algorithm | gm -static -MemberType Property
$options = $jobObj  | Get-VBRJobOptions
$options.BackupTargetOptions.Algorithm = "Syntethic"
$jobObj = Set-VBRJobOptions -job $jobObj -options $options


$options = $jobObj  | Get-VBRJobOptions
$options.JobOptions.RunManually = $false
$jobObj = Set-VBRJobOptions -job $jobObj -options $options

$scheduleroptions = $jobObj | Get-VBRJobScheduleOptions
$scheduleroptions.OptionsMonthly.Enabled = $true
$scheduleroptions.OptionsDaily.Enabled = $false
$scheduleroptions.OptionsPeriodically.Enabled = $false
$scheduleroptions.OptionsContinuous.Enabled = $false
$scheduleroptions.OptionsMonthly.DayOfWeek = "Friday"
$scheduleroptions.OptionsMonthly.DayNumberInMonth = "First"
$jobObj = Set-VBRJobScheduleOptions -job $jobObj -options $scheduleroptions
The code for cloning the job options and scheduled options

Code: Select all

$clonejob = Get-VBRJob -name "cli"
get-vbrjob -name "cli2" | Set-VBRJobOptions -options $(get-vbrjoboptions $clonejob) | Set-VBRJobScheduleOptions -Options $(Get-VBRJobScheduleOptions $clonejob)
tdewin
Veeam Software
Posts: 1775
Liked: 646 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Massive VSS password update

Post by tdewin »

Code: Select all

param (
  [string]$jobs=$(Read-Host -prompt "Please give a comma seperated list of jobs"),
  [string]$user=$(Read-Host -prompt "Please enter your account"),
  [System.Security.SecureString]$pass = $(Read-Host -assecurestring "Please enter your password")
)
if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) {
  Add-PsSnapin VeeamPSSnapIn
}

$basicstring = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)
$strpass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BasicString)

#([type]"Veeam.Backup.Common.CCredentials").GetConstructors() | foreach-object {  "(";$_.GetParameters() | foreach-object { ""+$_.ParameterType+" "+$_.Name};")" } 
$creds = New-Object -TypeName Veeam.Backup.Common.CCredentials -ArgumentList $user,$strpass,0,0


$jobs.split(",") | %{ Get-VBRJob -name $_ | %{ 
  $vbrjob = $_
  $vbrname = $vbrjob.Name
  Write-Host "Configuring job $vbrname"
  
  $vssoptions = $vbrjob | Get-VBRJobVSSOptions
  $vssoptions.enabled = $true    
  $vssoptions.GuestFSIndexingType = [Veeam.Backup.Model.CVssOptions+EGuestFSIndexingType]::ExceptSpecifiedFolders
  $vssoptions.Credentials = $creds
  
  $result = $vbrjob | set-vbrjobvssoptions -options $vssoptions
 }
}
tdewin
Veeam Software
Posts: 1775
Liked: 646 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Reports

Post by tdewin »

Report on jobs

Code: Select all

if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) {
  Add-PsSnapin VeeamPSSnapIn
}

$tab = @()
Get-VBRJob | where { $_.Jobtype -eq "Backup" } | ForEach-Object { 
  $entry = New-Object psobject
  $entry | Add-Member -type NoteProperty -name "Name" -value $_.Name
  $entry | Add-Member -type NoteProperty -name "Next Run" -value $_.ScheduleOptions.NextRun
  $entry | Add-Member -type NoteProperty -name "Latest" -value $_.ScheduleOptions.LatestRun
  $entry | Add-Member -type NoteProperty -Name "Success" -Value $_.getLastResult()
  $tab += $entry
}
$tab | Export-Csv -Path export.csv
$header = "<title>Custom Report</title><style>body { background-color:#DDDDDD;}</style>"
$tab | ConvertTo-Html -head $header | Out-File report.html
Report on Backup Repositories

Code: Select all

if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) {
  Add-PsSnapin VeeamPSSnapIn
}

$tab = @()
Get-VBRBackupRepository | ForEach-Object { 
  $entry = New-Object psobject
  $entry | Add-Member -type NoteProperty -name "Name" -value $_.Name
  $entry | Add-Member -type NoteProperty -name "Type" -value $_.Type
  $backups = ""
  foreach($backup in $_.getBackups())
  {
    $backups += $backup.Name+", "
  }
  $backups = $backups -replace ", $",""
  $entry | Add-Member -type NoteProperty -name "Backups" -value $backups
  $tab += $entry
}
$header = "<title>Custom Report</title><style>"
$header = $header + "BODY{font-family: Tahoma;}"
$header = $header + "TABLE{font-family: Tahoma;border-collapse: collapse;}"
$header = $header + "TH{font-family: Tahoma;background-color: #00B050;color: White;font-weight: bold;font-size: 16px;height: 70px;vertical-align: bottom;padding: 0 0 15px 15px;}"
$header = $header + "TD{font-family: Tahoma;font-size: 12px;border: 1px solid #a7a9ac;}"
$header = $header + "</style>"
$tab | ConvertTo-Html -head $header | Out-File report.html
Same report but mail the report

Code: Select all

if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) {
  Add-PsSnapin VeeamPSSnapIn
}

$tab = @()
Get-VBRBackupRepository | ForEach-Object { 
  $entry = New-Object psobject
  $entry | Add-Member -type NoteProperty -name "Name" -value $_.Name
  $entry | Add-Member -type NoteProperty -name "Type" -value $_.Type
  $backups = ""
  foreach($backup in $_.getBackups())
  {
    $backups += $backup.Name+", "
  }
  $backups = $backups -replace ", $",""
  $entry | Add-Member -type NoteProperty -name "Backups" -value $backups
  $tab += $entry
}
$header = "<title>Custom Report</title><style>"
$header = $header + "BODY{font-family: Tahoma;}"
$header = $header + "TABLE{font-family: Tahoma;border-collapse: collapse;}"
$header = $header + "TH{font-family: Tahoma;background-color: #00B050;color: White;font-weight: bold;font-size: 16px;height: 70px;vertical-align: bottom;padding: 0 0 15px 15px;}"
$header = $header + "TD{font-family: Tahoma;font-size: 12px;border: 1px solid #a7a9ac;}"
$header = $header + "</style>"

$smtpServer = "localhost"

$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = "veeamreports@yourcompany.com"
$msg.ReplyTo = "noreply@yourcompany.com"
$msg.To.Add("it@yourcompany.com")
$msg.subject = "Repository Job Report"
$msg.body = $tab | ConvertTo-Html -head $header 
$msg.IsBodyHTML = $true;

$smtp.Send($msg)
Report on sessions

Code: Select all

if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) {
  Add-PsSnapin VeeamPSSnapIn
}
$header = "<title>Custom Report</title><style>"
$header = $header + "BODY{font-family: Tahoma;}"
$header = $header + "TABLE{font-family: Tahoma;border-collapse: collapse;}"
$header = $header + "TH{font-family: Tahoma;background-color: #00B050;color: White;font-weight: bold;font-size: 16px;height: 70px;vertical-align: bottom;padding: 0 0 15px 15px;}"
$header = $header + "TD{font-family: Tahoma;font-size: 12px;border: 1px solid #a7a9ac;}"
$header = $header + "TD.red{background-color:#FF6E77;}"
$header = $header + "TD.orange{background-color:#FFC180;}"
$header = $header + "TD.green{background-color:#00B050;}"
$header = $header + "</style>"


$today = Get-Date
$after = $today.AddDays(-30)

$sessions = Get-VBRBackupSession | Sort-Object endtime -Descending | where { $_.endtime -gt  $after}

$intro = "<h1>Sessions</h1><ul><li>From $after</li><li>To $today</li></ul>"

$tab = $sessions | select Jobname,Result,EndTime,CreationTime
$tabhtml = $tab | ConvertTo-Html -fragment 
$tabhtml = $tabhtml -replace "<td>Failed","<td class=red>Failed"
$tabhtml = $tabhtml -replace "<td>Warning","<td class=orange>Warning"
$tabhtml = $tabhtml -replace "<td>Success","<td class=green>Success"


ConvertTo-Html -head $header -Body "$intro $tabhtml"  | Out-File report.html
tdewin
Veeam Software
Posts: 1775
Liked: 646 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Setting up a post job

Post by tdewin »

THIS WILL RUN ON EVERY JOB!!!

Code: Select all

if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) {
  Add-PsSnapin VeeamPSSnapIn
}

Get-VBRJob | where { ($_.Jobtype -eq "Backup")} | ForEach-Object { 
  $vbrjob = $_
  $vbrname = $vbrjob.Name
  Write-Host "Adding post job $vbrname"
  
  $postjob = "`"C:\Program Files\Veeam\Cloud Backup\cbb.exe`" plan -r `"Plan $vbrname`""
  $joboptions = $vbrjob | Get-VBRJobOptions
        
  $joboptions.PostJobcommand.Enabled = $True
  $joboptions.PostJobcommand.CommandLine = $postjob
  $joboptions.PostJobcommand.Periodicity = "Cycles"
  $joboptions.PostJobcommand.Frequency = 1
  $vbrjob = $vbrjob | Set-VBRJobOptions -Options $joboptions
}
tdewin
Veeam Software
Posts: 1775
Liked: 646 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Pre and Post Scripts

Post by tdewin »

Simple Example

Code: Select all

$trigger = New-JobTrigger -Daily -At 3am
$trigger = New-JobTrigger -Once -At 11:27
$job = "JOB0011"
Register-ScheduledJob -Name  "Veeam_$job" -Trigger $trigger -ArgumentList ($job) -ScriptBlock {
  param([string]$job)
  if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) { 
    Add-PsSnapin VeeamPSSnapIn 
  }
    
  #Do Precommands here
  Get-vbrjob -name $job | Start-vbrjob
}
Fault Tolerance Example (use with care, this was only made to be used as a show case in the presentation so please review and build in your own checks)

Code: Select all


$job = "cli"
$vms = "SMALL"
$vchost = "192.168.149.10"
$vcuser = "sixtyfive\ftjob"
$attime = "11:54"

[System.Security.SecureString]$pass = $(Read-Host -assecurestring "Please enter your password")
$vcpass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))


if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) { 
    Add-PsSnapin VeeamPSSnapIn 
}

#You can just skip this by manually disabling CBT
$vbrjob = Get-vbrjob -name $job
$joboptions = $vbrjob | Get-VBRJobOptions
$joboptions.hvsourceoptions.UseChangeTracking = $False
$vbrjob = $vbrjob | Set-VBRJobOptions -Options $joboptions



#Script Block
$sb = {
 param([string]$job,[string]$vms,[string]$vchost,[string]$vcuser,[string]$vcpass)
 if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) { 
  Add-PsSnapin VeeamPSSnapIn 
 }
 if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null) {
  Add-PSSnapin VMware.VimAutomation.Core 
 }
 $vcserver = Connect-Viserver -server $vchost -user $vcuser -password $vcpass -Force:$true
 $vmsarr = $vms -split "," 
 $vmsarr | foreach {
    Get-VM -Name $_ | foreach {
        $vmview = $_ | Get-View
        $vmview.TurnOffFaultToleranceForVM()
    }
 }
 Get-vbrjob -name $job | Start-vbrjob
 $vmsarr | foreach {
  $vmview = Get-VM -Name $_ | select -First 1 | Get-View
  $vmview.CreateSecondaryVM($null)
 }
 Disconnect-VIServer $vcserver -Confirm:$false -Force:$true
} 

$trigger = New-JobTrigger -Once -At $attime
Register-ScheduledJob -Name  "Veeamft_$job" -Trigger $trigger -ScriptBlock $sb -ArgumentList ($job,$vms,$vchost,$vcuser,$vcpass)
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests