How do I make use of New-ScriptFileInfo PrivateData?

627 views Asked by At

I've been using a PSD1 file, named the same as the script file (e.g. Foo.ps1 --> Foo.psd1) to store settings for my script. I was thinking that I might able to make use of the script's PrivateData section to store this information, which would be contained in the InstallScriptInfos folder when the script is published.

The New-ScriptFileInfo command has a string parameter named PrivateData, which I expected to create a section similar to what exists in a PSD1 file (abbreviated for brevity):

PrivateData = @{
    PSData = @{
        # Prerelease = ''
        # Tags = @()
        # LicenseUri = ''
        # ProjectUri = ''
        # IconUri = ''
        # ReleaseNotes = ''    
    }
}

When creating a new script file, however, setting this parameter:

PS> New-ScriptInfo -Path .\Foo.ps1 -Description 'lorem ipsum' -PrivateData @{Key='Value'}

results in a string:

<#PSScriptInfo    
.VERSION 1.0
.GUID 4652dc63-6433-494a-8dcb-55cec29191d4
.AUTHOR [email protected]
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES 
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA System.Collections.Hashtable
#>
<# 
.DESCRIPTION 
lorem ipsum
#> 
Param()

If this script is published to a repository:

Publish-Script -Path .\Foo.ps1 -Repository 'MyRepository'

The resulting InstallScriptInfos\foo_InstalledScriptInfo doesn't contain this piece of metadata:

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>Microsoft.PowerShell.Commands.PSRepositoryItemInfo</T>
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="Name">foo</S>
      <S N="Version">1.0</S>
      <S N="Type">Script</S>
      <S N="Description">lorem ipsum</S>
      <S N="Author">[email protected]</S>
      <Obj N="CompanyName" RefId="1">
        <TN RefId="1">
          <T>System.Management.Automation.PSCustomObject</T>
          <T>System.Object</T>
        </TN>
      </Obj>
      <Nil N="Copyright" />
      <Ref N="PublishedDate" RefId="1" />
      <Obj N="InstalledDate" RefId="2">
        <DT>2020-10-08T09:28:45.542507-05:00</DT>
        <MS>
          <Obj N="DisplayHint" RefId="3">
            <TN RefId="2">
              <T>Microsoft.PowerShell.Commands.DisplayHintType</T>
              <T>System.Enum</T>
              <T>System.ValueType</T>
              <T>System.Object</T>
            </TN>
            <ToString>DateTime</ToString>
            <I32>2</I32>
          </Obj>
        </MS>
      </Obj>
      <Nil N="UpdatedDate" />
      <Nil N="LicenseUri" />
      <Nil N="ProjectUri" />
      <Nil N="IconUri" />
      <Obj N="Tags" RefId="4">
        <TN RefId="3">
          <T>System.Object[]</T>
          <T>System.Array</T>
          <T>System.Object</T>
        </TN>
        <LST>
          <S>PSScript</S>
        </LST>
      </Obj>
      <Obj N="Includes" RefId="5">
        <TN RefId="4">
          <T>System.Collections.Hashtable</T>
          <T>System.Object</T>
        </TN>
        <DCT>
          <En>
            <S N="Key">DscResource</S>
            <Obj N="Value" RefId="6">
              <TNRef RefId="3" />
              <LST />
            </Obj>
          </En>
          <En>
            <S N="Key">Function</S>
            <Ref N="Value" RefId="6" />
          </En>
          <En>
            <S N="Key">RoleCapability</S>
            <Ref N="Value" RefId="6" />
          </En>
          <En>
            <S N="Key">Workflow</S>
            <Ref N="Value" RefId="6" />
          </En>
          <En>
            <S N="Key">Command</S>
            <Ref N="Value" RefId="6" />
          </En>
          <En>
            <S N="Key">Cmdlet</S>
            <Ref N="Value" RefId="6" />
          </En>
        </DCT>
      </Obj>
      <Nil N="PowerShellGetFormatVersion" />
      <Nil N="ReleaseNotes" />
      <Obj N="Dependencies" RefId="7">
        <TNRef RefId="3" />
        <LST />
      </Obj>
      <S N="RepositorySourceLocation">/Volumes/PowerShellRepository</S>
      <S N="Repository">MyRepository</S>
      <S N="PackageManagementProvider">NuGet</S>
      <Obj N="AdditionalMetadata" RefId="8">
        <TNRef RefId="1" />
        <MS>
          <S N="description">lorem ipsum</S>
          <S N="requireLicenseAcceptance">False</S>
          <S N="isLatestVersion">False</S>
          <S N="isAbsoluteLatestVersion">False</S>
          <S N="tags">PSScript</S>
          <S N="developmentDependency">False</S>
          <B N="IsPrerelease">false</B>
        </MS>
      </Obj>
      <S N="InstalledLocation">/Users/craig/.local/share/powershell/Scripts</S>
    </MS>
  </Obj>
</Objs>

What is the purpose of this parameter?

3

There are 3 answers

0
craig On BEST ANSWER

This was easier than expected:

<#PSScriptInfo
.VERSION 0.0.0
.GUID 8249e67f-497d-4cef-aed1-4b2ccf56f974
.AUTHOR First Last
.COMPANYNAME 
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES 
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA @{ Foo=123; Bar=$true; Name='lorem' }
#>

<#
.DESCRIPTION 
lorem ipsum
#>

$Info = Test-ScriptFileInfo $PSCommandPath
Invoke-Expression $Info.PrivateData

Name                           Value
----                           -----
Foo                            123
Bar                            True
Name                           lorem
0
PowerShellGuy On

The purpose of that parameter, is to store informational metadata about the actual module, which is used by the gallery. ctrl + F privatedata

For instance, if you were to add some tags to your privatedata, one of them being "banana" then another user could do this to find your module

 Find-Script -Tag "banana"

source

0
ErikW On

I was able to get it working like this

new-scriptfileinfo header snippet:

<#
.PRIVATEDATA
Foo=123
Bar=$true
Name='lorem'
#>

accessing privatedata from within the script:

$PDString = (Test-ScriptFileInfo -Path $PSCommandPath).PrivateData
$PD = $PDString | ConvertFrom-StringData
$PD.Foo