GlobalFunctions shared PowerShell library

Last Updated: 2017-08-19

Exchange Server 2013Exchange Server 2016Description

PowerShell module providing centralizied logging and other helpful functions.

Examples

Import-Module GlobalFunctions 
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path 
$ScriptName = $MyInvocation.MyCommand.Name 

# Create a new logger object, keeping the last 14 days of log files
$logger = New-Logger -ScriptRoot $ScriptDir -ScriptName $ScriptName -LogFileRetention 14 

# Write a new informational message to the log file  
$logger.Write('My Log Message')

# Write an error message to the log file
$logger.Write('My custom error message')

# Write a warning message to the log file
$logger.Write('My custom warning')

# Send a log file by email at the end of your script
$logger.SendLogFile('sender@mcsmemail.de', 'recipient@mcsmemail.de', 'smtpserver.mcsmemail.de')

How to install a PowerShell Module

PowerShell 5

When using PowerShell 5 or newer, you can simply use the following PowerShell command from within an administrative PowerShell window.

Install-Module GlobalFunctions

When a new version of the GlobalFunctions module has been released, use the following PowerShell command to update the module.

Older PowerShell Version

You can “install” a PowerShell module by copying the module to a sub folder of the same name as the module in either of the two following locations:

  • Default PowerShell module path define by PSModulePath variable ($env:PSModulePath)
  • Custom PowerShell module path, which requires the full path to be added to the PSModulePath system variable

Default PowerShell Module Path

  • Open a new PowerShell window and query the content of PSModulePath variable
PS C:\> $env:PSModulePath
C:\Users\admin\Documents\WindowsPowerShell\Modules;
C:\Program Files\WindowsPowerShell\Modules;
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
  • Create a new folder named GlobalFunctions in C:\Program Files\WindowsPowerShell\Modules
  • Copy the GlobalFunctions.psm1 file to C:\Program Files\WindowsPowerShell\Modules\GlobalFunctions

    That’s it.

Custom PowerShell Module Path

These steps assume that you use a dedicated PowerShell scripts folder, e.g. D:\MyScripts

  1. Create a new folder named MyModules in D:\MyScripts
    -> D:\MyScripts\MyModules
  2. Create a new folder named GlobalFunctions in D:\MyScripts\MyModules
    -> D:\MyScripts\MyMOdules\GlobalFunctions
  3. Copy the GlobalFunctions.psm1 file to D:\MyScripts\MyModules\GlobalFunctions
  4. Add the full file path D:\MyScripts\MyModules to the system variable PSModulePath
    You can use Set-PersistentPSModulePath.ps1 script, provided here. Just copy the script to D:\MyScripts\MyModules 
    .\Set-PersistentPSModulePath.ps1 -Add

Close the current PowerShell window and open a new PowerShell window. That’s it.

Version History

  • 1.0 Initial release
  • 1.1 Write to Event log added, send log file added
  • 1.2 CopyFile added
  • 1.3 Updated for PowerShellGallery
  • 2.0 Converted to UNICODE, Functions added: Replace-SpecialCharactersUpperCase, New-RandomPassword
  • 2.1 WriteToConsole switch added to Logger.Write method

Links

Follow

%d Bloggern gefällt das: