2016-08-18 140 views
1

我有一項任務,每天在WS2012R2上執行3次以獲取磁盤大小,文件和文件夾的總數,包括來自遠程服務器上文件夾的子目錄。獲取文件夾中的文件和子文件夾的總數

enter image description here

我已經嘗試過PowerShell腳本:

目前我通過RDP'ing爲目標,導航到該文件夾​​並右擊該文件夾複製的信息獲取此信息

Get-ChildItem E:\Data -Recurse -File | Measure-Object | %{$_.Count} 

和其他PowerShell腳本。

哪些產生了無數的錯誤有關沒有權限的一些子目錄或只是給結果我不希望這樣。

我已經試過VBscript,但VBscript根本無法得到這些信息。

+0

獲取文件和文件夾總數的目的是什麼? – Eilidh

回答

1

您可以直接訪問count屬性:

$items = Get-ChildItem E:\Data -Recurse -File 
($items | Where { -not $_.PSIsContainer}).Count #files 
($items | Where $_.PSIsContainer).Count #folders 
+0

給出錯誤:Get-ChildItem:指定的路徑,文件名或兩者都太長。全限定文件名必須小於 260個字符,並且目錄名稱必須少於248個字符。 在線:1 char:2 +(Get-ChildItem E:\\ Folder \ -Recurse -File).Count + ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~類別:ReadError:(E:\ Folder \ String)[Get-ChildItem],PathTooLongExce ption + FullyQualifiedErrorId:DirIOError,Microsoft.PowerShell .Commands.GetChildItemCommand – Daniel

+0

@MartinBrandl,考慮使用計數器變量,因爲任何大型文件結構都會填充內存。當然,爲任何大型文件結構做這件事是令人震驚的... –

+0

事情我很困惑是你可以在常規選項卡右擊文件夾時看到此信息,爲什麼在Powershell中存在問題?我是否需要使用另一種語言? – Daniel

0

到目前爲止總結的意見。

你可以得到的C盤與大小:

$Drive = "C" 
    Get-PSDrive -Name $Drive | Select-Object @{N="Used Space on $Drive Drive (GB)"; E={[Math]::Round($_.Used/1GB)}} 

但你不能使用類似下面的代碼來算的文件爲整個C盤&文件夾,但它可以用於簡單filestructures和簡單的驅動器。 我使用代碼來計算我的其他驅動器和我的homepath,只需將$ Path更改爲例如「$ Env:Homepath」。

There's the path length problem which is a .NET thing and won't be fixed until everyone (and PowerShell) is using .NET 4.6.2. Then there's that you're acting as you counting it, not the operating system counting.

[Int]$NumFolders = 0 
[Int]$NumFiles = 0 
$Path = "C:\" 
$Objects = Get-ChildItem $Path -Recurse 

$Size = Get-ChildItem $Path -Recurse | Measure-Object -property length -sum 
$NumFiles = ($Objects | Where { -not $_.PSIsContainer}).Count 
$NumFolders = ($Objects | Where {$_.PSIsContainer}).Count 

$Summary = New-Object PSCustomObject -Property @{"Path" = $Path ; "Files" = $NumFiles ; "Folders" = $NumFolders ; "Size in MB" = ([Math]::Round($Size.sum /1MB))} 
$Summary | Format-list 

在遠程計算機上運行此,我建議使用新的PSSession到計算機/計算機,然後調用命令來運行使用新的會話的代碼。

相關問題