Declare functions at the beginning of the script

This topic contains 3 replies, has 3 voices, and was last updated by  Fan Mar 3 months ago.

  • Author
    Posts
  • #94011

    Fan Mar
    Participant

    Hello,
    I'm a complete ps newbie and please correct me, in case this is not the right forum!
    My question is how I can declare a function in ps at the beginning of the script and initialize the function at the end of the script. I'm looking for something like this.
    Thank you very much for your help!

    #################################################################
    #define function at the beginning of the code
    function: Build-EmailBody #here I get an error: "The term function: is not recognized"...

    #main function (just contains the basic functionalities, no long values, so that it is easily to oversee)
    main{
    $params = @{
    From = $sender
    To = $recipient
    SmtpServer = $smtpServer
    BodyAsHtml = Build-EmailBody()
    Body = $body
    Subject = "test"
    }
    Send-MailMessage @params
    }

    #initialization of the Build-EmailBody-Function
    #this function is very long because of the base64 image-code
    #to give the script a good strcture, I wan to put this function to the end of the code
    function Build-EmailBody{
    $letterBeginning = Get-content "C:\bla\test1.html"
    $letterEnd = Get-content "C:\bla\test2.html"
    #image in base64-code
    $img2 =@"
    iVBORw0KGgoAAAANSUhEUgAAACkAAAAxCAYAAAEw4rCWAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAABISSURBVFhHjVkJeBRVtq5OCITs3UknYclCQhIhK4GEhD0JEAEBAY0oKCAIgsoisqgQdgZEVBx3QQ2CiCwuwxP5GPn0zTxHeTKOIyg8QZJOd6ezg1nY87//3OpKmgw609/3p25V3Tp17jnnnvOfiuYVkQnNnNwGSyq08AxoWlgqKka
    MRAuAaw47nCOGwxTaW79R3jkULZcvodxsVWPNcpt+w2HyR5nWAaWE3DCZ1Y0UCgEq/MPhCukOZ0c+EZIEzRTeB4GaBs2fs4IT0Kt3CrSwdD4R2Q+m8L48oTZumKxp8gRVM1MLt6omK88bBg+Fc2Qh6jdvopojkBLaC1p9ZwvQ0gJbBz9U3X4H0kIToV3QOsPeyYya2Y8q9VItvCiqncjIRWVkPCo6hcJHVvlwTjz+66sjCIwfiABrNMLDZSERffU3eqokFqwbPRo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mDvvhJs2XkAW+cUwdevK99H7xmW9FRy8es78OggKkQmkzR6Ez7ZvxIxPlSSDa768JyQh+TetDpL3qS1e7Bz0yqs3HUYr8weAn9fH3hpYXjomf14cQEbK87Jun8Djv7tU2yeNx4mLQJrd+3
    HI2NysHQHFZzUh3O8oHVIxrJdx7B7+VhdBvOnFtwDJhUObhqpK0kwQL2CY+AV2B0m9qqmoFh0IPUyyXdmCWTGnxaYAJ+IZFhjUhBs4Wo7hCh0tCbCGpeB4C6JfL4LvAMiubAozo9Gh0CO/a10b3d4B3WDN4mtpgXAx5oAa490BITHUoF3paesMamITSKyonXjO/WN/XInuA1k4Bjo+U12kujd26FkGYKMqCEGtmiPYzCITB2sec1otXFhqzwNPw/YHpmI+ArRFUAAAAASUVORK5CYII=
    "@
    $img2_html = ""
    $emailBody = $letterBeginning + $img2 + $letterEnd
    return $emailBody}

  • #94017

    Don Jones
    Keymaster

    You'll need to start by learning the correct syntax for functions in PowerShell 😉 as what you're doing isn't PowerShell.

    An example of a function, followed by a call to that function:

    function Get-Something {
     Param(
     )
    }
    
    Get-Something
    

    "function:" is a reference to the built-in FUNCTION PSDrive. You don't use "function:" to declare a function, and a function isn't declare solely with a name, as you've done with your "main." There's also no reason to use "main" as a procedure name, and in fact that doesn't comply with PowerShell naming conventions.

    • #94171

      Fan Mar
      Participant

      Hello Don Jones, hello postanote,
      thank you for your friendly help!
      I will try to find some time to watch the learning videos which you recomended me, postanote. Thank you for the effort of seeking and posting them all!
      However I have to write this script for my work and so I have to finish it soon.
      I have understood, that it is not the style of PS to first define and then initialize functions, thank you John. My problem is that I do have this very long block of base64-Code in my script, which I need in the middle and which I always have to scroll up...and scroll down and so on! So by now I do have the main instructions in one function at the beginning an I call this function at the end of the script. I will give the main function a more powershell-like name, like "Start-Script". However I have the feeling that this is also not what you meant 🙁 Scrolling though the code lasts so long and is not so helpful when you are trying to work on the code. Have you got an idea, is there another way of structuring the code?

      There is another issue I'm dealing with, and it would be super if you could help me with this, too.
      My task is to send an e-mail automatically with embedded pictures.

      This works when the script reds the pictures from a path, but it would be better for the purpose to have everything in one file.
      Example for taking images from a path and embedding them:
      $attachment2 = Get-Item "C:\Windows\ACO\Scripts\WelcomeEmail\SPMail1.png"
      $img2 = "" -f ($attachment2.Name)"

      So I have tried to encode the pictures in base64 and put them in the code. I'll show you how at the end of my post.

      When I test the base64-code on an internet webpage, it works, however when I send the e-mail it works not!! I have tried a lot (still like PS, don't worry), and to me it looks as if the code is correct but not working in our enterprise network.

      So there is another method of embedding a picture in an e-mail, by giving the image an id and refering to it via cid in the html code.

      I haven't found a good example for this over google- do you maybe have one for me? That would be great. Or do you have an idea why the embedding of pictures is not working?

      Thank you very much and kind regards
      Fa

      Embed an image inline.
      $img2txt =@"
      iVBORw0KGgoAAAANSUhEUgAAACkAAAAxCAYAAAEw4rCWAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAABISSURBVFhHjVkJeBRVtq5OCITs3UknYclCQhIhK4GEhD0JEAEBAY0oKCAIgsoisqgQdgZEVBx3QQ2CiCwuwxP5GPn0zTxHeTKOIyg8QZJOd6ezg1nY87//3OpKmgw609/3p25V3Tp17jnnnvOfiuYVkQnNnNwGSyq08AxoWlgqKkaMRAuAaw47nCOGwxTaW79R3jkULZcvodxsVWPNcpt+w2HyR5nWAaWE3DCZ1Y0UCgEq/MPhCukOZ0c+EZIEzRTeB4GaBs2fs4IT0Kt3CrSwdD4R2Q+m8L48oTZumKxp8gRVM1MLt6omK88bBg+Fc2Qh6jdvopojkBLaC1p9ZwvQ0gJbBz9U3X4H0kIToV3QOsPeyYya2Y8q9VItvCiqncjIRWVkPCo6hcJHVvlwTjz+66sjCIwfiABrNMLDZSERffU3eqokFqwbPRoVw4e7McJjrKNLTH9aVfTnE/W+ZtgT03D1zBlUTZ2KivwCNOwsQdUD02jQ7rpSZjGPTOYyRNPysC6oLBgJe/cYVBVNhq1TkLqeFsqJYkvRR369pr6J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oHVIxrJdx7B7+VhdBvOnFtwDJhUObhqpK0kwQL2CY+AV2B0m9qqmoFh0IPUyyXdmCWTGnxaYAJ+IZFhjUhBs4Wo7hCh0tCbCGpeB4C6JfL4LvAMiubAozo9Gh0CO/a10b3d4B3WDN4mtpgXAx5oAa490BITHUoYF3paesMamITSKyonXjO/WN/XInuA1k4Bjo+U12kujd26FkGYKMqCEGtmiPYzCITB2sec1otXFhqzwNPw/YHpmI+ArRFUAAAAASUVORK5CYII=
      "@

      $img2_html = @"
      test
      "@

      #And tis is how I buid the email body and send the email
      #put everything together in the body
      $emailBody = @"

      Guten Tag $toName ,

      a:link { color:#0563c1; text-decoration: underline; }
      im Namen der IT heiße ich Sie herzlich willkommen bei ACO!
      Wir hoffen Sie hatten einen angenehmen Start bei uns.

      Einige Tage sind Sie bereits bei ACO, daher möchten wir uns gerne kurz bei Ihnen vorstellen:
      Die IT bei ACO wird zentral aus der Academy in Rendsburg gesteuert und unser User-Helpdesk steht Ihnen in der Zeit von Montag-Donnerstag von 6:00 – 16:45Uhr und Freitags von 6:00 – 15:15Uhr über unser
      Service Portal $img2

      $letterEnd

      $fromName

      "@

      return $emailBody
      }

  • #94096

    postanote
    Participant

    As for this...

    I'm a complete ps newbie and please correct me, in case this is not the right forum!

    Any of the PowerShell (PoSH) forums are the place to view samples, search for examples, and ask questions.

    However, as a complete noob to any language, it's best to do some 'no cost online courses', before you dive into it. Otherwise you are going to have unnecessary frustration, issues, and wrong thinking. As DonJ just pointed.

    There are far to many freely available training options, article, resources, and built-in online help to get you where you need to be at the start. Without wrapping yourself in a baseline, you will easily come to potentially hate what you are trying to do, because of guessing about things, assuming something should work, or work like X or Y like something else. Don't do this to yourself. Take the time, even just a few days, to watch the online videos for beginning PowerShell.

    Here are few (well, a bunch), to get you on your way to a better understanding and experience with all that is PowerShell

    Use the default built-in help

        # Get parameters, examples, full and Online help for a cmdlet or function
    
        (Get-Command -Name Get-Content).Parameters
        Get-help -Name Get-Content -Examples
        Get-help -Name Get-Content -Full
        Get-help -Name Get-Content -Online
    
        Get-Help -Name about_* 
        Get-Help -Name about_Functions
    
    
        # All Help topics locations
        explorer "$pshome\$($Host.CurrentCulture.Name)"
    

    Resource list

    Microsoft Virtual Academy
    'mva.microsoft.com/liveevents/powershell-jumpstart '
    'mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276'
    'mva.microsoft.com/search/SearchResults.aspx#!q=PowerShell&lang=1033'

    Microsoft Channe9
    'channel9.msdn.com/Series/GetStartedPowerShell3'
    'channel9.msdn.com/Search?term=powershell#ch9Search&lang-en=en&pubDate=year'

    Youtube
    'youtube.com/watch?v=wrSlfAfZ49E 'youtube.com/results?search_query=beginning+powershell
    'youtube.com/results?search_query=powershell+ise+scripting+for+beginners'

    Windows PowerShell Survival Guide
    'social.technet.microsoft.com/wiki/contents/articles/183.windows-powershell-survival-guide.aspxStart-Process'

    eBooks...
    'blogs.technet.microsoft.com/pstips/2014/05/26/free-powershell-ebooks'
    'idera.com/resourcecentral/whitepapers/powershell-ebook 'powershell.org/ebooks'

    Books Learn Windows PowerShell in a Month of Lunches
    manning.com/jones'

    Windows PowerShell in Action, Third Edition
    'amazon.com/s/ref=nb_sb_ss_c_1_28/133-0145057-4953560?url=search-alias%3Daps&field-keywords=windows+powershell+in+action%2C+third+edition&sprefix=Windows+PowerShell+in+Action%2Caps%2C220&crid=1IS8LZE5E6EIE'

    Windows PowerShell Cookbook, 2nd Edition
    'shop.oreilly.com/product/9780596801519.do'

    And start with lots of examples.
    'powershellgallery.com 'gallery.technet.microsoft.com/scriptcenter/site/requests 'technet.microsoft.com/en-us/scriptcenter/bb410849.aspx'

    Then, learn by doing, by discovery.

    1 – Everything you'd normally do at the DOS command prompt or VBScript, etc... start doing all that in the PowerShell ISE or PowerShell Console host or Visual Studio Code using the PowerShell Extension. Using only the normal DOS commands and then do the same with the cmdlets which do the same thing.

    Windows PowerShell equivalents for common networking commands (IPCONFIG, PING, NSLOOKUP)
    'blogs.technet.microsoft.com/josebda/2015/04/18/windows-powershell-equivalents-for-common-networking-commands-ipconfig-ping-nslookup'

    Know that interactive DOS commands don't work in the PowerShell ISE natively. You can make them work.

    See Using Windows PowerShell to run old command line tools (and their weirdest parameters)
    'blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters'

    Note: Sometimes it is just easier to use the old DOS commands, even in PowerShell scripts, because though they may not be as flexible, they are more concise. I wrote a function that allows me to do this sort of stuff to avoid having to type all this stuff all the time.

    2 – Read the help files and about_* files, leverage the examples in the help files. Tweak the examples in the help files.

    3 – Use any Windows tool that will write the baseline code for you (which you can then tweak), such as the Windows Server 2012 ADAC console leveraging the PowerShell History Viewer.

    Active Directory Administrative Center:
    Getting Started 'technet.microsoft.com/en-us/library/dd560651(v=ws.10).aspx'

    Active Directory Administrative Center
    'docs.microsoft.com/en-us/windows-server/identity/ad-ds/get-started/adac/active-directory-administrative-center'

    Step-By-Step: Utilizing PowerShell History Viewer in Windows Server 2012 R2
    'blogs.technet.microsoft.com/canitpro/2015/03/04/step-by-step-utilizing-powershell-history-viewer-in-windows-server-2012-r2'

    4 – Make a copy of...

    C:\Windows\System32\WindowsPowerShell\v1.0\Modules ...

    then, open the scripts, modules, functions in the default Windows PowerShell folder, in the PowerShell ISE or VSCode and review that's going on in them. Make a copy of them to play with and tweak. Also look for any other location that has .ps files, make a copy and review.

    5 – You can also directly look at many of the function code in PowerShell by doing this approach...

    Spying on Function Source Code
    'community.idera.com/powershell/powertips/b/tips/posts/spying-on-function-source-code'

    6 – Get real familiar with the PowerShell Snippets feature. press CTRL+J to see what is there.

    7 – Other MS produces and 3rdP vendor products, will show the PowerShell code they are using under the covers. As you use the Window GUI's pause a minute to look for the code on the screen and copy and save that off for your review.

    8 – Continue the video training, MVA, 'channel9.msdn.com/tags/PowerShell', YouTube, Plural, etc.

    9 – Keep a library of everything you find in a central location, OneNote, doc, Github, or your own module for easy recall / research.

You must be logged in to reply to this topic.