Create a ScheduleDefinition object in Powershell - c#

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

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

Get input from the user using ReadLine in c# code that runs as part of PowerShell script in PowerShell ISE

I need to prompting users for input in C# part of the code, but it doesn't work for me (gets empty input automagically) when run from PowerShell ISE. Code works as expected when run from just regular PowerShell command prompt (asks for the name)
$id = get-random
$code = #"
using System;
using System.Linq;
namespace Application
{
public class Program$id
{
public static void Main()
{
Console.WriteLine("Please enter your sweet name....");
String name = Console.ReadLine();
Console.WriteLine("Hello *"+name+" %" );
}
}
}
"#
Add-type -TypeDefinition $code -Language CSharp
iex "[Application.Program$id]::Main()"
When run in ISE the output is following (notice there is no chance to provide input):
PS C:\Users\Pavithra Kathirvel\Desktop> C:\Users\Pavithra Kathirvel\Desktop\demo.ps1
Please enter your sweet name....
Hello * %
As a beginner start with the basics in C# and powershell. If you are able to use one, it will be easier for you to learn the other. Also there are parts, which are in powershell faster than in c# and less code.
If you still want to execute your C# code in powershell, have a look at this link, it will run the script from a .cs file
Run a C# .cs file from a PowerShell Script
Also here is a really good tutorial for using c# in powershell:
https://blog.adamfurmanek.pl/2016/03/19/executing-c-code-using-powershell-script/

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.

Translating C# to PowerShell in InterIMAP

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');

Categories