【问题标题】:Looping through the different network share paths循环通过不同的网络共享路径
【发布时间】:2014-03-31 21:37:20
【问题描述】:

我是 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 文件替换为当前共享路径(从文本文件中读取)。我该怎么做?

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

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

    【讨论】:

    • foreach ($dir in $dirs) {...} 会是更好的方法,因为路径必须同时提供给Get-ChildItemExport-Csv
    • 如何将输入文件中的共享路径提供给Export-Csv cmdlet?
    • @AnsgarWiechers 和 OP 一样:... | Select-Object $_.FullName, $_.CreationTime, @{Name="Size";Expression={$_.Length}}, @{Name="ModifiedDate";Expression={$_.LastWriteTime} | Export-Csv D:\scans.csv'
    • 有没有办法用我的 sharepath 的最后一部分保存文件。例如,如果我的路径像 "\\ctsintbmcelb\Abbott_GIS_DBA" 我可以用名称 Abbott_GIS_DBA.csv 保存 csv 文件。我打算将所有 csv 文件保存在具有上述命名概念的同一文件夹中
    • 我不明白你想要什么
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-05-13
      • 1970-01-01
      • 2017-05-26
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      相关资源
      最近更新 更多