KB0075 - Importing Scripts into PowerShell Universal

KB0075 - Importing Scripts into PowerShell Universal

Scope

This article provides details on how to import scripts for users that have existing PowerShell scripts they would like to surface in PowerShell Universal. 

Problem

The scripts.ps1 metadata file is required to display scripts within PowerShell Universal. 

Solution

You can use the following PowerShell script to create a scripts.ps1 file and import them into PowerShell Universal. The scripts are expected to reside in C:\ProgramData\UniversalAutomation\Repository. You can override this with the -Repository parameter. Special folders are excluded. 
  1. [CmdletBinding()]
    param($Repository = "$Env:ProgramData\UniversalAutomation\Repository")

    function Import-Folder {
        param($Path)

        Write-Verbose "Importing $Path"

        $Excluded = @(".universal", ".git", "dashboards", "pages", "modules")

        foreach ($Exclusion in $Excluded) {

            Write-Verbose "Testing $Path against $Exclusion"

            if (Test-Folder -Path $Path -Exclusion $Exclusion) {
                Write-Verbose "Excluding $Path"
                return
            }
        }

        Get-ChildItem -Path $Path | ForEach-Object {
            if ($_.PSIsContainer) {
                Write-Verbose "Importing Folder $($_.FullName)"
                Import-Folder -Path $_.FullName
            }
            else {
                Write-Verbose "Importing Script $($_.FullName)"
                Import-Script -Path $_.FullName
            }
        }
    }

    function Test-Folder {
        param($Path, $Exclusion)

        $ExclusionPath = Join-Path $Repository $Exclusion
        $Path -eq $ExclusionPath
    }

    function Import-Script {
        param($Path)

        $RelativePath = $Path.Replace($Repository, "").TrimStart("\")
        $Line = "New-PSUScript -Name '$($Path | Split-Path -Leaf)' -Path '$RelativePath'"

        $Universal = "$Repository\.universal"
        if (-not (Test-Path $Universal)) {
            Write-Verbose "Creating $Universal"
            New-Item -ItemType Directory -Path $Universal
        }

        Write-Verbose "Writing $Line to $Universal\scripts.ps1"

        $Line | Out-File "$Universal\scripts.ps1" -Append
    }

    Import-Folder -Path $Repository
    • Related Articles

    • PowerShell Universal Service crashes on startup after an upgrade to 1.4.6

      Version: PowerShell Universal 1.4.6 Issue The PowerShell Universal service will crash with the following error in Event Viewer.  Application: Universal.Server.exe CoreCLR Version: 4.700.19.56402 .NET Core Version: 3.1.0 Description: The process was ...
    • KB0011 - Are licenses different between Production, QA and Test/Development servers?

      Update January 24th, 2023 Adam recently summarized the Developer's license per below: The only real limitation on the developer license is that it cannot be accessed remotely. The server is only available on loopback when using the dev license. If ...
    • KB0069 - PowerShell Universal Startup Process

      Purpose The purpose of this document is to outline the steps that PowerShell Universal takes when starting up. Process 1. Insert current product version and install date in database Updates the database with a record about the current product version ...
    • KB0074 - Connecting to PSU API w/Windows Auth

      Scope This article applies only to PSU environments where Windows Authentication[1] is enabled and known to be working[2]. Problem You are not able to interact with the PSU instance using the Invoke-WebRequest PowerShell cmdlet even though logging in ...
    • KB0073 - Resetting PowerShell Universal configuration data

      Scope The scope of this document is applicable to all PowerShell Universal installations. Problem Incorrect configuration causes PowerShell Universal to behave in a manner that prevents the user from fixing the configuration. This can include ...