Alan's Blog

"Yeah. I wrote a script that will do that."

Menu
  • About My Blog
Menu

A Dot Source Reminder for Advanced Functions

Posted on February 25, 2017April 2, 2017 by Alan

One of the problems with writing advanced functions is that new PowerShell users think that they don’t do anything.  Frankly, I couldn’t figure out a way to get a notification to work, so I reached out to the sponsor for the Charlotte PowerShell User Group, Microsoft PFE Brian Wilhite. Brian sent me some code  which I incorporated into the following snippet:

### Dot Source Reminder Begins ###
$ScriptPath= $MyInvocation.MyCommand.Definition.ToString()
$fn =  $MyInvocation.MyCommand.Name.Replace(".ps1","")
if (($host.Name).Contains("ISE")){
 Write-host  "`n`nYou have loaded the advanced function `"$fn`"" -F Green
  "  Use `"Get-Help`" for information and usage, Ex:
  PS C:\> Get-Help $fn -detailed`n`n"
}ELSE{
 if ($MyInvocation.InvocationName -ne '.') {
 Write-Host "`n`nThis advanced function must be dot sourced to run.`n" -F Green
 " Load example:`n PS C:\> . `"$ScriptPath`n
 After loading, use `"Get-Help`" for information and usage, Ex:
 PS C:\> Get-Help $fn -detailed`n`n"
 #Pause if launched from shell with  "Run with PowerShell"
 if (([Environment]::GetCommandLineArgs()) -match '&'){pause}
 }
}
### Dot Source Reminder Ends  ###

If you expect to have the end-user save the function with code calling it at the bottom of the script, try this version which won’t prompt if there is added text at the bottom:

### Dot Source Reminder Begins ###
$ScriptPath= $MyInvocation.MyCommand.Definition.ToString()
#you can hard code name of function as $fn
#$fn = "Verb-Noun"
#Presumew function name is same as script Name
$fn =  $MyInvocation.MyCommand.Name.Replace(".ps1","")
$ScriptTxt = Get-Content $ScriptPath
$MoreTxt = [regex]::Split($ScriptTxt,'Reminder Ends  ###')[2]
if ($MoreTxt.Length -lt $fn.Length){
    if (($host.Name).Contains("ISE")){
        Write-host  "`nYou have loaded the advanced function `"$fn`"" -F Green
        "`tUse `"Get-Help`" for information and usage, Ex:`n`tPS C:\> Get-Help $fn -detailed`n"
    }ELSE{
    if ($MyInvocation.InvocationName -ne '.') {
        Write-Host "`nThis advanced function must be dot sourced to run.`n" -F Green
        "Load example:`n`tPS C:\> . `"$ScriptPath`"`n
        After loading, use `"Get-Help`" for information and usage, Ex:
        PS C:\> Get-Help $fn -detailed`n"
        #Pause if launched from shell with  "Run with PowerShell"
        if (([Environment]::GetCommandLineArgs()) -match '&'){pause}
    }
  }
}
### Dot Source Reminder Ends  ###

Put either bit of code at the bottom of any advanced function. If the script runs inside the ISE, you will get something like this:

You have loaded the advanced function "Get-DownTime"
Use "Get-Help" for information and usage, Ex:
PS C:\> Get-Help Get-DownTime -detailed

If you run it inside the PowerShell console you will see:

This advanced function must be dot sourced.

Load example:
PS C:\> . "C:\scripts\Get-DownTime.ps1"

Use "Get-Help" for information for information and usage, Ex:
PS C:\> Get-Help Get-DownTime -detailed

If you use the shell menu option to Run with PowerShell, it adds a pause so the script does not close.  Note that the colors are not rendered properly in the example text  — the first line has green text, following lines display with default colors.

Update:  if the code above doesn’t pause when you use “Run with PowerShell” from the shell context menu, replace if (([Environment]::GetCommandLineArgs()) -match ‘&’){pause} with if ([regex]::IsMatch([Environment]::GetCommandLineArgs(), ‘powershell_ise’) -eq $false){Pause}. For more on this see this post.

Leave a Reply

You must be logged in to post a comment.

Search

Please Note

All the scripts are saved as .txt files. Newer files have a “View Script” button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use “Save Target As”. Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with version 3 or later.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient.  If you find a post error or a script which doesn’t work as expected, I appreciate being notified.  My email is my first name at the domain name, and you are welcome to contact me that way.

Tags

1E ACLS Active Directory ActiveDirectory ADSI Advanced Functions Audit Change Administrator Password COMObject Computer Groups DateTime Desktop DNS Excel FileScriptingObject Forms General GPO GPS Group Policy Hacks ISE Lockout logons NAV740 Nessus OU OU permissions Outlook Pick Folder Power PowerShell Powershell Scriptlets RDP SCCM schedule reboot Scripting Security Shell.Application user information VBA Windows Update WMI WordPress WPF

Categories

akaplan.com

  • Back to Home Page

Archives

Scripting Sites

  • A Big Pile of Small Things
  • Adam, the Automator
  • Art of the DBA
  • Ashley McGlone
  • Boe Prox
  • Carlo Mancini
  • DexterPOSH
  • Doug Finke
  • Jaap Brasser's Blog
  • JeffOps The Scripting Dutchman
  • Jonathan Medd's Blog
  • Keith Hill's Blog
  • LazyWinAdmin
  • Nana Lakshmanan
  • PowerShell Magazine
  • PowerShell Team Blog
  • PowerShell.org
  • PwrShell.net
  • Richard Siddaway's Blog
  • Ryan Yates' Blog
  • Skatterbrainz
  • The Lonely Administrator

SQL Site

  • Art of the DBA

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Alan's Blog | Theme by SuperbThemes

Terms and Conditions - Privacy Policy