<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Powershell: Load and use a Dynamic Module directly from GitHub in Tasks in HPE Morpheus Enterprise</title>
    <link>https://community.hpe.com/t5/hpe-morpheus-enterprise/powershell-load-and-use-a-dynamic-module-directly-from-github-in/m-p/7251108#M4282</link>
    <description>&lt;P&gt;I use a library of Powershell functions frequently in my Morpheus Tasks. I decided to build these into a script and store it on Github. For convenience I developed a Powershell function which you can add to your Morpheus Powershell tasks. The function takes a &lt;STRONG&gt;raw&lt;/STRONG&gt; Url from Github and loads the Powershell into a Dynamic Module which then gives the Morpheus Task access to the library of Powershell functions.&lt;/P&gt;
&lt;P&gt;Follow the example below to try this for yourself.&lt;/P&gt;
&lt;P&gt;Create a &lt;STRONG&gt;Powershell&lt;/STRONG&gt; Task type, set the E&lt;STRONG&gt;xecute Target&lt;/STRONG&gt; as &lt;STRONG&gt;Resource&lt;/STRONG&gt;. and add this function into the Task script&lt;/P&gt;
&lt;PRE style="background : #f0f1f2;"&gt;&lt;CODE class="lang-auto"&gt;# Declare the function - this will be standard in all Tasks
Function New-ModulefromGitHub {
    &amp;lt;#
    .SYNOPSIS
    Takes a Github Raw url, downloads the Powershell Script and Executes as a Dynamic Module

    .DESCRIPTION
    Takes a Github Raw url, downloads the Powershell Script and Executes as a Dynamic Module

    Examples:
    New-ModulefromGitHub -ScriptUrl &amp;lt;Github Raw URL for the Powershell script&amp;gt; -Name "GibModule"

    .PARAMETER ScriptUrl
    GitHub Raw Url to Powershell script

    .PARAMETER Name
    A String to identify the Dynamic Module name

    .OUTPUTS
    Loads a Dynamic Module

    #&amp;gt;     
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true,Position=0)]
        [String]$ScriptUrl,
        [Parameter(Mandatory=$true,Position=1)]
        [String]$Name
    )
    if ($ScriptUrl) {
        try {
            $Response = Invoke-WebRequest -Uri $ScriptUrl -UseBasicParsing -ErrorAction SilentlyContinue
            $StatusCode = $Response.StatusCode
        }
        catch {
            $StatusCode = $_.Exception.Response.StatusCode
            Write-Warning "Cannot locate Script Url $ScriptUrl - Status:$StatusCode"
        }
        if ($StatusCode -eq 200) {
            try {
                New-Module -Name $Name -ScriptBlock ([ScriptBlock]::Create($Response.Content))
            }
            catch {
                Write-Error "ScriptUrl is not a valid Powershell Module ScriptBlock"
            }           
        }
    } else {
        Write-Warning "You must enter a Script URL"
    }
}

# Start your custom script here

# Git Raw URL of a scipt to load as a Module. (Note it must be a raw Url type)
$GitUrl = "https://raw.githubusercontent.com/spottsmorpheus/invokePSTask/main/Examples/moduleExample.ps1"

# Hide Progress updates in Morpheus
$ProgressPreference="SilentlyContinue"

# Load the Dynamic Module using the function created above. To prevent output being written into the
# script assign the command below into a variable eg $NoOutput = New-ModuleFromGitHub ...

New-ModuleFromGitHub -ScriptUrl $GitUrl -Name "Git-Test"

#Fuctions are now available - use them as you normally would. You may access Morpheus Variables
$Inst = "&amp;lt;%=instance.name%&amp;gt;"
#Call a Function defined in the Module - here you can pass in any Morpheus Variables
$Status = Get-InstanceInfo -Instance $Inst 
$Status | ConvertTo-Json -Depth 3

#End of Task

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Execute the task against an Instance type. It should generate output as so&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;DIV class="lightbox-wrapper"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Git-test"&gt;&lt;img src="https://community.hpe.com/t5/image/serverpage/image-id/149796i3C09DECCA4179C54/image-size/large?v=v2&amp;amp;px=2000" role="button" title="abf2d68f91c44038993f2fb33e409ebd8b97d327.png" alt="abf2d68f91c44038993f2fb33e409ebd8b97d327.png" /&gt;&lt;/span&gt;&lt;/DIV&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Steve Potts&lt;BR /&gt;
Morpheus Support EMEA&lt;/P&gt;</description>
    <pubDate>Tue, 26 Jul 2022 20:42:22 GMT</pubDate>
    <dc:creator />
    <dc:date>2022-07-26T20:42:22Z</dc:date>
    <item>
      <title>Powershell: Load and use a Dynamic Module directly from GitHub in Tasks</title>
      <link>https://community.hpe.com/t5/hpe-morpheus-enterprise/powershell-load-and-use-a-dynamic-module-directly-from-github-in/m-p/7251108#M4282</link>
      <description>&lt;P&gt;I use a library of Powershell functions frequently in my Morpheus Tasks. I decided to build these into a script and store it on Github. For convenience I developed a Powershell function which you can add to your Morpheus Powershell tasks. The function takes a &lt;STRONG&gt;raw&lt;/STRONG&gt; Url from Github and loads the Powershell into a Dynamic Module which then gives the Morpheus Task access to the library of Powershell functions.&lt;/P&gt;
