PowerShell and LuaInterface.dll - c#

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")

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?

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.

C# in PowerShell: Language Mode Not Supported

I am trying to get a very simple script to work in PowerShell, and it keep popping back with the following error:
Add-Type : Cannot add type. Definition of new types is not supported in this language mode.
At C:\Users\jdkin_000.ATHENA\cs_init.ps1:16 char:1
+ Add-Type -TypeDefinition $Source -Language CSharp
Here is the script:
$Source = #"
using System;
namespace cs1
{
public class Program
{
public static void Main()
{
Console.WriteLine("Hello world!");
}
}
}
"#
Add-Type -TypeDefinition $Source -Language CSharp
Any suggestions on how to get this script to work on the exact device that is throwing back the error. The script and similar scripts work on other machines, but when the script contains C#, this machine just doesn't want to play fair.
Try running this:
$ExecutionContext.SessionState.LanguageMode
Here is the link explaining the LanguageMode. I assume you are in a mode that restricts the ability to define new types.
https://technet.microsoft.com/en-us/library/dn433292.aspx
Edit: Based on the Windows RT 8.1 OS, (
Windows RT Powershell (PermissionDenied) on New-Object)
It doesn't appear that it can be done.
Powershell also may switch to ConstrainedLanguage mode when there is no space available on system drive.

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

Why PowerShell does not 'understand' the instructions of a response file?

I have created the following response file (I'm following the examples found in this article: Working with the C# 2.0 Command Line Compiler):
# MyCodeLibraryArgs.rsp
#
# These are the options used
# to compile MyCodeLibrary.dll
# Output target and name.
/t:library
/out:MyCodeLibrary.dll
# Location of C# files.
/recurse:*.cs
# Give me an XML doc.
/doc:myDoc.xml
Then I try to execute it using the C# Compiler (csc.exe) from the PowerShell:
csc #MyCodeLibraryArgs.rsp
Then it generates the following error:
Cannot expand the splatted variable '#MyCodeLibraryArgs'. Splatted variables
cannot be used as part of a property or array expression. Assign the result of
the expression to a temporary variable then splat the temporary variable instead.
At line:1 char:23 + csc #MyCodeLibraryArgs <<<< .rsp
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : NoPropertiesInSplatting
So, I decide to use the prompt (command-line), and it works correctly.
What is the cause (s) of this problem with PowerShell (v.3.0)?
Thanks in advance for your responses and comments.
# is a special syntax in powershell for "splatting" variables. You want to escape the # like so
csc `#MyCodeLibraryArgs.rsp
Splatting allows you to pass cmdlet arguments in a hashtable. This is convenient if you want to dynamically build the arguments you are passing. It can also be more readable if there are a lot of arguments. More info on splatting here.

Categories