powershell:在循环内调用时,命令不起作用
以下命令在powershell控制台中不起作用powershell:在循环内调用时,命令不起作用,powershell,Powershell,以下命令在powershell控制台中不起作用 Restore-SvnRepository D:\temp\Backup\foo.vsvnbak (Restore SvnRepository是随附的命令,它期望将文件的路径或unc还原为参数) 因为我需要对大量文件(>500)执行此命令,所以我将其嵌入到powershell循环中,但它不起作用 $fileDirectory = "D:\temp\Backup" $files = Get-ChildItem $fileDirectory -Fil
Restore-SvnRepository D:\temp\Backup\foo.vsvnbak
(Restore SvnRepository是随附的命令,它期望将文件的路径或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 ...
}
$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 ...
}
为了确认,您正在提供文件c/o$fichier的完整路径?在工作示例中,您没有向字符串添加
“
”-不要在循环中这样做。根据错误消息,您是否检查了路径以确保它们是绝对路径或UNC路径?如果在映射到计算机上驱动器号的网络共享上的目录中循环,则该路径不是绝对路径或UNC路径。如果是网络共享,则应使用\\ServerAddress\path\To\File这样的路径访问它。请确认,您提供的是文件c/o$fichier的完整路径?在工作示例中,您没有将“
添加到字符串中-根据错误消息,不要在循环中这样做,您是否检查了路径以确保它们是绝对路径或UNC路径?如果在映射到计算机上驱动器号的网络共享上的目录中循环,则该路径不是绝对路径或UNC路径。如果是网络共享,则应使用\\ServerAddress\path\To\File这样的路径访问。为什么“命令”**“处理文件”+$fichier**在控制台中显示预期字符串?为什么“命令”**“处理文件”+$fichier**在控制台中显示预期字符串?