Call class file method from NuGet package using Powershell

1

I'm trying to create a nuget package from my class libary. So far I managed to create and install the package in a test project, but I also want to create a package manager command. My NuGet package is created with the NuGet Package Explorer.

NuGet Package structure

structure

NuGet files content

init.ps1

param($installPath, $toolsPath, $package) 
Import-Module (Join-Path $toolsPath MyNuGetCommands.psm1)

MyNuGetCommands.psm1

function Hello($name, $city) 
{ 
    Write-Host (‘Hello ‘ + $name + ‘. See you soon in ‘ + $city + ‘.’) 

    $lib = [Reflection.Assembly]::LoadFile("\\lib\EF.XML.dll")
    $obj = new-object Parser
    $result = $obj.UpdateXml()
}

Export-ModuleMember Hello

Register-TabExpansion ‘Hello’ @{  
‘name’ = { "MSFTees", "MVPs", "My friends" }; 
‘city’ = { "Redmond", "Seattle", "Bellevue", "Duvall" }; 
}  

I found the Hello function on the net and it worked in my project so I thougt lets add some lines found here to call my C# method. When I call the Hello function in my test project I get this errors:

Error 1

Exception calling "LoadFile" with "1" argument(s): "Cannot find the network path. (Exception from HRESULT: 0x80070035)" At \svr\redirectedfolders\Stage\my documents\visual studio 2015\Projects\Test\packages\XML.EF.1.0.0\tools\MyNuGetCommands.psm1:5 char:43 + $lib = [Reflection.Assembly]::LoadFile <<<< ("\lib\EF.XML.dll") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

Error 2

New-Object : Cannot find type [Parser]: make sure the assembly containing this type is loaded. At \svr\redirectedfolders\Stage\my documents\visual studio 2015\Projects\Test\packages\XML.EF.1.0.0\tools\MyNuGetCommands.psm1:6 char:22 + $obj = new-object <<<< Parser + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

Error 3

You cannot call a method on a null-valued expression. At \svr\redirectedfolders\Stage\my documents\visual studio 2015\Projects\Test\packages\XML.EF.1.0.0\tools\MyNuGetCommands.psm1:7 char:29 + $result = $obj.UpdateXml <<<< () + CategoryInfo : InvalidOperation: (UpdateXml:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

So I think in the above errors obj is null but how can I fix this (I am not sure if LoadFile path is correct)? I did run set-executionpolicy RemoteSigned in Powershell.

Parser.cs file structure in EF.XML.dll

public class Parser
{       
    public void UpdateXml()
    {
        //code
    }
}

This is the code to I use to call the method from a .cs file (which works but I want to call this from my Powershell Module:

 var parser = new EF.XML.Parser();
 parser.UpdateXml();
c#
powershell
asked on Stack Overflow Oct 19, 2015 by Sybren • edited May 23, 2017 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0