Advertisement
PtiTom

SharePoint Migration - Analyse arborescence V2

Jun 8th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-FolderItem {
  2.     <#
  3.         .SYNOPSIS
  4.             Lists all files under a specified folder regardless of character limitation on path depth.
  5.  
  6.         .DESCRIPTION
  7.             Lists all files under a specified folder regardless of character limitation on path depth.
  8.  
  9.         .PARAMETER Path
  10.             The type name to list out available constructors and parameters
  11.  
  12.         .PARAMETER Filter
  13.             Optional parameter to specify a specific file or file type. Wildcards (*) allowed.
  14.             Default is '*.*'
  15.        
  16.         .PARAMETER ExcludeFile
  17.             Exclude Files matching given names/paths/wildcards
  18.  
  19.         .PARAMETER MaxAge
  20.             Exclude files older than n days.
  21.  
  22.         .PARAMETER MinAge
  23.             Exclude files newer than n days.    
  24.  
  25.         .EXAMPLE
  26.             Get-FolderItem -Path "C:\users\Administrator\Desktop\PowerShell Scripts"
  27.  
  28.             LastWriteTime : 4/25/2012 12:08:06 PM
  29.             FullName      : C:\users\Administrator\Desktop\PowerShell Scripts\3_LevelDeep_ACL.ps1
  30.             Name          : 3_LevelDeep_ACL.ps1
  31.             ParentFolder  : C:\users\Administrator\Desktop\PowerShell Scripts
  32.             Length        : 4958
  33.  
  34.             LastWriteTime : 5/29/2012 6:30:18 PM
  35.             FullName      : C:\users\Administrator\Desktop\PowerShell Scripts\AccountAdded.ps1
  36.             Name          : AccountAdded.ps1
  37.             ParentFolder  : C:\users\Administrator\Desktop\PowerShell Scripts
  38.             Length        : 760
  39.  
  40.             LastWriteTime : 4/24/2012 5:48:57 PM
  41.             FullName      : C:\users\Administrator\Desktop\PowerShell Scripts\AccountCreate.ps1
  42.             Name          : AccountCreate.ps1
  43.             ParentFolder  : C:\users\Administrator\Desktop\PowerShell Scripts
  44.             Length        : 52812
  45.  
  46.             Description
  47.             -----------
  48.             Returns all files under the PowerShell Scripts folder.
  49.  
  50.         .EXAMPLE
  51.             $files = Get-ChildItem | Get-FolderItem
  52.             $files | Group-Object ParentFolder | Select Count,Name
  53.  
  54.             Count Name
  55.             ----- ----
  56.                95 C:\users\Administrator\Desktop\2012 12 06 SysInt
  57.                15 C:\users\Administrator\Desktop\DataMove
  58.                 5 C:\users\Administrator\Desktop\HTMLReportsinPowerShell
  59.                31 C:\users\Administrator\Desktop\PoshPAIG_2_0_1
  60.                30 C:\users\Administrator\Desktop\PoshPAIG_2_1_3
  61.                67 C:\users\Administrator\Desktop\PoshWSUS_2_1
  62.               437 C:\users\Administrator\Desktop\PowerShell Scripts
  63.                 9 C:\users\Administrator\Desktop\PowerShell Widgets
  64.                92 C:\users\Administrator\Desktop\Working
  65.  
  66.             Description
  67.             -----------
  68.             This example shows Get-FolderItem taking pipeline input from Get-ChildItem and then saves
  69.             the output to $files. Group-Object is used with $Files to show the count of files in each
  70.             folder from where the command was executed.
  71.  
  72.         .EXAMPLE
  73.             Get-FolderItem -Path $Pwd -MinAge 45
  74.  
  75.             LastWriteTime : 4/25/2012 12:08:06 PM
  76.             FullName      : C:\users\Administrator\Desktop\PowerShell Scripts\3_LevelDeep_ACL.ps1
  77.             Name          : 3_LevelDeep_ACL.ps1
  78.             ParentFolder  : C:\users\Administrator\Desktop\PowerShell Scripts
  79.             Length        : 4958
  80.  
  81.             LastWriteTime : 5/29/2012 6:30:18 PM
  82.             FullName      : C:\users\Administrator\Desktop\PowerShell Scripts\AccountAdded.ps1
  83.             Name          : AccountAdded.ps1
  84.             ParentFolder  : C:\users\Administrator\Desktop\PowerShell Scripts
  85.             Length        : 760
  86.  
  87.             Description
  88.             -----------
  89.             Gets files that have a LastWriteTime of greater than 45 days.
  90.  
  91.         .INPUTS
  92.             System.String
  93.        
  94.         .OUTPUTS
  95.             System.IO.RobocopyDirectoryInfo
  96.  
  97.         .NOTES
  98.             Name: Get-FolderItem
  99.             Author: Boe Prox
  100.             Date Created: 31 March 2013
  101.             Version History:
  102.             Version 1.5 - 09 Jan 2014
  103.                 -Fixed bug in ExcludeFile parameter; would only work on one file exclusion and not multiple
  104.                 -Allowed for better streaming of output by Invoke-Expression to call the command vs. invoking
  105.                 a scriptblock and waiting for that to complete before display output.  
  106.             Version 1.4 - 27 Dec 2013
  107.                 -Added FullPathLength property          
  108.             Version 1.3 - 08 Nov 2013
  109.                 -Added ExcludeFile parameter
  110.             Version 1.2 - 29 July 2013
  111.                 -Added Filter parameter for files
  112.                 -Fixed bug with ParentFolder property
  113.                 -Added default value for Path parameter            
  114.             Version 1.1
  115.                 -Added ability to calculate file hashes
  116.             Version 1.0
  117.                 -Initial Creation
  118.     #>
  119.     [cmdletbinding(DefaultParameterSetName='Filter')]
  120.     Param (
  121.         [parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
  122.         [Alias('FullName')]
  123.         [string[]]$Path = $PWD,
  124.         [parameter(ParameterSetName='Filter')]
  125.         [string[]]$Filter = '*.*',    
  126.         [parameter(ParameterSetName='Exclude')]
  127.         [string[]]$ExcludeFile,              
  128.         [parameter()]
  129.         [int]$MaxAge,
  130.         [parameter()]
  131.         [int]$MinAge
  132.     )
  133.     Begin {
  134.         $params = New-Object System.Collections.Arraylist
  135.         $params.AddRange(@("/L","/S","/NJH","/BYTES","/FP","/NC","/NDL","/TS","/XJ","/R:0","/W:0"))
  136.         If ($PSBoundParameters['MaxAge']) {
  137.             $params.Add("/MaxAge:$MaxAge") | Out-Null
  138.         }
  139.         If ($PSBoundParameters['MinAge']) {
  140.             $params.Add("/MinAge:$MinAge") | Out-Null
  141.         }
  142.     }
  143.     Process {
  144.         ForEach ($item in $Path) {
  145.             Try {
  146.                 $item = (Resolve-Path -LiteralPath $item -ErrorAction Stop).ProviderPath
  147.                 If (-Not (Test-Path -LiteralPath $item -Type Container -ErrorAction Stop)) {
  148.                     Write-Warning ("{0} is not a directory and will be skipped" -f $item)
  149.                     Return
  150.                 }
  151.                 If ($PSBoundParameters['ExcludeFile']) {
  152.                     $Script = "robocopy `"$item`" NULL $Filter $params /XF $($ExcludeFile  -join ',')"
  153.                 } Else {
  154.                     $Script = "robocopy `"$item`" NULL $Filter $params"
  155.                 }
  156.                 Write-Verbose ("Scanning {0}" -f $item)
  157.                 Invoke-Expression $Script | ForEach {
  158.                     Try {
  159.                         If ($_.Trim() -match "^(?<Size>\d+)\s(?<Date>\S+\s\S+)\s+(?<FullName>.*)") {
  160.                            $object = New-Object PSObject -Property @{
  161.                                 ParentFolder = $matches.fullname -replace '(.*\\).*','$1'
  162.                                 FullName = $matches.FullName
  163.                                 Name = $matches.fullname -replace '.*\\(.*)','$1'
  164.                                 Length = [int64]$matches.Size
  165.                                 LastWriteTime = [datetime]$matches.Date
  166.                                 Extension = $matches.fullname -replace '.*\.(.*)','$1'
  167.                                 FullPathLength = [int] $matches.FullName.Length
  168.                             }
  169.                             $object.pstypenames.insert(0,'System.IO.RobocopyDirectoryInfo')
  170.                             Write-Output $object
  171.                         } Else {
  172.                             Write-Verbose ("Not matched: {0}" -f $_)
  173.                         }
  174.                     } Catch {
  175.                         Write-Warning ("{0}" -f $_.Exception.Message)
  176.                         Return
  177.                     }
  178.                 }
  179.             } Catch {
  180.                 Write-Warning ("{0}" -f $_.Exception.Message)
  181.                 Return
  182.             }
  183.         }
  184.     }
  185. }
  186.  
  187. Get-FolderItem -Path 'C:\OneDrive Entreprise' |`
  188. foreach{
  189. $Item = $_.Name
  190. $Type = $_.Extension
  191. $Path = $_.FullName
  192. $Folder = $False #$_.ParentFolder
  193. $Size = $_.Length
  194.  
  195. $Path | Select-Object `
  196.     @{n="Name";e={$Item}},`
  197.     @{n="Size";e={$Size}},`
  198.     @{n="filePath";e={$Path}},`
  199.     @{n="Extension";e={if($Folder){"Folder"}else{$Type}}}`
  200. }| Export-Csv 'C:\Detail.csv' -NoTypeInformation -Encoding UTF8 -Delimiter ';'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement