• PowerShell Coding Trick

    Home » Forums » Developers, developers, developers » DevOps Lounge » PowerShell Coding Trick

    Author
    Topic
    #2011072

    Hey Y’all,

    I was doing some investigating in PowerShell the other day and found out that you can call
    another .ps1 program from your current .ps1 running program and then use both functions
    and variables declared in the initial program.

    Here’s a couple of test files you can mess with if you are so inclined.

    Test-CallingExternalCode.ps1:

    #---  Test-CallingExternalCode.ps1 ---
    
    #----- Setup for User Messaging --------------------
    Add-Type -AssemblyName System.Windows.Forms  
    
    $MsgBox  = [Windows.Forms.MessageBox]
    $Buttons = [Windows.Forms.MessageBoxButtons]
    #Values: OK, OkCancel, AbortRetryIgnore, YesNo, 
    #        YesNoCancel, RetryCancel
    
    $Icon    = [Windows.Forms.MessageBoxIcon]
    #Values: None Question, [Stop | Hand], 
    #        [Warning | Exclamation], [Information | Asterisk]
    
    $top = 
     new-Object System.Windows.Forms.Form -property @{Topmost=$true}
    $StatusMsg = {
      $MsgBox::Show($top,$Message,$MBTitle,$Buttons,$Icon)}
    
    #---------- End User Messaging Setup --------------------------
    
    Function Hello { 
       $Message = "I'm Function Hello called from " +
                  "Test-CalledExternalCode.ps1"
       $MBTitle = "Function Hello:"
       $Buttons = "Ok"
       $Icon    = "Information"
       $Null    = & $StatusMsg
     } #Used in called script
    
    Clear-Host
    
    If (Test-Path -Path "$PsScriptRoot\Test-CalledExternalCode.ps1") {
    
     #--- Code for retrieving answer from called script  ---#
     
     $Answer = & ($PsScriptRoot + "\Test-CalledExternalCode.ps1")
    
     "The Answer was: $Answer"
    
    <#--- Code when not retrieving answer from called script --- $Null = & ($PsScriptRoot + "\Test-CalledExternalCode.ps1") #>
    
    } #End Test-CallingExternalCode.ps1
    
    

    Test-CalledExternalCode.ps1:

    #---  Test-CalledExternalCode.ps1  ---
    
    <#+---------------------------------------------------------+ | Note: This program uses the Message Box code setup from | | the calling script to write its message! | +---------------------------------------------------------+ #>
    
    Hello   #--- Function declared in calling script
    #---  Test-CalledExternalCode.ps1  ---
    
    #--- Variables declared in calling script ---
    
    $Message = "I'm Function Set-Cleanup"
    $MBTitle = "Program Running:"
    $Buttons = "YesNo"
    $Icon    = "Information"
    
    #--- When retruning the Msg result to the calling script ---
    
    & $StatusMsg  #Note: &StatusMsg defined in calling script!
    
    <#--- When not returning a value to the calling script --- $Null = & $StatusMsg #>
    
    #--- End: Test-CalledExternalCode.ps1  ---
    

    Note: Both programs must reside in the same directory, there might be ways around this but I haven’t worked this out yet.
    If the called program doesn’t exist the calling program will just ignore it and continue on. I found this useful as it allows end users to add their own code, if desired, to a program I’ve written as long as they name it properly.

    Hope you find this as interesting as I did. 😎

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!
    Computer Specs

    1 user thanked author for this post.
    Reply To: PowerShell Coding Trick

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: