循环通过不同的网络共享路径

时间:2014-03-31 08:59:25

标签: powershell

我是PowerShell脚本的新手。我的要求是动态更改Get-ChildItem命令中的文件路径。文件路径正在从文本文件中读取。路径存储如下:

\\bmcelb\Abbott_GIS_DBA
\\bmcelb\Abbott_GIS_MAR

一旦我读完所有目录'和子目录'名称,所有者,修改详细信息等。我必须将其导出为CSV文件,该文件应使用相同的路径名保存。

我目前的脚本如下:

Get-ChildItem -Path Z:\ -Filter *.* -Recurse | Select-Object FullName, CreationTime, @{Name="Size";Expression={$_.Length}}, @{Name="ModifiedDate";Expression={$_.LastWriteTime} | Export-Csv D:\scans.csv'

我需要将Z:\替换为我从文本文件中读取的名称,以及使用当前共享路径的CSV文件(从文本文件中读取)。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

您必须阅读包含路径的文件,然后为每个执行您的scriptblock。 这将是:

$dirs=get-content c:\path\to\filepath.txt

$dirs | foreach {
    #do your stuff, the current path is accessible trough $_ variable
    get-ChildItem -Path $_ -Filter *.* -Recurse .....
} 

答案 1 :(得分:0)

这样的事情应该做你想做的事情:

$infile = 'C:\path\to\filepath.txt'
$outdir = 'D:\'

foreach ($dir in (Get-Content $infile)) {
  $outfile = Join-Path $outdir ($dir -split '\\')[-1]
  Get-ChildItem -Path $dir -Filter *.* -Recurse | select ... |
    Export-Csv $outfile -NoType
}