Powershell: 'Add-Type' doesn't work in applications that host powershell

Created on 13 May 2019  路  7Comments  路  Source: PowerShell/PowerShell

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.

Area-Cmdlets-Utility Area-Maintainers-Build Resolution-Fixed

Most helpful comment

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

All 7 comments

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

Admit it, you just want a PSWordCloud Azure function endpoint :joy:

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

@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.

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

:tada:This issue was addressed in #10587, which has now been successfully released as v7.0.0-preview.5.:tada:

Handy links:

:tada:This issue was addressed in #10587, which has now been successfully released as v6.2.4.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings