110 likes | 213 Views
Powershell Peer Review. New-OutFilePathBase.ps1 9-21-2013. Past Script Logging. In the past, NT BAT and VBS were the only scripting languages that were practically guarantied to be available on all Windows systems . DevOps reports in a global operational environment required:
E N D
Powershell Peer Review New-OutFilePathBase.ps1 9-21-2013
Past Script Logging • In the past, NT BAT and VBS were the only scripting languages that were practically guarantied to be available on all Windows systems. • DevOps reports in a global operational environment required: • Date and time stamps • Relevant report naming • Error logging • VBS awkward access to File System Objects (FSO) was tedious, so best to leave reporting outside of the VBS shell. Same solution works consistently with other commands.
Sample BAT file @ECHO OFF SETLOCAL REM *** ATTENTION: delims char and token order depend upon regional settings in control panel FOR /F "tokens=2-4 delims=/ " %%i in ("%DATE%") DO (SET month=%%i& SET day=%%j& SET year=%%k) FOR /F "tokens=1-3 delims=:." %%l in ("%TIME%") DO (SET hour=%%l& SET minute=%%m& SET second=%%n) SET stamp="%year%%month%%day%T%hour%%minute%%second%" SET stamp=%stamp: =0% IF NOT EXIST Logs MKDIR Logs SET logFile=Logs\%stamp%-%~n0.log <CMD COMMAND> 1>> "%logFile%" 2>&1 • System clock was convert to uniform, zero padded, sortable date and time stamp (pseudo ISO-8601:2004 basic format). • Separation of scripts and reports into a subfolder. • Name of the BAT file implicitly names the report file name • CMD standard output and standard error were merged into one stream.
Powershell Logging • Powershell structure focuses on what you want to do, not how to do it. • Concise reference to: • System clock including time zone offset • Calling script name • Execution environment • Plus native features: • In-place string substitution • Piping and redirection • Consistent support of local files and remote URLs • Export-* commands
New-OutFilePathBase.ps1 • Creates an outfile path name without an extension. • Requires Powershell 2.0, tested with 3.0 • Gets the current script execution location • Builds an ISO-8601:2004 basic format date & time stamp including time zone offset • Gets the current script execution environment; such as forest, domain, computer name, or Exchange organization • Gets the invoking script name
New-OutFilePathBase.ps1Example Usage • Import (dot source) the function: . .\New-OutFilePathBase.ps1 • Call the function: $outFilePathBase = New-OutFilePathBase • Use the results: $outFilePathName = "$($outFilePathBase.Value).csv" • Create the report file: Export-CSV -Path $outFilePathName -NoTypeInformation
New-OutFilePathBase.ps1Sample Results • Default execution: New-OutFilePathBase Value : C:\Users\Terry\Documents\WindowsPowerShell\Reports\20130921T090102-0500-MyCorp FolderPath : C:\Users\Terry\Documents\WindowsPowerShell\Reports\ DateTimeStamp : 20130921T090102-0500 ExecutionSourceName : MyCorp ScriptFileName : FileName : 20130916T090102-0500-MyCorp
New-OutFilePathBase.ps1Parameters • Ability to control most aspects of the generated file path name components: • DateOffsetDays • Optionally specify the number of days added or subtracted from the current date. Default is 0 days. • ExecutionSource • Specifiythe script's execution environment source. Must be either a string, 'ComputerName', 'DomainName', 'ForestName', 'msExchOrganizationName' or an arbitrary string. If msExchOrganizationName is requested, but there is no Exchange organization the domain name will be used; If ForestName is requested, but there is no forest the domain name will be used; if the domain name is requested, but the computer is not a domain member, the computer name is used. Defaults is msExchOrganizationName. An arbitrary string can be used in the case where the Microsoft Exchange Organization name, forest name or domain name is too generic (e.g. 'EMAIL', 'CORP' or 'ROOT'). • FileNameComponentDelimiter • Optional file name component delimiter. Default is hyphen '-'. • InvalidFilePathCharsSubstitute • Optionally specify which character to use to replace invalid folder and file name characters. Default is underscore '_'. The substitute character cannot itself be an folder or file name invalid character. • OutFileNameTag • Optional comment string added to the end of the output file name. • OutFolderPath • Specify where to write the output file. Supports UNC and relative reference to the current script folder. The default is .\Reports subfolder. Except for UNC paths this function will attempt to create and compress the output folder if it doesn’t exist.
New-OutFilePathBase.ps1 …Code Review…
New-OutFilePathBase.ps1Wish List • Code is perfect as is, no opportunity for enhancements • Instead of returning multi-value NoteProperty, rather return a default ‘value’ with other multi-values accessible.
New-OutFilePathBase.ps1 • Thank you for your time. • Terry E Dow