locked
Read PSScriptInfo RRS feed

  • Question

  • Hi

    I have updated my ps1 files with a PSscripinfo like:

    <#PSScriptInfo
    
    .VERSION 19.0.211.8
    
    .GUID 93cff3b2-1891-4b62-94a1-9f7a31a33639
    …
    #>
    

    But how do i read this from the script itself? 

    I would like to get the version number for a log.

    Thanks

    Kim

    Tuesday, July 30, 2019 8:01 AM

Answers

  • You can read the sciptinfo block into variables (object) like this>

    <#PSScriptInfo
    
    .VERSION 1.0
    
    .GUID 6099dec4-c781-4ed1-bfc6-9530cacb2869
    
    .AUTHOR pattif@contoso.com
    
    .COMPANYNAME
    
    .COPYRIGHT
    
    .TAGS
    
    .LICENSEURI
    
    .PROJECTURI
    
    .ICONURI
    
    .EXTERNALMODULEDEPENDENCIES 
    
    .REQUIREDSCRIPTS
    
    .EXTERNALSCRIPTDEPENDENCIES
    
    .RELEASENOTES
    
    
    .PRIVATEDATA
    
    #>
    
    <# 
    
    .DESCRIPTION 
     My test script file description goes here 
    
    #> 
    Param()
    
    Test-ScriptFileInfo  $MyInvocation.InvocationName | fl
    

    The "Test-ScriptFIleInfo" returns the "PsScriptInfo" settings.

    PS D:\scripts> d:\Test\Temp-Scriptfile.ps1
    
    
    Name                       : Temp-Scriptfile
    Version                    : 1.0
    Guid                       : 6099dec4-c781-4ed1-bfc6-9530cacb2869
    Path                       : D:\Test\Temp-Scriptfile.ps1
    ScriptBase                 : D:\Test
    Description                : My test script file description goes here
    Author                     : pattif@contoso.com
    CompanyName                :
    Copyright                  :
    Tags                       :
    ReleaseNotes               :
    RequiredModules            :
    ExternalModuleDependencies :
    RequiredScripts            :
    ExternalScriptDependencies :
    LicenseUri                 :
    ProjectUri                 :
    IconUri                    :
    DefinedCommands            :
    DefinedFunctions           :
    DefinedWorkflows           :
    PrivateData                :
    
    
    You can use the CmdLet to return the info block from any script file.


    \_(ツ)_/


    • Edited by jrv Tuesday, July 30, 2019 9:06 AM
    • Marked as answer by Kim CarlsenMVP Tuesday, July 30, 2019 11:18 AM
    Tuesday, July 30, 2019 9:04 AM

All replies

  • The information is written into comments and will not be executed while the script runs, you need to save the same either in a variable or sting so that later on or in some other script you can capture the same.

    $ScriptInfo = @"
    PSScriptInfo
    
    .VERSION 19.0.211.8
    
    .GUID 93cff3b2-1891-4b62-94a1-9f7a31a33639
    …
    "@
    
    
    Write-Output $ScriptInfo

    Tuesday, July 30, 2019 8:36 AM
  • You can read the sciptinfo block into variables (object) like this>

    <#PSScriptInfo
    
    .VERSION 1.0
    
    .GUID 6099dec4-c781-4ed1-bfc6-9530cacb2869
    
    .AUTHOR pattif@contoso.com
    
    .COMPANYNAME
    
    .COPYRIGHT
    
    .TAGS
    
    .LICENSEURI
    
    .PROJECTURI
    
    .ICONURI
    
    .EXTERNALMODULEDEPENDENCIES 
    
    .REQUIREDSCRIPTS
    
    .EXTERNALSCRIPTDEPENDENCIES
    
    .RELEASENOTES
    
    
    .PRIVATEDATA
    
    #>
    
    <# 
    
    .DESCRIPTION 
     My test script file description goes here 
    
    #> 
    Param()
    
    Test-ScriptFileInfo  $MyInvocation.InvocationName | fl
    

    The "Test-ScriptFIleInfo" returns the "PsScriptInfo" settings.

    PS D:\scripts> d:\Test\Temp-Scriptfile.ps1
    
    
    Name                       : Temp-Scriptfile
    Version                    : 1.0
    Guid                       : 6099dec4-c781-4ed1-bfc6-9530cacb2869
    Path                       : D:\Test\Temp-Scriptfile.ps1
    ScriptBase                 : D:\Test
    Description                : My test script file description goes here
    Author                     : pattif@contoso.com
    CompanyName                :
    Copyright                  :
    Tags                       :
    ReleaseNotes               :
    RequiredModules            :
    ExternalModuleDependencies :
    RequiredScripts            :
    ExternalScriptDependencies :
    LicenseUri                 :
    ProjectUri                 :
    IconUri                    :
    DefinedCommands            :
    DefinedFunctions           :
    DefinedWorkflows           :
    PrivateData                :
    
    
    You can use the CmdLet to return the info block from any script file.


    \_(ツ)_/


    • Edited by jrv Tuesday, July 30, 2019 9:06 AM
    • Marked as answer by Kim CarlsenMVP Tuesday, July 30, 2019 11:18 AM
    Tuesday, July 30, 2019 9:04 AM
  • Thanks the $myinvocation did the trick
    Tuesday, July 30, 2019 11:18 AM