TrainingUtils.NFTP.psm1
<#
.Synopsis Función para escribir logs. .Description Esta función nos permite mostrar log por pantalla y crear un fichero en el disco. .Parameter LogType Tipos de Logs a elegir : INFO, WARNING, ERROR, DEBUG, VERBOSE. .Parameter Message Mensaje que queremos escribir en el log. .Parameter FilePath Path donde vamos a guardar nuestro log File. .Parameter Verbose Muestra por pantalla logs del tipo Verbose .Parameter Debug Muestra por pantalla logs del tipo Debug .Example Write-TrainingUtilsLog -LogType "INFO" -Message "Mensaje de Información" -FilePath .\Mylog.log Write-TrainingUtilsLog -LogType "Debug" -Message "Mensaje de Información" -FilePath c:\temp\Mylog2.log -Debug #> function Write-TrainingUtilsLog() { [CmdletBinding()] param( [Parameter(Mandatory=$true, HelpMessage="Tipos de Logs : INFO, WARNING, ERROR, DEBUG, VERBOSE" )] [ValidateSet("INFO", "WARNING", "ERROR", "DEBUG", "VERBOSE")] [string]$LogType, [ Parameter(Mandatory=$true, HelpMessage="Mensaje que queremos escribir en el log") ] [string]$Message, [ Parameter(Mandatory=$true, HelpMessage="Path donde vamos a guardar nuestro fichero de log") ] [string]$FilePath ) try { $Timestamp= "" $Timestamp= (Get-Date).ToString("dd/MM/yyyy HH:mm:ss") $strLog = "$Timestamp $logType : $message" Write-toScreen -log $strlog -Type $LogType Write-LogFile -FilePath $FilePath -ContentText $strLog } catch { $Message = $_ Write-Warning "Info : " $Message } } # end function function Write-toScreen ([string]$log, [string]$Type){ try { if ( ($Type -eq "DEBUG" -and $PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent) -or ($Type -eq "VERBOSE" -and $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) ) { Write-Output $log } elseif ( ($Type -eq "INFO") -or ($type -eq "WARNING") -or ($Type -eq "ERROR") ) { Write-Output $log } } catch { $Message = $_ Write-Warning "Info : " $Message } }#end function function Write-LogFile([string]$FilePath,[string]$ContentText ){ try { Write-Output "Creating a Log File : " $FilePath Write-Output $ContentText | Out-File $FilePath -Append -Encoding utf8 } Catch{ $Message = $_ Write-Warning "Info : " $Message } }#end function |