PowerShell script exchange
Post Reply
col197725
Lurker
Posts: 1
Liked: never
Joined: Jul 15, 2019 10:14 am
Full Name: Colin Battersby
Contact:

BAT script to run powershell script for a backup job on a remote server

Post by col197725 »

Currently the Veeam backup server located at the main site runs a Backup job every night at 00:30. Then at 05:00 the Veeam backup server at the DR site starts the hourly Replication job until midnight. The issue is that the Backup job has been overrunning, which then causes the Replication job to fail because the VMs have active snapshots. Once the Backup job has finished, the Replication job takes up to 3 days of constant running to complete successfully.

Ideally, the Replication job would be disabled when the Backup job starts and then not enabled until the Backup job has completed. The Veeam powershell commands Disable-VBRJob and Enable-VBRjob achieve this, so as a test on the DR site I did the following:

Created a secret file so that the password is not saved in clear text

Code: Select all

[i]Add-PSSnapin VeeamPSSnapin
Connect-VBRServer -Server "DR-site" -User "domain.local\backupadmin" -Password "???????????"
$encPassFileName = 'C:\Users\backupadmin\Documents\veeam-ps-secret'
(Get-Credential).Password | ConvertFrom-SecureString | Set-Content $encPassFileName
Disconnect-VBRServer[/i]
Note: Enter the password in place of all the ???????????

PS to login and run command

Code: Select all

[i]Add-PSSnapin VeeamPSSnapin
$encPassFileName = 'C:\Users\backupadmin\Documents\veeam-ps-secret'
$username = "domain.local\backupadmin"
$password = Get-Content $encPassFileName | ConvertTo-SecureString
$psCredential = New-Object System.Management.Automation.PsCredential($username,$password)
Connect-VBRServer -Server "DR-site" -Credential $PSCredential
#Disable-VBRJob -Job test-schedule
Disable-VBRJob -Job test-schedule | Out-File -FilePath C:\Users\backupadmin\Documents\Disable-OUTPUT.txt
Disconnect-VBRServer[/i]
BAT file to run above PS at the start of the Backup job

Code: Select all

powershell.exe -executionpolicy Bypass -file C:\Users\backupadmin\Documents\Disable-Windchill-Replication.ps1 > C:\Users\backupadmin\Documents\Disable-Bat.txt
The above worked on the DR-site backup server, once I'd changed the logon account to start the Veeam Backup Service to be domain.local\backupadmin. The next step was to test it on the main backup server. I created a new secret file while logged in as domain.local\backupadmin on the main backup server. When I run the BAT file I get this error:

Code: Select all

[i]Set-ExecutionPolicy : Access to the registry key 
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To change the execution 
policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option. To 
change the execution policy for the current user, run "Set-ExecutionPolicy -Scope CurrentUser".
At C:\Users\backupadmin\Documents\Disable-Windchill-Replication.ps1:3 char:1
+ Set-ExecutionPolicy RemoteSigned
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyComma 
   nd
Connect-VBRServer : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Veeam\Veeam Backup and Replication' is 
denied.
At C:\Users\seagate2\Documents\Disable-Windchill-Replication.ps1:8 char:1
+ Connect-VBRServer -Server "mtc-veeam" -Credential $PSCredential
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Connect-VBRServer], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Veeam.Backup.PowerShell.Cmdlets.ConnectVBRServer
 
Disable-VBRJob : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Veeam\Veeam Backup and Replication' is denied.
At C:\Users\backupadmin\Documents\Disable-Windchill-Replication.ps1:9 char:1
+ Disable-VBRJob -Job test-schedule | Out-File -FilePath C ...
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException[/i]

If the PS script is ran from powershell I get the same errors as above accessing the remote registry.

Is there a way for this BAT script to run on a remote server when not ran as administrator and with UAC turned on? Otherwise should this be a feature request for a Backup job to have an option to do this without scripts?

Many thanks

Colin
Vitaliy S.
VP, Product Management
Posts: 27377
Liked: 2800 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: BAT script to run powershell script for a backup job on a remote server

Post by Vitaliy S. »

col197725 wrote:Is there a way for this BAT script to run on a remote server when not ran as administrator and with UAC turned on? Otherwise, should this be a feature request for a Backup job to have an option to do this without scripts?
I don't think that's possible, either UAC needs to be disabled or you need to use the local admin account to trigger the BAT script remotely.
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests