1

I am working on a sharepoint on-premises farm 2016, and i created a scheduler task to do a backup for the whole farm + each site collection separately.

here is the action inside the task scheduler:-

enter image description here

here is the backup.bat file:-

"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" -Command Start-Process "$PSHOME\powershell.exe" -Verb RunAs -ArgumentList "'%~dp0\backup.ps1'"

and the backup.ps1, is as follow:-

Backup-SPFarm -Directory  \\***\Back -BackupMethod full -BackupThreads 10 -Force
Backup-spsite -identity http://***/  -path c:\Back\main.bak –force
Backup-spsite -identity http://***/sites/Info   -path c:\Back\info.bak –force
Backup-spsite -identity http://***/offices/  -path c:\Back\offices.bak –force

now i have noted that the task will only create 3 .bak files representing the site collection backups, but the first command for backuping the farm will not generate any files/folder, and when i tried to run this command manually i got this error:-

PS C:\batchfiles> C:\batchfiles\backup.ps1
Backup-SPFarm : You need to have Machine administrator priviliges to run this cmdlet.

although i am defining to run the scheduler task with highest privileges:- enter image description here

so can anyone advice on this?

1 Answer 1

0

Try adding this code to the beginning of your script:

It will elevate if you are not exectuing as admin:

# The following section was taken from http://blogs.msdn.com/b/virtual_pc_guy/archive/2010/09/23/a-self-elevating-powershell-script.aspx
# This section is used to ensure we are running the script "As Administrator" so we don't get access denied errors when we run 
# commands that require administrator permissions.
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
   # We are running "as Administrator" - so change the title and background color to indicate this
   $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
   $Host.UI.RawUI.BackgroundColor = "DarkBlue"
   clear-host
}
else
{
    # We are not running "as Administrator" - so relaunch as administrator
    # Create a new process object that starts PowerShell
    $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
    # Specify the current script path and name as a parameter
    $newProcess.Arguments = $myInvocation.MyCommand.Definition;
    # Indicate that the process should be elevated
    $newProcess.Verb = "runas";
    # Start the new process
    [System.Diagnostics.Process]::Start($newProcess);
    # Exit from the current, unelevated, process
    exit
}
##########################################################################################################################################

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.