PowerShell script exchange
Post Reply
Andreas Neufert
VP, Product Management
Posts: 6747
Liked: 1408 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Andys scripting corner -Set explicit VM VSS credentials (v6)

Post by Andreas Neufert »

This example adds VSS credentials to a VM in a Job.

Start code:
SetVBRJobObjectVSSOptionsYourPowershellscriptexampleStartYourPowershellscript.bat

Code: Select all

@ECHO OFF
REM Veeam                                                                              
REM                                                                                                            
REM Author: Andreas Neufert - Veeam Software Systems Engineer Centrael EMEA (Germany)
REM                                                                                                                                                                                                
REM March 2012                                                                                                 
REM
REM 
REM This script starts a Powershellscript and document what it does in a joblog                                                                                                                                                                            
REM                                                                                                          
REM #################################################################

REM  PowerShell.exe
set powershell=c:\windows\system32\windowspowershell\v1.0\powershell.exe

REM  Set Folder with the SetVBRJobObjectVSSOptionsYourPowershellscriptexample.ps1 in it
set PS1=E:\scripts\


REM Output LOG incl. Folder
set joblog=E:\scripts\SetVBRJobObjectVSSOptionsYourPowershellscriptexample.log


REM ###################################################################
"%powershell%" "%PS1%SetVBRJobObjectVSSOptionsYourPowershellscriptexample.ps1" >> "%joblog%"
Your code:
SetVBRJobObjectVSSOptionsYourPowershellscriptexample.ps1

Code: Select all

$vbrjobname = "ad"
$VM = "Exchange3"
$username = "Administrator"
$password = "Password"
$domain = "Domain"
./SetVBRJobObjectVSSOptions.ps1 $vbrjobname $VM $username $password $domain
Program code:
SetVBRJobObjectVSSOptions.ps1

Code: Select all

#############################################################################################################
#                                                                                                           
# Veeam                                                                              
#                                                                                                           
# Author: Andreas Neufert - Systems Engineer Central EMEA (Germany)                                                                                   
#                                                                                                           
# March 2012
#
# Version 1.01                                                                                                
#                                                                                                           
# ./SetVBRJobObjectVSSOptions.ps1 <object:vbrjobname> <objects:VM> <object:Username> <object:Password> <object:Domain>
#
# This script adds VSS Credentials to a single VM in a Job                                                                                                                                                                         
#                                                                          
#                                                                                                           
#############################################################################################################
#Manual Input (delete # in front of the next lines)
#$vbrjobname = "ad"
#$VMs = "Exchange3"
#$username = "Administrator"
#$password = "Password"
#$domain = "Domain"
#############################################################################################################
# External Input
$vbrjobname = $args[0]
$VMs = $args[1]
$username = $args[2]
$password = $args[3]
$domain = $args[4]
#############################################################################################################
# Empty lines to see every code in the powershellwindow (jup under Powershellprozessing bar)
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "

#Loads Veeam Powershell Snapin
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue

#Reset Error Counter
$Resultcounter = 0

#Write informations with timestamps
$5Time = get-date
$5TimeFormated = $5Time.ToUniversalTime()
Write-host $5TimeFormated "Information: SetVBRJobObjectVSSOptions.ps1"
$AStartTime = get-date
$AStartTimeFormated = $AStartTime.ToUniversalTime()
write-host $AStartTimeFormated "Information: Start time:" $AStartTimeFormated
$6Time = get-date
$6TimeFormated = $6Time.ToUniversalTime()
write-host $6TimeFormated "Information: Adding Credentials for VM " $VMs  " to Job: " $vbrjobname


#Load Job object and check if it exists
$vbrjob = Get-VBRJob | Where {$_.Name -eq $vbrjobname}
if  ($vbrjob -eq $Null)  {
$resultcounter++
$1Time = get-date
$1TimeFormated = $1Time.ToUniversalTime()
Write-host $1TimeFormated "Error: Jobname not correct. Jobname: " $vbrjobname
} Else {
$2Time = get-date
$2TimeFormated = $2Time.ToUniversalTime()
Write-host $2TimeFormated "Information: Jobname name accepted. Jobname: " $vbrjobname

#Load Jobobject (VM) object and check if it exists
$vmloaded = Get-VBRJob -Name $vbrjobname | Get-VBRJobObject -Name $VMs
if  ($vmloaded -eq $Null)  {
$resultcounter++
$4Time = get-date
$4TimeFormated = $4Time.ToUniversalTime()
Write-host $4TimeFormated "Error: VM " $VMs " not included in Job " $vbrjobname
} Else {
$3Time = get-date
$3TimeFormated = $3Time.ToUniversalTime()
Write-host $3TimeFormated "Information: " $VMs " is included in job " $vbrjobname

#Set new credentials
Write-host "-----------------------------------------------------------------"
$cred1 = "$domain\$username"
$cred2 = "$password"
$Credentials = New-Object -TypeName Veeam.Backup.Common.CCredentials -ArgumentList $cred1,$cred2,0,0
$VSSJobOptions = $vmloaded.GetVssOptions()
$VSSJobOptions.Credentials = $Credentials
Set-VBRJobObjectVssOptions -object $vmloaded -options $VSSJobOptions
Write-host "-----------------------------------------------------------------"

#End ($vmloaded -eq $Null) 
}
#End ($vbrjob.TotalItemSize -eq $Null) 
}

