-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.

Community
  • 1
  • 1
Sarvavyapi
  • 810
  • 3
  • 23
  • 35

1 Answers1

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
Kevin Richardson
  • 3,592
  • 22
  • 14
  • It works for me. What errors are you seeing? Note that in the code you provided, you have `$BackupDir = ""` which isn't going to work properly. I'm not sure if you changed that specifically for your post or not. – Kevin Richardson Jun 06 '14 at 23:04
  • 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 – Sarvavyapi Jun 06 '14 at 23:36
  • I also tried what has been suggested here, but did not work -- http://social.technet.microsoft.com/Forums/windowsserver/en-US/132e170f-e3e8-4178-9454-e37bfccd39ea/startprocess-verb-runas-credential. – Sarvavyapi Jun 07 '14 at 00:02
  • If you look at the documentation for Start-Process (http://technet.microsoft.com/en-us/library/hh849848.aspx) you will see that there are 2 parameter sets. You can't combine `-verb` with `-NoNewWindow`. – Kevin Richardson Jun 07 '14 at 17:41