Translating C# to PowerShell in InterIMAP - c#

I'm having a problem with using C# in my PowerShell script. The library I'm trying to use is InterIMAP
Specifically, I am trying to convert this constructor:
using InterIMAP;
IMAPConfig config = new IMAPConfig("server","username","password", true, false, "INBOX");
The PowerShell script I have so far:
$loadLib =[Reflection.Assembly]::LoadFile('C:\Users\......\InterIMAP.dll);
$config= [InterIMAP.IMAP.IMAPConfig]::IMAPConfig("imap.gmail.com", $user, $password, $true, $true, "INBOX");
I think the issue I'm having is because the IMAP class has a pointer to the IMAPConfig class: http://interimap.codeplex.com/SourceControl/latest#InterIMAP/InterIMAP/Client/IMAP.cs
I must be doing something wrong. Any help appreciated

In your PowerShell code, you're accessing IMAPConfig() as a static method of the IMAPConfig class, which I'm guessing is not valid. In order to use an object constructor in PowerShell, you need to use the New-Object cmdlet.
The -TypeName parameter specifies the .NET class that you want to instantiate. The -ArgumentList parameter accepts an array of arbitrary System.Object objects that represent the constructor parameters.
$config = New-Object -TypeName InterIMAP.IMAP.IMAPConfig -ArgumentList #('imap.gmail.com', $User, $Password, $true, $true, 'INBOX');

Related

How to use C# interfaces and extension methods in Powershell?

I am trying to use some C# interfaces and extension methods in Powershell. I have loaded necessary binaries at the beginning of the script and written following piece of code:
$environmentsPath = Resolve-Path (Join-Path -Path $PSScriptRoot -ChildPath '..\Deploy.V2\environments')
$configFolder = Join-Path -Path $environmentsPath -ChildPath '.\Stage\configFiles'
$builder = New-Object Microsoft.Extensions.Configuration.ConfigurationBuilder
[Microsoft.Extensions.Configuration.IConfigurationBuilder]$interface = ([Microsoft.Extensions.Configuration.IConfigurationBuilder]$builder)
$interface = [Microsoft.Extensions.Configuration.FileConfigurationExtensions]::SetBasePath($interface, $configFolder)
I am getting following error while trying to run above code:
Cannot convert argument "builder", with value: "Microsoft.Extensions.Configuration.ConfigurationBuilder", for "SetBasePath" to type
"Microsoft.Extensions.Configuration.IConfigurationBuilder": "Cannot convert the "Microsoft.Extensions.Configuration.ConfigurationBuilder" value of type
"Microsoft.Extensions.Configuration.ConfigurationBuilder" to type "Microsoft.Extensions.Configuration.IConfigurationBuilder"."
Even though I have tried to convert object to an interface implementation (at line 4 of the above script) I still see in debugger that it has Microsoft.Extensions.Configuration.ConfigurationBuilder type and the error occurs. I have tried several ways to cast the object but without a success.
Could someone explain me please how can I pass C# function argument being interface implementation in Powershell?

PowerShell and LuaInterface.dll

LuaInterface
Here is an example on c#
Im newbie. How is properly to call this dll?
I was trying this:
[System.Reflection.Assembly]::LoadFile("E:\\lua\\LuaInterface.dll")
$Lua = new-object LuaInterface.Lua # Here IntelliSense see class lua after dot
$lua.DoString("local a=5") # Here IntelliSense see all methods after dot
And this:
Add-Type -path "E:\lua\LuaInterface.dll"
[LuaInterface.Lua]::DoString("local a=5")
But unsuccessfully. Pls, show me example of "3+2" from LuaInterface.
Methods from class Lua PS somehow cant see.
On screenshot powershell can see methods from luaDLL class. But there needed always one more parameter luastate.
You're really close, but :: is only for static member access.
I got the following working in a 32-bit console (PowerShell 5.1):
# Load LuaInterface
Add-Type -Path path\to\luainterface.dll
# Create Lua instance
$lua = [LuaInterface.Lua]::new()
# Set global variable values
$lua['a'] = 2
$lua['b'] = 3
# return result of `a+b`
$lua.DoString("return a+b")

Can I specify a dll from which to use New-Object in Powershell?

I have a scenario where by I need to New-Object a type but there are multiple types with the same name and namespace. I can't find how New-Object decides which dll to create the type from when it exists in multiple places. Is it possible to specify? Ideally I'd like to pass the dll name to the New-Object but there doesn't appear to be an option for this.
Specify the full assembly-qualified type name:
New-Object -TypeName 'MyType, AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=abcdef0123456789'
This works with type literals as well:
[MyType,AssemblyName,Version=1.0.0.0,Culture=neutral,PublicKeyToken=abcdef0123456789]::new()
You can easily obtain the fully qualified name of an assembly from disk with AssemblyName.GetAssemblyName():
# Load the assembly
$AssemblyPath = '.\MyAssembly.dll'
Add-Type -Path $AssemblyPath
# Fetch the assembly's name
$AssemblyName = [System.Reflection.AssemblyName]::GetAssemblyName((Resolve-Path $AssemblyPath).Path)
# Construct full type name
$TypeName = 'MyType'
$AssemblyQualifiedTypeName = $TypeName,$AssemblyName -join ', '
# Instantiate and object of this type
$MyObject = New-Object -TypeName $TypeName
#or
$MyObject = ($TypeName -as [Type])::new()
I think you can try to use LoadFrom:
[System.Reflection.Assembly]::LoadFrom(dllPath)
Then PS should first look into it when using New-Object FullyQualifiedType .
EDIT :
Alternatively, it seems there is an option to specify an DLL assembly with Add-Type :
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-5.1
Add-Type -AssemblyName "yourAssembly"
Then, the type will be available in the current PowerShell session.

Unable to call a method inside a property inside a class from a .NET DLL in Powershell

I have added a dll to my script and declared a new instance of a class from said dll. I've been able to use all properties and methods from that class directly without errors as shown below:
$here = $PSScriptRoot
Add-Type -Path (Join-Path $here 'AudioPrecision.API.dll')
#Declare a variable type APx500
$APX = New-Object AudioPrecision.API.APx500 -ArgumentList True
#Make the window visible
$APX.Visible = "True"
#open a specific project
$APX.OpenProject($here+"\test1.approjx")
Now, It is vital to me to use a method ( Run() ) which is inside a property (Sequence) which inside my already declared class instance. I was able to access a property from said property as shown below:
($APX.Sequence.Count).Count
But when trying to access methods from that Property, it is not working. I am trying the following:
$APX.Sequence.Run()
and I keep getting the following error:
Method invocation failed because [System.MarshalByRefObject] does not contain a method named 'Run'.At C:\Users\eblacio\Documents\Hardware Validation\Audio tests automation\Powershell\Audio Precision API\AP
automation.ps1:18 char:1
+ $APX.Sequence.Run()
I've tried quite a few variants without success.
Any help will be appreciated. Thanks.

Create a ScheduleDefinition object in Powershell

I already have a powershell script that imports reports into SSRS with powershell using the ReportService2010.asmx?wsdl API.
I have issues with creating the cache options however, in particular to create a simple schedule.
The code example on the MSDN does not have a powershell example.
http://technet.microsoft.com/en-us/library/reportservice2010.reportingservice2010.createcacherefreshplan.aspx
I was hoping someone could help me how to write this example in powershell?
In particular how to create a ScheduleDefinition object.
It seems the trick is to assign a namespace and class on your New-WebServiceProxy
I had this line in my code:
$RS = New-WebServiceProxy -Uri $reportServerURI -UseDefaultCredential
Now I have changed it to this:
$RS = New-WebServiceProxy -Class 'RS' -Namespace 'RS' -Uri $reportServerURI -UseDefaultCredential
This allows me to create the ScheduleDefinition object via
$definition New-Object RS.ScheduleDefinition

Categories