PowerShell script exchange
Post Reply
xcgedich
Novice
Posts: 5
Liked: never
Joined: Mar 19, 2015 11:04 am
Full Name: xcgedich

After the job script dont work

Post by xcgedich »

Hello Everybody,

I need to run a powershell script after job which one will be doing sth like that:
- check status of backup (Success/Warning/Failed)
- if status is Success -> create file (log) in specific location and send e-mail notification
- if status is Warning or Failed -> send e-mail notification

Unfortunately when I run my ps script manually from Veeam PS Console everything works well, but when I add script (cmd) to my backup job nothing happen :-( (job -> storage -> advanced -> advanced -> Run the following script after the job)

My powershell script look like that:

Code: Select all

	asnp VeeamPSSnapin
	$backuplog="C:\scriptsreport\SUCCESS.log"
	$Job = Get-VBRJob -name "VMs_afters"
	$Error.Clear()

	function SendEmail($To, $From, $Subject, $Body, $attachment, $smtpServer)
	{
			Send-MailMessage -To $To -From $From -Subject $Subject -Body $Body -SmtpServer $smtpServer
	}
	$emailto=tom.mach@xc.local”
	$emailfrom=”veeamserver@xc.local”
	$emailserver="mail.xc.local"
	Get-VBRJob -name "VMs_afters"
	if($job.GetLastresult() -eq "Success")
		{
			remove-item "$backuplog" -Force -Recurse
			Write-Output ("$(get-date -f yyyyMMdd_hhmmss)") | Out-File "$backuplog" -Append
			SendEmail -To "$emailto" -From "$emailfrom" -Subject "VMs_afters Success" -Body "The backup has Success!" -smtpServer "$emailserver"
			break
		} 
	if($job.GetLastresult() -eq "Failed")
		{
			remove-item "$backuplog" -Force -Recurse
			SendEmail -To "$emailto" -From "$emailfrom" -Subject "VMs_afters Failed" -Body "The backup has Failed!" -smtpServer "$emailserver"
			break
		}
	if($job.GetLastresult() -eq "Warning")
		{
			remove-item "$backuplog" -Force -Recurse
			SendEmail -To "$emailto" -From "$emailfrom" -Subject "VMs_afters Warning" -Body "The backup has Warning!" -smtpServer "$emailserver"
			break
		}
My BATCH file (script.cmd) looks like that:

Code: Select all

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -Noninteractive -File "C:\Scripts\vmsafters.ps1"
I try to fix it in may way but everytime failed after backup job nothing happen :-( any clue or idea ?
Thanks Tom
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: After the job script dont work

Post by veremin »

What happens if you try to run the script manually, using Veeam service account?
xcgedich
Novice
Posts: 5
Liked: never
Joined: Mar 19, 2015 11:04 am
Full Name: xcgedich

Re: After the job script dont work

Post by xcgedich »

Hi Vladimir,

I relog to serivce account, run cmd as a user and now I see many errors :/

C:\scripts>C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -Noninterac
tive -File "C:\Scripts\vmsafters.ps1"
Get-VBRJob : Requested registry access is not allowed.
At C:\Scripts\vmsafters.ps1:5 char:8
+ $Job = Get-VBRJob -name "VMs_afters"
+ ~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SecurityException
+ FullyQualifiedErrorId : System.Security.SecurityException

Get-VBRJob : Requested registry access is not allowed.
At C:\Scripts\vmsafters.ps1:15 char:1
+ Get-VBRJob -name "VMs_afters"
+ ~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SecurityException
+ FullyQualifiedErrorId : System.Security.SecurityException

You cannot call a method on a null-valued expression.
At C:\Scripts\vmsafters.ps1:16 char:4
+ if($job.GetLastresult() -eq "Success")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
C:\scripts>
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: After the job script dont work

Post by veremin »

That means that specified account doesn't have required permissions and the scripts don't get executed due to that.
xcgedich
Novice
Posts: 5
Liked: never
Joined: Mar 19, 2015 11:04 am
Full Name: xcgedich

Re: After the job script dont work

Post by xcgedich »

I try to find out, where I need to give higher privileges .

My Veeam Service account is Domain Admin (veeam user),

when I run cmd as a user i got errors (Get-VBRJob : Requested registry access is not allowed.)
when I run cmd as "run as administrator" my batch file run without any errors but nothing happen :-(
when I run cmd via powershell console as a user i got errors (Get-VBRJob : Requested registry access is not allowed.)
when I run cmd via powershell as "run as administrator" my batch file run without any errors but nothing happen :-(
even when I run ps1 script via powershell as normal and "run as administrator" nothing happen :-(
but when I copy all script from ps1 file and run as a administrator, everything works fine.

Maybe somebody have some idea what I need to change on server ? (Win2012R2)

Best Regards
Tom
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: After the job script dont work

Post by veremin »

even when I run ps1 script via powershell as normal and "run as administrator" nothing happen :-(
but when I copy all script from ps1 file and run as a administrator, everything works fine.
Could you elaborate on what you meant by "run ps1 script via PowerShell as normal" and "I copy all script from ps1 file"? Thanks.
xcgedich
Novice
Posts: 5
Liked: never
Joined: Mar 19, 2015 11:04 am
Full Name: xcgedich

Re: After the job script dont work

Post by xcgedich »

"run ps1 script via PowerShell as normal" -> Use powershell console and run powershell script (vmsafters.ps1) as a Domain Admin (name of user: veeam)

I copy all script from ps1 file -> open my script (vmsafters.ps1 ), copy everything and paste dirlecty into powershell console and run

sorry for inconvenience
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: After the job script dont work

Post by veremin »

In 64-bit OS there should be two versions of PowerShell: 32 and 64 ones. Can you try to check versions in both provided cases via the following script? Are those versions the same or different?

Code: Select all

[Environment]::Is64BitProcess
Thanks.
xcgedich
Novice
Posts: 5
Liked: never
Joined: Mar 19, 2015 11:04 am
Full Name: xcgedich

Re: After the job script dont work

Post by xcgedich »

PS C:\scripts> [Environment]::Is64BitProcess
True

PS C:\scripts> [System.Environment]::OSVersion.Version
Major Minor Build Revision
----- ----- ----- --------
6 3 9600 0

PS C:\scripts> (Get-WmiObject -class Win32_OperatingSystem).Caption
Microsoft Windows Server 2012 R2 Datacenter
+ all updates
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 19 guests