简体   繁体   English

powershell:在循环内调用时命令不起作用

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

The following command does work in a powershell console 以下命令在Powershell控制台中有效

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

(Restore-SvnRepository is a command that comes with visualsvn , it expects a path or unc to a file to be restored as parameter) (还原- SvnRepository是带有一个命令的VisualSVN ,它期望被恢复作为参数到文件路径或UNC)

As I need to execute this command for a large number of files (>500) I embedded it inside a powershell loop but then it doesn't work 因为我需要对大量文件(> 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');

I don't understand why this does not work. 我不明白为什么这行不通。 The loop and filename looks good but when executed, each command throws the following error message 循环和文件名看起来不错,但是在执行时,每个命令都会引发以下错误消息

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)

Could you help me? 你可以帮帮我吗?

EDIT 编辑

It looks like that I was confused by Get-ChildItem that return a System.IO.FileSystemInfo rather than a string. 看起来我对Get-ChildItem感到困惑,因为它返回System.IO.FileSystemInfo而不是字符串。
I didn't notice because of a implicit call to the ToString() when writing to the console that made me think I was dealing with string (rather than FSI) 我没有注意到,因为在写入控制台时隐式调用ToString(),使我认为我正在处理字符串(而不是FSI)

The following code works 以下代码有效

$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 isn't a string, it's an object containing file data. $file不是字符串,而是包含文件数据的对象。

You can simplify your code as follows: 您可以按如下方式简化代码:

$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.

相关问题 Powershell命令输入CMD时不起作用 - Powershell command does not work when inputed into CMD PowerShell - 从 Invoke-Command 内部调用时,ShouldProcess 不起作用 - PowerShell - ShouldProcess not working when called from inside Invoke-Command 如何使此powershell命令在循环内批量工作? - How to make this powershell command work inside a loop in batch? Powershell 命令手动工作,但在 Jenkins 中执行时不起作用 - Powershell command works manually, but does not work when executed in Jenkins 从VBA调用时,PowerShell命令不起作用,但其他方式有效 - PowerShell command doesn't work when called from VBA, but otherwise works 当由vba调用时,powershell -replace无法正常工作 - powershell -replace wont work when called by vba Powershell Copy-item命令在脚本中不起作用,尽管在从命令行运行时确实起作用 - Powershell Copy-item command not working in script although it does work when run from a command line 为什么Powershell Start-Process在从go(golang)调用时不起作用? - Why does Powershell Start-Process not work when called from go (golang)? 为什么命令在 Windows CMD 中工作而不是在 PowerShell 中工作? - Why does command work in Windows CMD and not PowerShell? 通过 Invoke-Command 调用命令时 PowerShell 不一致 - Inconsistent PowerShell when command is called via Invoke-Command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM