TSM for VE - Powershell Retry script and more

Toro

Active Newcomer
Joined
Apr 16, 2014
Messages
6
Reaction score
1
Points
0
Hi there,

Old timer reader, first time posting here.

I've created these set of PS scripts a few weeks ago to workaround the bloody 115 quiesce issue when creating snapshots and to be able to retrieve a list of existing VMs avoiding 4390 error for a non existing machine prior running the backup.

Software requirements:

Powershell v4
PowerCLI 5.5 R2
PSTools


Folders to be created:

'C:\SCRIPTS\"
‘C:\Temp\’

With PSTools you need to open up Powershell (RunAs SYSTEM) and store the PowerCLI credentials. We do this so we don't need to pass user/pass in plain text and because it works.

psexec -i -s PowerShell.exe


You then load the VMware plugin:

Add-PSSnapin VMware.VimAutomation.Core

Then we set env variables so we don't get warnings/errors which might stop our script from running:

Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$false
Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false


After that you create the VI credentials (which PowerCLI will load/use):

New-VICredentialStoreItem -Host VC_IP_ADDRESS -User "User" -Password "Password" -File "C:\SCRIPTS\PowerCLIcred.xml"

Once you have done the one-off steps above you will be able to run the first script using the store credentials (only the User who created the xml file will be able to use it, in this case that is SYSTEM, which also runs the TSM Acceptor/Scheduler service).

The first PS script is this one:

$Date = Get-Date -format yyyy.MM.dd
$VMProxy = "NODE_PROXY"
$VMCluster = "VM_CLUSTER"
$ExcludeAlways = Import-Csv -Delimiter "," -Header VM -LiteralPath C:\SCRIPTS\STATIC_EXCLUDE_LIST.txt
$ExcludeTemp = Import-Csv -Delimiter "," -Header VM -LiteralPath C:\SCRIPTS\TEMP_EXCLUDE_LIST.txt

# Load VMCli module and generate VM list from VCenter
Add-PSSnapin VMware.VimAutomation.Core
Sleep 70
$Creds = Get-VICredentialStoreItem -File "C:\SCRIPTS\PowerCLIcred.xml"
Connect-VIServer IP_address -User $Creds.User -Password $Creds.Password
Sleep 60
Get-Cluster -Name $VMCluster | Get-VM | Select Name,Version,Host | Export-Csv C:\Temp\$VMProxy-VMlist-$Date.csv -NoTypeInformation -UseCulture | Out-Null
Sleep 45

# Create Report file to start outputting stuff for later processing
" $Date - $VMProxy Proxy - VM List from $VMCluster has been generated " | Out-File C:\Temp\$VMProxy-Report-$Date.txt -Append
" ## WARNING ## The following VMs are being excluded temporarily " | Out-File C:\Temp\$VMProxy-Report-$Date.txt -Append
Import-Csv -Delimiter "," -Header VM,Incident -LiteralPath C:\SCRIPTS\TEMP_EXCLUDE_LIST.txt | Out-File C:\Temp\$VMProxy-Report-$Date.txt -Append

Sleep 3

# Prepare VM list retrieve from VCenter
Import-CSV -Delimiter "," -Header VM C:\Temp\$VMProxy-VMlist-$Date.csv | Out-File C:\Temp\$VMProxy-VCenterList-$Date.txt
Get-Content C:\Temp\$VMProxy-VCenterList-$Date.txt | Select-Object -Skip 4 | Out-File C:\Temp\$VMProxy-VCenterList2-$Date.txt
Remove-Item C:\Temp\$VMProxy-VCenterList-$Date.txt
Rename-Item -NewName C:\Temp\$VMProxy-VCenterList-$Date.txt -Path C:\Temp\$VMProxy-VCenterList2-$Date.txt

# Prepare Exclude Always list
Import-Csv -Delimiter "," -Header VM C:\SCRIPTS\STATIC_EXCLUDE_LIST.txt | Out-File C:\Temp\$VMProxy-ExcludeAlwaysList-$Date.txt
Get-Content C:\Temp\$VMProxy-ExcludeAlwaysList-$Date.txt | Select-Object -Skip 3 | Out-File C:\Temp\$VMProxy-ExcludeAlwaysList2-$Date.txt
Remove-Item C:\Temp\$VMProxy-ExcludeAlwaysList-$Date.txt
Rename-Item -NewName C:\Temp\$VMProxy-ExcludeAlwaysList-$Date.txt -Path C:\Temp\$VMProxy-ExcludeAlwaysList2-$Date.txt

# Prepare Exclude Temp list
Import-Csv -Delimiter "," -Header VM C:\SCRIPTS\TEMP_EXCLUDE_LIST.txt | Out-File C:\Temp\$VMProxy-ExcludeTempList-$Date.txt
Get-Content C:\Temp\$VMProxy-ExcludeTempList-$Date.txt | Select-Object -Skip 3 | Out-File C:\Temp\$VMProxy-ExcludeTempList2-$Date.txt
Remove-Item C:\Temp\$VMProxy-ExcludeTempList-$Date.txt
Rename-Item -NewName C:\Temp\$VMProxy-ExcludeTempList-$Date.txt -Path C:\Temp\$VMProxy-ExcludeTempList2-$Date.txt

# Merge both Exclude list and remove old ones
gc C:\Temp\$VMProxy-ExcludeTempList-$Date.txt,C:\Temp\$VMProxy-ExcludeAlwaysList-$Date.txt | Out-File C:\Temp\$VMProxy-ExcludeList-$Date.txt
Remove-Item C:\Temp\$VMProxy-ExcludeTempList-$Date.txt,C:\Temp\$VMProxy-ExcludeAlwaysList-$Date.txt

# Compare lists and remove excluded VMs
$ESXList=(gc "C:\Temp\$VMProxy-VCenterList-$Date.txt") -replace " "
$ExcludeList=(gc "C:\Temp\$VMProxy-ExcludeList-$Date.txt") -replace " "

$FinalList = compare-object -referenceobject $ESXList -differenceobject $ExcludeList |Where-Object {$_.SideIndicator -le "<="}
$FinalList | Format-Table -Property InputObject | Out-File C:\Temp\$VMProxy-FinalList-$Date.txt
Get-Content C:\Temp\$VMProxy-FinalList-$Date.txt | Select-Object -Skip 3 | Out-File C:\Temp\FinalList.txt
Remove-Item C:\Temp\$VMProxy-FinalList-$Date.txt
Remove-Item C:\Temp\$VMProxy-VCenterList-$Date.txt,C:\Temp\$VMProxy-ExcludeList-$Date.txt
(gc C:\Temp\FinalList.txt) | ? {$_.trim() -ne "" } | set-content C:\Temp\FinalList.txt



Some things to consider here:

1. There are two exclude lists, reason for this is that the TEMP one will warn you in the output (Line 18).
2. Results are written to the same file as above.
3. You might remove the "Sleeps" commands, it depends on how fast the connections are in your environment.

Second scripts actually runs the backup:

workflow VMincrBKP{
#List of VMs to backup
$VMlist = Get-Content -LiteralPath C:\Temp\FinalList.txt

foreach -parallel -Throttle 5 ($VM in $VMlist){
inlineScript {
$Tries=0
do
{
$Tries++
"###### Try number $Tries ######"
$Date = Get-Date -format yyyy.MM.dd
$StartTime = Get-Date -format HH:mm:ss
$VMProxy = "NODE_PROXY"
#Specify the amount of retries you want
$Retries = 3
$Backup = Start-Process -PassThru -FilePath dsmc.exe -ArgumentList "backup vm $using:VM -asnode=NODE_NAME -mode=IFIncr -optfile=C:\SCRIPTS\dsm.opt " -WorkingDirectory "C:\Program Files\Tivoli\TSM\baclient" -Wait
$Result = $Backup.ExitCode
"####### Success $Result ######"

}until (($Tries -eq $Retries) -or ($Result -le 8))
"####### $using:VM Total Tries $Tries ######"
"####### VM $using:VM Total Tries $Tries ######" -replace '\s+', ' '
"## VM $using:VM Backup Finished - Return Code $Result - Total Tries $Tries #" -replace '\s+', ' ' | Out-File -FilePath C:\Temp\$VMProxy-Report-$Date.txt -Append
$EndTime = Get-Date -format HH:mm:ss
if ($Result -le 8) {Start-Process -PassThru -FilePath dsmadmc.exe -ArgumentList "-ID=ID -PA=pwd issue message I '$VMProxy,$using:VM,Backup Successful,$Date,$StartTime,$EndTime'" -WorkingDirectory 'C:\Program Files\Tivoli\TSM\baclient' }
Else
{Start-Process -PassThru -FilePath dsmadmc.exe -ArgumentList "-ID=ID -PA=pwd issue message W '$VMProxy,$using:VM,Backup Failed,$Date,$StartTime,$EndTime'" -WorkingDirectory 'C:\Program Files\Tivoli\TSM\baclient' }
}
}

}
VMincrBKP


Some things to consider here:

1. Throttle will say how many backups will be happening at the same time (w2k8 has hard coded 5 (iirc), w2k12 doesn't). In my case it will keep always 5 backups active unless it hit the bottom of the list.
2. The last steps (Line 26/28) will generate a message using dsmadmc so you can later do reporting easily based on the TSM server act log.
3. Line 24 will append the results to the txt file generated in the previous script.


Hopefully it might help some guys out there, been using this forum for the last 6 years ;)
 
Last edited:
Back
Top