PowerShell script exchange
Post Reply
sebkoe
Influencer
Posts: 14
Liked: 1 time
Joined: Aug 20, 2021 11:07 am
Full Name: Sebastian
Contact:

Problem with Powershell Script in SureBackup Job

Post by sebkoe »

Hi,

i tried to create a Script that should do the following in a SureBackup Job.

1. Open a File on a Windows Share of the loaded VM
2. Check the Content of the File
3. exit 0 or 1 depending on the content

If i start the VM in SureBackup and execute the Script manually on the VBR Server (VBR 12.1) the Script is working fine. If i use the same Script inside the SureBackup Job it failes.

Here is the Script i use:

Code: Select all

$networkPath = "\\10.255.150.113\Monitoring\veeam.txt"

$networkUsername = "yyyyyyy"
$networkPassword = ConvertTo-SecureString "xxxxxx" -AsPlainText -Force

$networkCredential = New-Object -TypeName PSCredential -ArgumentList $networkUsername, $networkPassword

$result = Invoke-Command -ScriptBlock {
    param($filePath, $credentials)
    $content = Get-Content $filePath
    $content -contains "OK"
} -ArgumentList $networkPath, $networkCredential

if ($result) {
    Write-Host "Datei enthält 'OK'. Rückgabewert: 0"
    exit 0
} else {
    Write-Host "Datei enthält kein 'OK'. Rückgabewert: 1"
    exit 1
}
If the Files contains the word "OK", exit 0. If not, exit 1.

10.255.150.113 is the IP of the VM inside SureBackup. As mentioned, exactly that Script works fine when started manually against the running VM in SureBackup.

Inside of SureBackup i get the following error in the Veeam Logs:

Code: Select all

[Console] +     $content = Get-Content $filePath
[Console] +                ~~~~~~~~~~~~~~~~~~~~~
[Console]     + CategoryInfo          : PermissionDenied: (\\10.255.150.113\Monitoring\veeam.txt:String) [Get-Content], UnauthorizedAccessException
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand
[...]
I have no idea why this is working outside of SureBackup but not inside. The Permissions of the Share and/or File are looking perfectly fine for me.

Maybe someone here is able to help.

Thank you and best regards
Sebastian
david.domask
Veeam Software
Posts: 2113
Liked: 509 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Problem with Powershell Script in SureBackup Job

Post by david.domask »

Hi,

Can you check which account is running the Veeam Backup Service from services.msc? If it's SYSTEM, try using PSEXEC to launch powershell as SYSTEM account and see if it works or has similar issue. Also, may I ask is there a reason you use Invoke-Command in the script instead of either just New-SMBMapping and disconnecting afterwards? It's not wrong naturally, but it adds some complexity and it's a bit harder to debug this way.
David Domask | Product Management: Principal Analyst
sebkoe
Influencer
Posts: 14
Liked: 1 time
Joined: Aug 20, 2021 11:07 am
Full Name: Sebastian
Contact:

Re: Problem with Powershell Script in SureBackup Job

Post by sebkoe »

Hi,

thank you for your reply.

The Veeam Backup Service is running as "SERVICE_START_NAME : LocalSystem"

I tried starting the Script with PsExec but i am not even able to start it via an elevated cmd prompt, only with an none-elevated cmd prompt it does work.

Elevated:

C:\Windows\system32>powershell.exe -F "C:\SureBackup\FileLock\FileLock.ps1"
Get-Content : Der Pfad "\\10.255.150.113\Monitoring\veeam.txt" kann nicht gefunden werden, da er nicht vorhanden ist.
In C:\SureBackup\FileLock\FileLock.ps1:15 Zeichen:16
+ $content = Get-Content $filePath
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\\10.255.150.113\Monitoring\veeam.txt:String) [Get-Content], ItemNotFou
ndException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Regarding your Script related question. I am not the best Powershell Scripter. Most of the Code was generated by ChatGPT und just tweaked until it worked (in my case sadly just locally, not inside SureBackup).

Maybe i make it too complicated. The only thing i want to achieve is:

- load the backup into SureBackup and see if the VM is starting and reachable via Network (no Problem)
- Check if a File on Drive X on that vm is there, readable and the content of that File is "OK".

BR
Sebastian
sebkoe
Influencer
Posts: 14
Liked: 1 time
Joined: Aug 20, 2021 11:07 am
Full Name: Sebastian
Contact:

Re: Problem with Powershell Script in SureBackup Job

Post by sebkoe »

i went with a completely different approach now und got what i wanted working.

Thank you
david.domask
Veeam Software
Posts: 2113
Liked: 509 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Problem with Powershell Script in SureBackup Job

Post by david.domask »

Hi @sebkoe, glad to hear you came up with an alternative approach. TBH I was afraid to ask if that was ChatGPT related :D ChatGPT gets some really "funky" ideas when it comes to code...

I would have just used New-SMBMapping if you really wanted to do it this way and do a file check, but if you're willing to share your approach, would be great.
David Domask | Product Management: Principal Analyst
sebkoe
Influencer
Posts: 14
Liked: 1 time
Joined: Aug 20, 2021 11:07 am
Full Name: Sebastian
Contact:

Re: Problem with Powershell Script in SureBackup Job

Post by sebkoe »

My approach is definitely not a good one but its working and i am fine with it. I now have a script running on the VM itself. Right after booting it checks if the file is there, readable and if it has the right content. If that is the case it will open a port for 20 Minutes. SureBackup will just check the Port with the connection tester. Port open = everything is fine. Port closed = Problem.
david.domask
Veeam Software
Posts: 2113
Liked: 509 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Problem with Powershell Script in SureBackup Job

Post by david.domask »

Oh it's clever for sure, thanks for sharing!
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests