Private/Common.ps1
function Get-LastCreatedDir { ############################################################################## #.SYNOPSIS # Gets last created directory. # # #.DESCRIPTION # Gets last created directory in existing directory # #.PARAMETER Directory # Root directory used for searching (absolute path) # #.EXAMPLE # $directory = "C:\root" # $lastCreatedDir = Get-LastCreatedDir -Directory $directory ############################################################################## [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [ValidateScript({ if (Test-Path $_) { $true } else { Throw "The $_ path is not valid. Please, provide a valid value." }})] [string]$Directory ) Write-Verbose "Getting last created directory in '$Directory'" $lastCreatedDir = Get-ChildItem -Path $Directory | Where-Object {$_.PSIsContainer} | Sort-Object CreationTime -Descending | Select-Object -First 1 | Select-Object -ExpandProperty FullName Write-Verbose "Last created directory in '$Directory' is '$lastCreatedDir'" return $lastCreatedDir } function New-UniqueDir { ############################################################################## #.SYNOPSIS # Creates unique directory in existing directory. # # #.DESCRIPTION # Use this function to create a unique directory based on current DateTime. # Unique folder name format: yyyy-MM-dd_HH-mm # #.PARAMETER Directory # Root directory used for creating the unique directory (absolute path) # #.EXAMPLE # $directory = "C:\root" # $uniqueDir = New-UniqueDir -Directory $directory ############################################################################## [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [ValidateScript({ if (Test-Path $_) { $true } else { Throw "The $_ path is not valid. Please, provide a valid value." }})] [string]$Directory ) Write-Verbose "Creating unique directory in '$Directory'" $uniqueFolderName = get-date -format "yyyy-MM-dd_HH-mm" $count = 1 $uniqueDirPath = Join-Path $Directory $uniqueFolderName while(Test-Path $uniqueDirPath) { $count++ $unique = $uniqueFolderName + "(" + $count + ")" $uniqueDirPath = Join-Path $Directory $unique } $uniqueDir = New-Item -Path $uniqueDirPath -ItemType Directory Write-Verbose "Created unique directory: $uniqueDir" return $uniqueDir } function Remove-Dir { ############################################################################## #.SYNOPSIS # Removes Sitecore Deploymeny PowerShell log directory in existing directory. # #.DESCRIPTION # Removes Sitecore Deploymeny PowerShell log directory in existing directory # Log directory name: _SitecoreDeploymentPS # #.PARAMETER Directory # Root directory used for removing a log directory (absolute path) # #.EXAMPLE # $directory = "C:\root" # Remove-Dir -Directory $directory ############################################################################## [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [string]$Directory ) if ([String]::IsNullOrWhiteSpace($Directory)) { return } if (Test-Path $Directory) { #TODO handle locked items Remove-Item -Path $Directory -Force -Recurse } } function New-EmptyDirs { ############################################################################## #.SYNOPSIS # Creates empty direcrories. # # #.DESCRIPTION # Creates empty direcrories in existing directory. # #.PARAMETER Directory # Root directory. # # .EXAMPLE # $directory = "C:\root" # $relativeDirs = @("\App_Data\MediaCache", "\_DEV") # New-EmptyDirs -Directory $directory -RelativeDirs $relativeDirs ############################################################################## [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [ValidateScript({ if (Test-Path $_) { $true } else { Throw "The $_ path is not valid. Please, provide a valid value." }})] [string]$Directory, [string[]]$RelativeDirs ) ForEach($dir in $RelativeDirs) { $absolutePath = Join-Path -Path $Directory -ChildPath $dir if(Test-Path $absolutePath) { continue } #TODO handle files New-Item -Path $absolutePath -ItemType Directory } } function Show-RobocopyResultMessage { ############################################################################## #.SYNOPSIS # Displays detailed result message after Robocopy execution # # #.DESCRIPTION # Displays detailed result message depending on the Robocopy exit code # #.PARAMETER RobocopyExitCode # Robocopy exit code # # .EXAMPLE # Robocopy -SOME_OPTIONS # $lastExitCode = $LASTEXITCODE # Show-RobocopyResultMessage -RobocopyExitCode $lastExitCode ############################################################################## [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [int]$RobocopyExitCode ) $lastExitcodeMessage = "Robocopy 'Exit code $RobocopyExitCode' :" $checkLogMessage = "Check log file for details." switch ($RobocopyExitCode) { 0 { Write-Output "$lastExitcodeMessage SUCCESS. No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized." } 1 { Write-Output "$lastExitcodeMessage SUCCESS. One or more files were copied successfully." } 2 { Write-Output "$lastExitcodeMessage SUCCESS. Some Extra files or directories were detected. No files were copied." } 3 { Write-Output "$lastExitcodeMessage SUCCESS. Some files were copied. Additional files were present. No failure was encountered." } 4 { Write-Warning "$lastExitcodeMessage WARNING. Some Mismatched files or directories were detected. Housekeeping might be required. $checkLogMessage" } 5 { Write-Warning "$lastExitcodeMessage WARNING. Some files were copied. Some files were mismatched. No failure was encountered. $checkLogMessage" } 6 { Write-Warning "$lastExitcodeMessage WARNING. Additional files and mismatched files exist. No files were copied and no failures were encountered. This means that the files already exist in the destination directory. $checkLogMessage" } 7 { Write-Warning "$lastExitcodeMessage WARNING. Files were copied, a file mismatch was present, and additional files were present. $checkLogMessage" } 8 { Write-Error "$lastExitcodeMessage ERROR. Some files or directories could not be copied(copy errors occurred and the retry limit was exceeded). $checkLogMessage" } { @(9, 10, 11, 12, 13, 14, 15) -contains $_ } { Write-Error "$lastExitcodeMessage ERROR. $checkLogMessage" } 16 { Write-Error "$lastExitcodeMessage FATAL ERROR. Robocopy did not copy any files. Either a usage error or an error due to insufficient access privileges on the source or destination directories. $checkLogMessage" } default { Write-Error "$lastExitcodeMessage ERROR. UNKNOWN CODE. $checkLogMessage" } } } |