diff --git a/.gitignore b/.gitignore index 91f3e39..3aec8dc 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,18 @@ $RECYCLE.BIN/ # ========== # Data files # ========== -*.cro \ No newline at end of file +*.cro + +# ================ +# Encryption Files +# ================ + +# Credential files +*.key + +# ============ +# Module Files +# ============ + +# Manifiest files +/Modules/*/*.psd1 \ No newline at end of file diff --git a/Modules/File.Transfer/File.Transfer.psm1 b/Modules/File.Transfer/File.Transfer.psm1 new file mode 100644 index 0000000..801d0f2 --- /dev/null +++ b/Modules/File.Transfer/File.Transfer.psm1 @@ -0,0 +1,42 @@ +# Get functions and types to import +$scriptsToConfigure = @(Get-ChildItem -Path $PSScriptRoot -File -Filter "*.ps1" -ErrorAction SilentlyContinue) +$classesToImport = @( Get-ChildItem -Path $PSScriptRoot\Classes -File -Filter "*.cs" -ErrorAction SilentlyContinue | Sort-Object Name ) +$privateFunctionsToImport = @( Get-ChildItem -Path $PSScriptRoot\Private -File -Filter "*.ps1" -ErrorAction SilentlyContinue | Sort-Object Name ) +$publicFunctionsToImport = @( Get-ChildItem -Path $PSScriptRoot\Public -File -Filter "*.ps1" -ErrorAction SilentlyContinue | Sort-Object Name ) +$scriptsToImport = $privateFunctionsToImport + $publicFunctionsToImport + +# Special Module's configurations +foreach ($script in $scriptsToConfigure) { + try { + if($script.BaseName -eq $script.Directory.Name) + { & $script.FullName } + } + catch { + Write-Warning -Message "Failed to configure the module with $($script.FullName) script: $_" + return + } +} + +# Add types based on C# Classes +foreach ($class in $classesToImport) { + try { + $classType = Get-Content -Path $class.FullName -Raw + Add-Type -TypeDefinition $classType + } + catch { + Write-Error -Message "Failed to import the class $($class.FullName): $_" + return + } +} + +# Import functions +foreach ($function in $scriptsToImport) { + try { . $function.FullName } + catch { + Write-Error -Message "Failed to import the script $($function.FullName): $_" + return + } +} + +# Export public functions +Export-ModuleMember -Function $publicFunctionsToImport.BaseName diff --git a/Modules/File.Transfer/Public/New-FileTransfer.ps1 b/Modules/File.Transfer/Public/New-FileTransfer.ps1 new file mode 100644 index 0000000..044f32b --- /dev/null +++ b/Modules/File.Transfer/Public/New-FileTransfer.ps1 @@ -0,0 +1,51 @@ +function New-FileTransfer { + [CmdletBinding()] + param ( + [Parameter(Mandatory)] + [String] + $OriginPath, + [Parameter(Mandatory)] + [String] + $DestinationPath, + [Parameter(Mandatory)] + [String] + $FileNameToCopy + ) + + if ($null -ne $OriginPath){ + $originPath = (Get-Item -Path $OriginPath).FullName + } + + if ($null -ne $DestinationPath){ + $destinationPath = (Get-Item -Path $DestinationPath).FullName + } + + if ($null -ne $FileToCopy){ + $fileNameToCopy = (Get-Item -Path $FileNameToCopy).FullName + } + + $credential = Get-Credential + $originSVR = New-PSDrive -Name "OriginSVR" -Root $originPath -PSProvider "FileSystem" -Credential $credential + $fileToCopy = Get-ChildItem -Path $originSVR.Root | Where-Object {$_.Name -eq $fileNameToCopy} + + try { + Copy-Item -Path $fileToCopy.FullName -Destination $destinationPath + + $fileInDestination = Get-ChildItem -Path $destinationPath | Where-Object {$_.Name -eq $fileToCopy.Name} + + [String] $originFileHash = (Get-FileHash -Path $fileToCopy.FullName -ErrorAction SilentlyContinue).Hash + [String] $destinationFileHash = (Get-FileHash -Path $fileInDestination.FullName -ErrorAction SilentlyContinue).Hash + + if ($originFileHash -eq $destinationFileHash) { + Write-Host "OK, the integrity is correct" + } + else { + [String] $differentHashError = ("[ERROR] - File integrity error: {0}" -f $DestinationFile) + Write-Error -Message $differentHashError -ErrorAction Stop + } + } + # Catch the error when the file is being used + catch [Microsoft.PowerShell.Commands.WriteErrorException] { + Write-Host "Error, the file es being used" + } +} diff --git a/Modules/Template/Template.psm1 b/Modules/Template/Template.psm1 index 7705917..801d0f2 100644 --- a/Modules/Template/Template.psm1 +++ b/Modules/Template/Template.psm1 @@ -24,7 +24,7 @@ foreach ($class in $classesToImport) { Add-Type -TypeDefinition $classType } catch { - Write-Warning -Message "Failed to import the class $($class.FullName): $_" + Write-Error -Message "Failed to import the class $($class.FullName): $_" return } } @@ -33,7 +33,7 @@ foreach ($class in $classesToImport) { foreach ($function in $scriptsToImport) { try { . $function.FullName } catch { - Write-Warning -Message "Failed to import the script $($function.FullName): $_" + Write-Error -Message "Failed to import the script $($function.FullName): $_" return } }