After the job script dont work

PowerShell script exchange

After the job script dont work

Veeam Logoby xcgedich » Thu Mar 19, 2015 11:39 am

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
xcgedich
Novice
 
Posts: 5
Liked: never
Joined: Thu Mar 19, 2015 11:04 am
Full Name: xcgedich

Re: After the job script dont work

Veeam Logoby v.Eremin » Thu Mar 19, 2015 12:08 pm

What happens if you try to run the script manually, using Veeam service account?
v.Eremin
Veeam Software
 
Posts: 13290
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: After the job script dont work

Veeam Logoby xcgedich » Thu Mar 19, 2015 12:40 pm

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>
xcgedich
Novice
 
Posts: 5
Liked: never
Joined: Thu Mar 19, 2015 11:04 am
Full Name: xcgedich

Re: After the job script dont work

Veeam Logoby v.Eremin » Thu Mar 19, 2015 1:11 pm

That means that specified account doesn't have required permissions and the scripts don't get executed due to that.
v.Eremin
Veeam Software
 
Posts: 13290
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: After the job script dont work

Veeam Logoby xcgedich » Thu Mar 19, 2015 2:47 pm

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
xcgedich
Novice
 
Posts: 5
Liked: never
Joined: Thu Mar 19, 2015 11:04 am
Full Name: xcgedich

Re: After the job script dont work

Veeam Logoby v.Eremin » Thu Mar 19, 2015 2:55 pm

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.
v.Eremin
Veeam Software
 
Posts: 13290
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: After the job script dont work

Veeam Logoby xcgedich » Thu Mar 19, 2015 2:59 pm

"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
xcgedich
Novice
 
Posts: 5
Liked: never
Joined: Thu Mar 19, 2015 11:04 am
Full Name: xcgedich

Re: After the job script dont work

Veeam Logoby v.Eremin » Thu Mar 19, 2015 3:27 pm

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.
v.Eremin
Veeam Software
 
Posts: 13290
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: After the job script dont work

Veeam Logoby xcgedich » Thu Mar 19, 2015 3:46 pm

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
xcgedich
Novice
 
Posts: 5
Liked: never
Joined: Thu Mar 19, 2015 11:04 am
Full Name: xcgedich


Return to PowerShell



Who is online

Users browsing this forum: Yahoo [Bot] and 8 guests