-1

I am trying to schedule a weekly task that takes a backup of some important data (Eventually, I want to run the PowerShell script from Windows task manager). The software provider already has a batch script for this (backup.bat). I have written a powershell script that invokes this batch script. But invoking backupdb from powershell fails throwing a "Permission denied" error message.

I tried the below, which did not work:

start-process $BackupCmd -verb runas -ArgumentList "$Flags `"$BackupFile`""

After looking at several posts on SO and other forums, I was able to find answers for running a powershell script from inside a batch script as admin and not the other way round.

how to run as admin powershell.ps1 file calling in batch file, Run a powershell script in batch file as administrator and How to run a PowerShell script from a batch file

EDIT 1:

1.I run the batch script and the PowerShell script as the same user.

2.I tried elevating the PowerShell using "-verb runas", but did not work. Running the PowerShell script from the same elevated window as the batch script does not work.

3.Pasting the PowerShell script below:

$CurrentDate = get-date -format yyyyMMdd
$BackupStartDate = (get-date).AddDays(-7).ToString("yyyyMMdd") 
$BackupDir = "<directory path>"
$BackupFile = $BackupDir + "Backup-" + $BackupStartDate + "-to-" + $CurrentDate + ".txt"
$BackupCmd = "C:\Progra~1\bin\backup"
$Verbose = " -v "
$ArchiveStart = " -S " + $BackupStartDate
$Flags = $Verbose + $ArchiveStart

# Both commands below do not work
start-process $BackupCmd -verb runas -ArgumentList "$Flags `"$BackupFile`""
& $BackupCmd $Flags `"$BackupFile`"

4.Error:

backup.bat : Error writing to the debug log! <type 'exceptions.IOError'> [Errno  13] 
Permission denied: 'C:\\Program Files\\tmp\\debug.log'
(2014/06/05 12:42:01.07) [8764] --> Exception encountered.  <Unable to load config file!>
Error writing to the debug log! <type 'exceptions.IOError'> [Errno 13] Permission denied:

Thanks.

6
  • You should try just typing in the name of the batch file. It's amazing! Commented Jun 5, 2014 at 17:34
  • Cole9350, I did not understand your comment, but I did try running the batch script from a cmd prompt as an admin and it works. The script does not work from inside a powershell. Commented Jun 5, 2014 at 17:45
  • .\Batchname.bat, or just specify the full path Commented Jun 5, 2014 at 17:53
  • Are you running the PowerShell script from the same (admin) user as the working batch script? From the same elevated command window? Please post exactly how you're running the Powershell script, as well as the output when it fails. Commented Jun 5, 2014 at 17:57
  • I have edited my question to answer your questions. Commented Jun 5, 2014 at 18:15

1 Answer 1

1

I have encountered problems using start-process with -verb runas on batch scripts.

Try using start-process on powershell instead, passing your batch file as the first argument:

$CurrentDate = get-date -format yyyyMMdd
$BackupStartDate = (get-date).AddDays(-7).ToString("yyyyMMdd") 
$BackupDir = "C:\"
$BackupFile = $BackupDir + "Backup-" + $BackupStartDate + "-to-" + $CurrentDate + ".txt"
$BackupCmd = "C:\Progra~1\bin\backup.bat"
$Verbose = "-v"
$ArchiveStart = "-S $BackupStartDate"
$Flags = "$Verbose $ArchiveStart"
$Args = "$BackupCmd $Flags `"$BackupFile`""

start-process powershell -verb runas -ArgumentList $Args
Sign up to request clarification or add additional context in comments.

4 Comments

It works for me. What errors are you seeing? Note that in the code you provided, you have $BackupDir = "<directory path>" which isn't going to work properly. I'm not sure if you changed that specifically for your post or not.
Yes, I changed it specifically for the post. Here is the error: Start-Process : Parameter set cannot be resolved using the specified named parameters. At Backup.ps1:20 char:1 + start-process -NoNewWindow powershell -Verb runas -ArgumentList $Args + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.StartProcessCommand
I also tried what has been suggested here, but did not work -- social.technet.microsoft.com/Forums/windowsserver/en-US/….
If you look at the documentation for Start-Process (technet.microsoft.com/en-us/library/hh849848.aspx) you will see that there are 2 parameter sets. You can't combine -verb with -NoNewWindow.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.