powershell - test-path not working when in a scheduled task -
i have script return list of database backup files on number of remote database servers.
i use test-path
check if unc path exists, if exists returns list of files using get-childitem
.
when test script test-path works @ command prompt when run script in scheduled task returns false.
i have tried number of different ways use test-path none of them work. script runs under same account @ command prompt , schedule task.
the schedule task runs highest privileges , whether user logged on or not.
the script located on script server , called via unc, tested way @ command prompt , in scheduled task on mssql database server.
if(test-path -path "filesystem::$targetuncfolder") { ... }
and
$localuncfolderexists = test-path -path $targetuncfolder if($localuncfolderexists -eq $true) { ... }
and
if($(try {test-path -path $targetuncfolder} catch {$false})) { ... }
if bypass test-path , allow drop get-childitem, returns number of items correctly foreach not iterate through list returned get-childitem, again if ran in scheduled task
# add underscore current database name $databasename = "$dbname" + "_" # files <database name> followed underscore $localfiles = gci $targetuncfolder -include $databasename* -recurse -file | sort creationtime -descending | select fullname, name, directory, extension, length, lastwritetime # number of files $filesonlocalserver = @($localfiles).count; # log number of files, using custom log function log-output($logfile) ("number of files on server: $filesonlocalserver"); # loop through files foreach($backupfile in $localfiles) { ... }
nothing else in script fails when called scheduled task, reading files file-system.
edit question: code relevant parts
#for each server instance in list, check , gather server, instancce , database data foreach ($row in $datasetserverlist.tables[0].rows) { # ... $servername = $($row[1]).trim() # current server's name ... $instancename = $($row[3]).trim() # current instances's name # ... scan server $targetuncfolder = "\\$servername\backupsfull$\$instancename\" ($i=0;$i -lt $datasetserverdatabases.tables.count;$i++){ foreach($database in $datasetserverdatabases.tables[$i].rows) { #... $dbname = $($database[0]) # ... # check the unc path exists # *** returns false when ran in scheduled task if(test-path -path "filesystem::$targetuncfolder") { # add underscore current database name $databasename = "$dbname" + "_" # files <database name> followed underscore # *** doesn't return list of files when ran in scheduled task $localfiles = gci $targetuncfolder -include $databasename* -recurse -file | sort creationtime -descending | select fullname, name, directory, extension, length, lastwritetime # number of files $filesonlocalserver = @($localfiles).count; # log number of files, using custom log function log-output($logfile) ("number of files on server: $filesonlocalserver"); # loop through files foreach($backupfile in $localfiles) { # ... } } } } }
to working had use local path, not unc path script, don't know why fails when called via unc, test-path , gci cmdlet
Comments
Post a Comment