Skip to content

'Add-Type' doesn't work in applications that host powershell #9587

Closed

Description

Add-Type depends on the reference assemblies shipped with pwsh. The reference assemblies are not bundled with PowerShell SDK NuGet packages, so Add-Type doesn't work for applications that host PowerShell.

Activity

  1. added
    Issue-Question
    WG-Maintainers-Build
    and removed
    Issue-Question
    on May 13, 2019
  2. TylerLeonhardt commented on May 13, 2019

    @TylerLeonhardt
    Member

    As ^ declares, Dongbo originally discovered this in Azure Functions - where I think folks will use Add-Type a fair bit at least.

  3. vexx32 commented on May 30, 2019

    @vexx32
    Collaborator

    Admit it, you just want a PSWordCloud Azure function endpoint 😂

  4. invisibleaxm commented on Jun 18, 2019

    @invisibleaxm

    I can confirm that this issue is there in the latest version of Powershell core 6 / azure functions and is a blocker for us. Is there a known work-around to get this working on azure functions or perhaps an estimated ETA of when we think this may be addressed?

    In my scenario, I am loading a community module for cosmosdb ( https://www.powershellgallery.com/packages/CosmosDB/3.2.4.376) inside an azure powershell function. This module uses a custom type definition which yields an error when ran in azure functions only:

    [6/18/2019 3:39:54 PM] ERROR: Unable to find type [CosmosDB.ContextToken].
    [6/18/2019 3:39:54 PM] Result: ERROR: Unable to find type [CosmosDB.ContextToken].
    Exception: Unable to find type [CosmosDB.ContextToken].
    Stack: at System.Management.Automation.TypeOps.ResolveTypeName(ITypeName typeName, IScriptExtent errorPos)

    this is the custom type definition as per the community module

    if (-not ([System.Management.Automation.PSTypeName]'CosmosDB.Context').Type)
    {
        $typeDefinition = @'
    namespace CosmosDB {
        public class ContextToken
        {
            public System.String Resource;
            public System.DateTime TimeStamp;
            public System.DateTime Expires;
            public System.Security.SecureString Token;
        }
    
        public class BackoffPolicy
        {
            public System.Int32 MaxRetries;
            public System.String Method;
            public System.Int32 Delay;
        }
    
        public class Context
        {
            public System.String Account;
            public System.String Database;
            public System.Security.SecureString Key;
            public System.String KeyType;
            public System.String BaseUri;
            public CosmosDB.ContextToken[] Token;
            public CosmosDB.BackoffPolicy BackoffPolicy;
        }
    
        namespace IndexingPolicy {
            namespace Path {
                public class Index {
                    public System.String dataType;
                    public System.String kind;
                }
    
                public class IndexRange : CosmosDB.IndexingPolicy.Path.Index {
                    public readonly System.Int32 precision = -1;
                }
    
                public class IndexHash : CosmosDB.IndexingPolicy.Path.Index {
                    public readonly System.Int32 precision = -1;
                }
    
                public class IndexSpatial : CosmosDB.IndexingPolicy.Path.Index {
                }
    
                public class IncludedPath
                {
                    public System.String path;
                    public CosmosDB.IndexingPolicy.Path.Index[] indexes;
                }
    
                public class ExcludedPath
                {
                    public System.String path;
                }
            }
    
            public class Policy
            {
                public System.Boolean automatic;
                public System.String indexingMode;
                public CosmosDB.IndexingPolicy.Path.IncludedPath[] includedPaths;
                public CosmosDB.IndexingPolicy.Path.ExcludedPath[] excludedPaths;
            }
        }
    
        namespace UniqueKeyPolicy {
            public class UniqueKey {
                public System.String[] paths;
            }
    
            public class Policy
            {
                public CosmosDB.UniqueKeyPolicy.UniqueKey[] uniqueKeys;
            }
        }
    }
    '@
        Add-Type -TypeDefinition $typeDefinition
    }
    
    $PSVersionTable
    ----------------------
    Name                           Value
    ----                           -----
    PSVersion                      6.2.1
    PSEdition                      Core
    GitCommitId                    6.2.1
    OS                             Microsoft Windows 10.0.17763
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    
    -------------------------------------------------
    func --version
    2.7.1373
    
  5. TylerLeonhardt commented on Jun 18, 2019

    @TylerLeonhardt
    Member

    Alex Campos (@invisibleaxm) yep... that won't work today. Based on the code you've shown, it'd be very easy to convert those to PowerShell classes and things might just work...

    but yeah this issue needs some work to be done in PowerShell which is actually quite difficult to solve elegantly.

  6. jnevins-gcm commented on Sep 1, 2019

    @jnevins-gcm

    any news on this? kind of a deal killer for using Powershell Core

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Resolution-FixedWG-CmdletsWG-Maintainers-Build

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions