簡體   English   中英

powershell:在循環內調用時命令不起作用

[英]powershell : command does not work when called inside a loop

以下命令在Powershell控制台中有效

Restore-SvnRepository D:\temp\Backup\foo.vsvnbak

(還原- SvnRepository是帶有一個命令的VisualSVN ,它期望被恢復作為參數到文件路徑或UNC)

因為我需要對大量文件(> 500)執行此命令,所以我將其嵌入到powershell循環中,但是它不起作用

$fileDirectory = "D:\temp\Backup"
$files = Get-ChildItem $fileDirectory -Filter "*.vsvnbak"

foreach($file in Get-ChildItem $fileDirectory)
{
    $filePath = $fileDirectory + "\" + $file;

    # escape string for spaces
    $fichier =  $('"' + $filepath + '"')    

    # write progress status
    "processing file " + $fichier 

    # command call
    Restore-SvnRepository $fichier
}

Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');

我不明白為什么這行不通。 循環和文件名看起來不錯,但是在執行時,每個命令都會引發以下錯誤消息

Restore-SvnRepository : Parameter 'BackupPath' should be an absolute or UNC path to the repository
backup file you would like to restore: Invalid method Parameter(s) (0x8004102F)

你可以幫幫我嗎?

編輯

看起來我對Get-ChildItem感到困惑,因為它返回System.IO.FileSystemInfo而不是字符串。
我沒有注意到,因為在寫入控制台時隱式調用ToString(),使我認為我正在處理字符串(而不是FSI)

以下代碼有效

$fileDirectory = "D:\temp\Backup\"
$files = Get-ChildItem $fileDirectory -Filter "*.vsvnbak"

    foreach($file in $files) 
    {
        # $file is an instance of System.IO.FileSystemInfo, 
        # which contains a FullName property that provides the full path to the file. 
        $filePath = $file.FullName

         Restore-SvnRepository -BackupPath $filePath
    }

$file不是字符串,而是包含文件數據的對象。

您可以按如下方式簡化代碼:

$fileDirectory = "D:\temp\Backup"
$files = Get-ChildItem $fileDirectory -Filter "*.vsvnbak"

foreach($file in $files) 
{
    # $file is an instance of System.IO.FileSystemInfo, 
    # which contains a FullName property that provides the full path to the file. 
    $filePath = $file.FullName 

    # ... your code here ...

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM