2016-02-05 45 views
0

我有一个脚本来返回多个远程数据库服务器上的数据库备份文件列表。在计划任务中测试路径不工作

我使用Test-Path来检查UNC路径是否存在,如果存在,它会使用Get-ChildItem返回文件列表。

当我测试此脚本时,Test-Path在命令提示符下工作,但是当我在计划任务中运行脚本时,它总是返回FALSE。

我已经尝试了很多不同的方法来使用测试路径,但没有一个工作。 该脚本在命令提示符下和计划任务下以相同的帐户运行。

计划任务以最高特权运行并且用户是否登录。

脚本位于脚本服务器上并通过UNC调用,它在命令提示符处和MSSQL数据库服务器上的计划任务中以此方式进行了测试。

if(test-path -path "filesystem::$TargetUNCFolder") { 
    ... Do Something 
} 

$LocalUNCFolderExists = test-path -path $TargetUNCFolder 
if($LocalUNCFolderExists -eq $true) { 
    ... Do Something 
} 

if($(try {test-path -path $TargetUNCFolder} catch {$false})) { 
    ... Do Something 
} 

如果我绕过测试的路径,并允许它拖放到Get-childItem,它正确返回的项目数,但Foreach不会遍历从Get-ChildItem返回的列表,只有当它在预定任务中运行时才会迭代。

# 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 
} 

当从计划任务调用脚本时,没有其他任何操作失败,只是从文件系统读取文件时发生任何操作。

编辑问: 只有相关部分的代码

#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 

       }     
      } 
     } 
    } 
} 
+2

是否在任务运行的用户可以访问'$ TargetUNCFolder'?我也看不到你在哪里填充该变量 – Matt

+0

问题 - 在什么用户下运行的任务?猜测用户帐户没有权限来访问该文件夹。 –

+0

嗨,我已经包含了$ TargetUNCFolder的构建代码,当从命令提示符调用脚本并且它可以访问UNC路径时,它是同一个用户。 – Shane

回答

0

得到这个工作,我不得不使用脚本本地路径,而不是UNC路径,但不知道为什么失败当通过UNC调用时,仅用于测试路径和GCI cmdlet

0

刚碰到这个。相反,测试的路径我用

[System.IO.File]::Exists('\\srv\c$\target_file')