&lt;P&gt;Follow the example below to try this for yourself.&lt;/P&gt;
&lt;P&gt;Create a &lt;STRONG&gt;Powershell&lt;/STRONG&gt; Task type, set the E&lt;STRONG&gt;xecute Target&lt;/STRONG&gt; as &lt;STRONG&gt;Resource&lt;/STRONG&gt;. and add this function into the Task script&lt;/P&gt;
&lt;PRE style="background : #f0f1f2;"&gt;&lt;CODE class="lang-auto"&gt;# Declare the function - this will be standard in all Tasks
Function New-ModulefromGitHub {
    &amp;lt;#
    .SYNOPSIS
    Takes a Github Raw url, downloads the Powershell Script and Executes as a Dynamic Module

    .DESCRIPTION
    Takes a Github Raw url, downloads the Powershell Script and Executes as a Dynamic Module

    Examples:
    New-ModulefromGitHub -ScriptUrl &amp;lt;Github Raw URL for the Powershell script&amp;gt; -Name "GibModule"

    .PARAMETER ScriptUrl
    GitHub Raw Url to Powershell script

    .PARAMETER Name
    A String to identify the Dynamic Module name

    .OUTPUTS
    Loads a Dynamic Module

    #&amp;gt;     
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true,Position=0)]
        [String]$ScriptUrl,
        [Parameter(Mandatory=$true,Position=1)]
        [String]$Name
    )
    if ($ScriptUrl) {
        try {
            $Response = Invoke-WebRequest -Uri $ScriptUrl -UseBasicParsing -ErrorAction SilentlyContinue
            $StatusCode = $Response.StatusCode
        }
        catch {
            $StatusCode = $_.Exception.Response.StatusCode
            Write-Warning "Cannot locate Script Url $ScriptUrl - Status:$StatusCode"
        }
        if ($StatusCode -eq 200) {
            try {
                New-Module -Name $Name -ScriptBlock ([ScriptBlock]::Create($Response.Content))
            }
            catch {
                Write-Error "ScriptUrl is not a valid Powershell Module ScriptBlock"
            }           
        }
    } else {
        Write-Warning "You must enter a Script URL"
    }
}

# Start your custom script here

# Git Raw URL of a scipt to load as a Module. (Note it must be a raw Url type)
$GitUrl = "https://raw.githubusercontent.com/spottsmorpheus/invokePSTask/main/Examples/moduleExample.ps1"

# Hide Progress updates in Morpheus
$ProgressPreference="SilentlyContinue"

# Load the Dynamic Module using the function created above. To prevent output being written into the
# script assign the command below into a variable eg $NoOutput = New-ModuleFromGitHub ...

New-ModuleFromGitHub -ScriptUrl $GitUrl -Name "Git-Test"

#Fuctions are now available - use them as you normally would. You may access Morpheus Variables
$Inst = "&amp;lt;%=instance.name%&amp;gt;"
#Call a Function defined in the Module - here you can pass in any Morpheus Variables
$Status = Get-InstanceInfo -Instance $Inst 
$Status | ConvertTo-Json -Depth 3

#End of Task

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Execute the task against an Instance type. It should generate output as so&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;DIV class="lightbox-wrapper"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Git-test"&gt;&lt;img src="https://community.hpe.com/t5/image/serverpage/image-id/149796i3C09DECCA4179C54/image-size/large?v=v2&amp;amp;px=2000" role="button" title="abf2d68f91c44038993f2fb33e409ebd8b97d327.png" alt="abf2d68f91c44038993f2fb33e409ebd8b97d327.png" /&gt;&lt;/span&gt;&lt;/DIV&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Steve Potts&lt;BR /&gt;
Morpheus Support EMEA&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2022 20:42:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/hpe-morpheus-enterprise/powershell-load-and-use-a-dynamic-module-directly-from-github-in/m-p/7251108#M4282</guid>
      <dc:creator />
      <dc:date>2022-07-26T20:42:22Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell: Load and use a Dynamic Module directly from GitHub in Tasks</title>
      <link>https://community.hpe.com/t5/hpe-morpheus-enterprise/powershell-load-and-use-a-dynamic-module-directly-from-github-in/m-p/7251109#M4283</link>
      <description>&lt;P&gt;Ooo this is nifty&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2022 21:35:02 GMT</pubDate>
      <guid>https://community.hpe.com/t5/hpe-morpheus-enterprise/powershell-load-and-use-a-dynamic-module-directly-from-github-in/m-p/7251109#M4283</guid>
      <dc:creator>cbunge</dc:creator>
      <dc:date>2022-07-26T21:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell: Load and use a Dynamic Module directly from GitHub in Tasks</title>
      <link>https://community.hpe.com/t5/hpe-morpheus-enterprise/powershell-load-and-use-a-dynamic-module-directly-from-github-in/m-p/7251110#M4284</link>
      <description>&lt;P&gt;Brill work. Thanks for sharing &lt;IMG src="https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2022 07:27:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/hpe-morpheus-enterprise/powershell-load-and-use-a-dynamic-module-directly-from-github-in/m-p/7251110#M4284</guid>
      <dc:creator>cdtaylor</dc:creator>
      <dc:date>2022-07-27T07:27:15Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell: Load and use a Dynamic Module directly from GitHub in Tasks</title>
      <link>https://community.hpe.com/t5/hpe-morpheus-enterprise/powershell-load-and-use-a-dynamic-module-directly-from-github-in/m-p/7251111#M4285</link>
      <description>&lt;P&gt;Nice work Steve, keep them coming!&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2022 14:11:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/hpe-morpheus-enterprise/powershell-load-and-use-a-dynamic-module-directly-from-github-in/m-p/7251111#M4285</guid>
      <dc:creator />
      <dc:date>2022-07-27T14:11:15Z</dc:date>
    </item>
  </channel>
</rss>

