I have a script to return the list of database backup files on a number of remote database servers.
I use Test-Path to check if the UNC path exists, if it exists it returns the list of files using Get-ChildItem.
When I test this script the Test-Path works at a command prompt but when I run the script in a Scheduled Task it always returns FALSE.
I have tried a number of different ways to use Test-Path but none of them work. The script runs under the same account at the command prompt and for the Schedule task.
The schedule task runs with highest privileges and whether the user is logged on or not.
The script is located on a script server and is called via UNC, it was tested this way at the command prompt and in the scheduled task on a MSSQL Database Server.
if(test-path -path "filesystem::$TargetUNCFolder") {
... Do Something
}
and
$LocalUNCFolderExists = test-path -path $TargetUNCFolder
if($LocalUNCFolderExists -eq $true) {
... Do Something
}
and
if($(try {test-path -path $TargetUNCFolder} catch {$false})) {
... Do Something
}
If I bypass the Test-Path and allow it to drop to the Get-childItem, it returns the number of items correctly but the Foreach does not iterate through the list returned from Get-ChildItem, this again is only if it is ran in a Scheduled task
# Add an underscore to the current database name
$DatabaseName = "$DbName" + "_"
# Get all files with the <Database name> followed by an underscore
$LocalFiles = gci $TargetUNCFolder -Include $DatabaseName* -Recurse -File | Sort CreationTime -Descending | Select fullname, name, Directory, Extension, Length, LastWriteTime
# Get the number of files
$FilesOnLocalServer = @($LocalFiles).count;
# Log the number of files, using the custom log function
Log-Output($LogFile) ("Number of files on server: $FilesOnLocalServer");
# Loop through the files
foreach($Backupfile in $LocalFiles) {
... Do Something
}
Nothing else in the script fails when called from the scheduled task, just anything to do with reading files from the file-system.
Edit Question: The code with just the relevant parts
#For each server instance in the list, check and gather Server, Instancce and Database data
foreach ($Row in $DataSetServerList.Tables[0].Rows) {
# ...
$ServerName = $($Row[1]).Trim() # The current server's name
...
$InstanceName = $($Row[3]).Trim() # The current instances's name
# ... Scan server
$TargetUNCFolder = "\\$ServerName\BackupsFull$\$InstanceName\"
for ($i=0;$i -lt $DataSetServerDatabases.tables.count;$i++){
foreach($Database in $DataSetServerDatabases.Tables[$i].rows) {
#...
$DbName = $($Database[0])
# ...
# Check the the UNC path exists
# *** Always returns false when ran in a scheduled task
if(test-path -path "filesystem::$TargetUNCFolder") {
# Add an underscore to the current database name
$DatabaseName = "$DbName" + "_"
# Get all files with the <Database name> followed by an underscore
# *** Doesn't return a List of files when ran in a scheduled task
$LocalFiles = gci $TargetUNCFolder -Include $DatabaseName* -Recurse -File | Sort CreationTime -Descending | Select fullname, name, Directory, Extension, Length, LastWriteTime
# Get the number of files
$FilesOnLocalServer = @($LocalFiles).count;
# Log the number of files, using the custom log function
Log-Output($LogFile) ("Number of files on server: $FilesOnLocalServer");
# Loop through the files
foreach($Backupfile in $LocalFiles) {
# ... Do Something
}
}
}
}
}
$TargetUNCFolder?. Also I don't see where you populate that variable - Matt