#Results
IF ($Resultcounter -lt 1)
{
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 
write-host "Information: Job Finished"    
write-host "VSS Credentials where added successfully to VM " $VMs " at Job " $vbrjobname
$AEndTime = get-date
$AEndTimeFormated = $AEndTime.ToUniversalTime()
Write-host "Job started at " $AStartTimeFormated
Write-host "Job finished at " $AEndTimeFormated
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
}
ELSE
{
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
$AEndTime = get-date
$AEndTimeFormated = $AEndTime.ToUniversalTime()
Write-host $AEndTimeFormated "Information: ERROR ERROR ERROR ERROR ERROR ERROR ERROR"
Write-host $AEndTimeFormated "Information: Job Finished with Errors"
Write-host $AEndTimeFormated "Error: There are " $Resultcounter "Errors"
Write-host $AEndTimeFormated "Information: Job started at " $AStartTimeFormated
Write-host $AEndTimeFormated "Information: Job finished at " $AEndTimeFormated
Write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"    
}
Log:

Code: Select all

. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
27.03.2012 09:45:53 Information: SetVBRJobObjectVSSOptions.ps1
27.03.2012 09:45:53 Information: Start time: 27.03.2012 09:45:53
27.03.2012 09:45:53 Information: Adding Credentials for VM  Exchange3  to Job:  ad
27.03.2012 09:45:58 Information: Jobname name accepted. Jobname:  ad
27.03.2012 09:46:06 Information:  Exchange3  is included in job  ad
-----------------------------------------------------------------


Id                  : a9c7eaae-8d69-4691-84c6-c1ad547ed4b7
Object              : Name: Exchange3
Filter              : 
Name                : Exchange3
Type                : Include
Location            : vcenter.demolab.an.veeam.de\Exchange3
ApproxSizeString    : 0,0 KB
Info                : Veeam.Backup.Model.CDbObjectInJobInfo
JobId               : 43d287be-0f12-48a2-849b-883006208857
IsFolder            : False
IsIncluded          : True
IsVssChild          : False
IsExcludeDisksChild : False
IsExcluded          : False
IsExtended          : False
PolicyType          : None
VssOptions          : <CVssOptions><Enabled>True</Enabled><IgnoreErrors>False</
                      IgnoreErrors><GuestFSIndexingType>ExceptSpecifiedFolders<
                      /GuestFSIndexingType><Credentials><UserName>Domain\Admini
                      strator</UserName><Password>AQAAANCMnd8BFdERjHoAwE/Cl+sBA
                      AAAHm8lXvyCt0C3raNuvC1l2QAAAAACAAAAAAADZgAAqAAAABAAAABKa2
                      quH7SDifD4XSoTQrQkAAAAAASAAACgAAAAEAAAAL+X2bu4wwj116Cv5fr
                      8e5cQAAAAJUcwSvEWsqqJsF31KMLQZhQAAADdzmLYQeCjy7XzF1mlPr9y
                      6Ju6sA==</Password><IsLocalProtect>False</IsLocalProtect>
                      <CurrentUser>False</CurrentUser></Credentials><Transactio
                      nLogsTruncation>OnlyOnSuccessJob</TransactionLogsTruncati
                      on><IsFirstUsage>True</IsFirstUsage><IncludedIndexingFold
                      ers /><ExcludedIndexingFolders><string>%windir%</string><
                      string>%ProgramFiles%</string><string>%TEMP%</string></Ex
                      cludedIndexingFolders></CVssOptions>
ExtendedOptions     : Veeam.Backup.Model.COijExtendedOptions
DiskFilter          : 2000;2001;2002;2003;2004;2005;2006;2008;2009;2010;2011;20
                      12;2013;2014;2015;2016;2017;2018;2019;2020;2021;2022;2024
                      ;2025;2026;2027;2028;2029;2030;2031;2032;2033;2034;2035;2
                      036;2037;2038;2040;2041;2042;2043;2044;2045;2046;2047;204
                      8;2049;2050;2051;2052;2053;2054;2056;2057;2058;2059;2060;
                      2061;2062;2063
UpdateConfig        : True

-----------------------------------------------------------------
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Information: Job Finished
VSS Credentials where added successfully to VM  Exchange3  at Job  ad
Job started at  27.03.2012 09:45:53
Job finished at  27.03.2012 09:46:10
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


Feedback or comments are very welcome.

CU Andy
Attachments
SetVBRJobObjectVSSOptions.rar
(3.54 KiB) Downloaded 192 times
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests