I did everything according to instructions and script works interactively. It makes backup without errors.
Then I putted this script into Windows scheduler. Here are my problems began. Script works only when I run it in scheduler with option "run only when users is logged on". (of course option "run with highest privileges" is ON) When I changed option to "run whether user is logged on or not" script failed. User was the same. Message was:
Backup-SPFarm : You need to have Machine administrator priviliges to run this cmdlet.
At \\serversql\back\task\BackupSharepointFarm.ps1:2 char:14
+ Backup-SPFarm <<<< -Directory \\serversql\back\Backup -BackupMethod full -Verbose
+ CategoryInfo : InvalidData: (Microsoft.Share...mdletBackupFarm:SPCmdletBackupFarm) [Backup-SPFarm], SPException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletBackupFarm
Here is my script BackupSharepointFarm.ps1:
Backup-SPFarm -Directory \\serversql\back\Backup -BackupMethod full -Verbose
The farm has two servers:
1. sharepoint server , 2. sql server
Backup task is of course scheduled on sharepoint server.
Shared disk for backup is on sql server (separate, dedicated disk). All required users have full or write/modify access to disk & share. As I mentioned script works fine when run interactively.
What could be the reason for the error?
Please help me ! I spent plenty of time for testing and searching for clues in the network .
The reason is that Powershell is searching for a Profile when using Powershell. So when it has no profile to load, it will fail.
You can try to load pre-load a profile. So you'll hit a bat file which loads a profile, and then start the powershell script.
Could you please explain me the best way to load profile in batch script? I thought it is enough to run task in scheduler using domain user, with option "run whether user is logged on or not" and it should load user profile. But it doesn't work in my issue. I asked Google, but didn't find any interesting information about loading profile in batch file. Have you got any suggestion?
when running the option "run whether user is logged on or not" is not the solution, because it doesn't load the profile. It's running in another context, so the profile isn't loaded.
You can use the following command as example:
runas /profile /env /user:mydomain\admin "mmc %windir%\system32\dsa.msc"
In my case it didn't untill I logged in to the box as user that sheduled task is run under.
The first time you login windows creates user profile on the machine and I guess that was the case.
After that, as mentioned above:
1. Login to the box at leas once using user from Sheduled Task
2. powershell -Command c:\Scripts\Backup_sharePoint_Full.ps1
3. "Run whether the user is logged or not" and "Do not store password"
4. "Run with highest privileges"
5. RW access to the backup path granted for Central Admin App pool user, SQL Service user, user you run sheduled task under.
Works like a char.