Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell function returning instantiated object...kind of?

Tags:

powershell

I'm rather new to Powershell and am working on setting up my profile.ps1 file. I have a few managed DLLs that I use often to maintain processes throughout the day which I'd like to be able to load up with quick function calls. So I created this function in my ps1 file:

function LoadSomeDll
{
    [System.Reflect.Assembly]::LoadFrom("c:\wherever\SomeLib.dll")
    return new-object "SomeLib.SomeObject"
}

Then, in Powershell, I do this:

PS > $myLibInstance = LoadSomeDll

The problem is that $myLibInstance, though it appears to be loaded, doesn't behave the way I expect it to or if it would if I explicitly load it without the function. Say SomeLib.SomeObject has a public string property "ConnectionString" that loads itself (from the registry, yuck) when the object is constructed.

PS > $myLibInstance.ConnectionString
//Nothing returned

But, if I do it without the function, like this:

PS > [System.Reflect.Assembly]::LoadFrom("c:\wherever\SomeLib.dll")
PS > $myOtherLibInstance = new-object "SomeLib.SomeObject"

I get this:

PS > $myOtherLibInstance.ConnectionString
StringValueOfConnectionStringProperty

Why does this happen? Is there any way that I can return an instantiated new-object from a Powershell function?

Thanks in advance.

like image 776
AJ. Avatar asked Jul 02 '09 13:07

AJ.


People also ask

What does a PowerShell function return?

In PowerShell, the results of each statement are returned as output, even without a statement that contains the Return keyword. Languages like C or C# return only the value or values that are specified by the return keyword. Note.

How do I get the return value of a PowerShell script?

The proper reliable way to return a value from a script function is through setting a variable. Relying on the position of output is prone to breakage in the future if someone for example adds new output to the streams; Write-Output/Write-Warning/Write-Verbose, etc...

How do you return an array from a function in PowerShell?

First, use the array sub-expression operator @( ... ). This operator returns the result of one or more statements as an array. If there is only one item, the array has only one member. Use Write-Output with the switch -NoEnumerate to prevent PowerShell from un-rolling the array.


1 Answers

The problem you're running into is that your original function is returning an array of objects, not a single object.

One of the tricks in PowerShell is understanding that in a function, every statement which evaluates no a non-void value will be written to the pipeline. The return "value" of a function is simply the contents of the pipeline.

The call to LoadFrom returns an assembly. So the actual return of the function LoadSomeDll is an array containing an assembly and an instance of your object. You're actually calling ConnectionString on the type Object[] and hence it silently fails.

Try switching the function to the following. I intentionally left off the keyword return because it's confusing in the context of powershell.

function LoadSomeDll
{
    [System.Reflect.Assembly]::LoadFrom("c:\wherever\SomeLib.dll") | out-null
    new-object "SomeLib.SomeObject"
}
like image 196
JaredPar Avatar answered Oct 09 '22 14:10

JaredPar