BITS do not work in windows scheduled task

by Tadziz at 2013-02-14 01:22:11

Hi again,
when i run code from powershell ise it done everything as i want it to do, backup database and copy it to specified directories, but when i create a scheduled task, it only backup my database but dont execute "Start-BitsTransfer"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

Import-Module BitsTransfer
#------- MS SQL BACKUP -------###
#get date for backup name
$dt = get-date -format yyyyMMdd-HHmmss

$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "pvt-2012"

#path where to backup database
$path = 'C:\Install'

$dbs = $s.Databases

foreach ($db in $dbs) {

if(@("master", "model") -contains $db.name) {

$dbname = $db.Name
#Create a Backup object instance with the Microsoft.SqlServer.Management.Smo.Backup namespace
$dbBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup")

$dbBackup.Database = $dbname

#database name to copy
$copyDB = ($dbname + "" + $dt + ".bak")

#diff backups
#$smoBackup.Incremental = $true

#Specify the Action property to generate a FULL backup ( database, file, log )
$dbBackup.Action="DATABASE"

#Enable Backup type "COPY ONly"
#$dbBackup.CopyOnly = $TRUE

#Need to TEST
#$dbBackup.RetainDays = "7"

#Enable MS SQL Compression set to 1 disable 0
$dbBackup.CompressionOption = "1"

#Add the backup file to the Devices collection and specify File as the backup type
$dbBackup.Devices.AddDevice($path + $dbname + "
" + $dt + ".bak", "File")

#Call the SqlBackup method to generate the backup
$dbBackup.SqlBackup($s)
}
}
#get new backup file name
$extension = "*.bak"
$file = Get-ChildItem $path -Include $extension -Recurse | sort LastWriteTime | select Name |Select-Object -Last 2
$file = $file -replace "@{Name=", ""
$file = $file -replace "}", ""
#Write-Output $file

#specify where to copy backups
$dirs = '\tan01\share', "\tan01\share\eiger"



foreach ($files in $file) {

foreach($dir in $dirs) {

#This part does not work in Windows Scheduled TAsk . Need advice
Start-BitsTransfer -Source $path$files -Destination $dir -Asynchronous -TransferType Upload
}




}
by Tadziz at 2013-02-14 02:15:41
I found a solution in this forum post http://www.powershellcommunity.org/Forums/tabid/54/aft/5158/Default.aspx

[quote]Took me ages to find a solution. The answer is to run the scheduled task as a user that is ALWAYS logged on, eg one of the built-in security principals:
LOCAL SERVICE
NETWORK SERVICE
SYSTEM
[/quote]

Worked :slight_smile:
by Tadziz at 2013-02-14 06:42:12
Not, solved. maybe anyone knows how to run bitstransfer not under system account in scheduled tasks ?
by MattG at 2013-02-14 18:16:41
I would wager that the reason it’s not working for you running as System is because the '\tan01\share', "\tan01\share\eiger" shares won’t be mapped in the System context. Try changing the output directories to local directories and if that works then that’s your issue. Also, if you run psexec to get system, you’ll find that those shares won’t be mapped.
psexec -s -i cmd.exe
net use
by Tadziz at 2013-02-16 03:47:12
Hi, tell me if it’s possible to backup to remote dir using new-psdrive and set-location to new psdrive, and when run a backup on system account to maped dir ?

Thanks.
Tadas
by magicrohn at 2013-02-17 13:49:49
Are these computers part of a domain? If so, have you tried giving the computer account of the machine running the scheduled task write access to the network share? This should give processes running as SYSTEM or NETWORK SERVICE on that machine the ability to write to the share.
by Tadziz at 2013-02-18 00:23:04
Yes, all computers are in the same domain. I will try your suggestion.