2015-09-28 25 views
0

我正在使用Powershell脚本来计算2个网络路径的每个子目录中文件的数量。当我在ISE Debug中运行它时,我的脚本工作得很好,但是当我尝试运行Powershell时,输出被截断,并且不提供计数。我很确定这是由于我在错误的位置关闭了foreach,但我似乎无法弄清楚这一点。我对Powershell非常陌生。运行PowerShell vs ISE时网络位置,不同输出的PowerShell计数文件

$dirs = "\\myserver\myshare\directory1" , "\\myserver\myshare\directory2" 
$txt = "\\myserver\myshare\output.txt" 

ForEach ($dir in $dirs) 
    { 
    (Get-ChildItem $dirs -recurse -Directory | ForEach-Object{ 
     $props = @{ 
     Folder = $_.FullName 
     Count = (Get-ChildItem -Path $_.Fullname -File | Measure- Object).Count 
       } 
New-Object PSObject -Property $props } | Select-Object Folder , Count) | Format-Table -AutoSize | Out-File $txt 
} 

回答

0

两件事情:

  1. GET-ChildItem应该是$不是$目录显示目录
  2. 您的格式,表格应包含字段名称。
$dirs = "\\myserver\myshare\directory1", "\\myserver\myshare\directory2" 
$txt = "\\myserver\myshare\output.txt" 

ForEach ($dir in $dirs) 
{ 
    (Get-ChildItem $dir -recurse -Directory | ForEach-Object { 
     $props = @{ 
     Folder = $_.FullName 
     Count = (Get-ChildItem -Path $_.Fullname -File | Measure-Object).Count 
       } 
New-Object PSObject -Property $props } | Select-Object Folder, Count) | Format-Table -AutoSize -property Folder, Count | Out-File $txt 
} 

我相信#2是因为格式表是假设你的PS窗口的宽度是最大线宽。如果ISE窗口全屏,则PS窗口的宽度大于独立PS窗口的默认设置。

0

谢谢,我现在有这个工作。我仍然不得不使用Get-ChildItem $ Dirs,通过使用Dir,它只写入了一个到csv文件中的路径。然而,我根据你的建议使用了格式表,但是我将“外部化”改为了“包装”。

$dirs= ""\\myserver\myshare\directory1","\\myserver\myshare\directory2"                           
$txt = "\\myserver\myshare\output.txt" 

ForEach ($dir in $dirs) 
{ 
(Get-ChildItem $dirs -recurse -Directory | ForEach-Object { 
    $props = @{ 
    Folder = $_.FullName 
    Count = (Get-ChildItem -Path $_.Fullname -File | Measure-Object).Count 
      } 
New-Object PSObject -Property $props } | Select-Object Folder, Count) |Format-Table -Wrap -property Folder, Count | Out-File $txt 
} 
相关问题