PowerShell via C# Questions

PowerShell script exchange

PowerShell via C# Questions

Veeam Logoby oneillsdm » Thu Feb 20, 2014 4:20 pm

Hi,

I'm new to using Veeam... I am trying to automate the replication of VM's in our environment using PowerShell and C#... I have the PowerShell script working well but when I try to run it in C# (so that I can do batches via multithreading), I have a problem... The Veeam PS snapin does not load for me... Any other PS snapin that I try to load via C# works just fine but the Veeam one does not... Has anyone tried to do this before with any success?

Second question, I noticed that someone mentioned on a different post that you could reference some .dll files from the Veeam install to use in C#? Is there any reference material for these dll's and how they can be used in C#?

Shane...
oneillsdm
Novice
 
Posts: 3
Liked: never
Joined: Thu Feb 20, 2014 4:13 pm
Full Name: Shane O'Neill

Re: PowerShell via C# Questions

Veeam Logoby Vitaliy S. » Thu Feb 20, 2014 6:07 pm

Hello Shane,

1. What kind of error do you receive? Can you please give us a bit more details on automation you're trying to achieve with C# and PS?
2. There is no such references available.

Thank you!
Vitaliy S.
Veeam Software
 
Posts: 19558
Liked: 1102 times
Joined: Mon Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov

Re: PowerShell via C# Questions

Veeam Logoby oneillsdm » Fri Feb 21, 2014 10:37 am

Hi Vitaliy,

I don't get any error, just that I can't load the Veeam PowerShell snapin via C#...

Here is a code example... I am adding the Veeam snapin and the VMWare snapins that I also have installed... I then call them to verify that they loaded...

private void example()
{
Runspace rs = RunspaceFactory.CreateRunspace();
rs.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddScript("Add-PSSnapin VeeamPSSnapin");
ps.AddScript("Add-PSSnapin VMware.*");
ps.AddScript("Get-PSSnapin");
try
{
foreach (PSObject result in ps.Invoke())
{
Console.Write(result.ToString());
}
}
catch(Exception e)
{
rs.Close();
}
rs.Close();
}
}

This is the output that I get...

Microsoft.PowerShell.Diagnostics
Microsoft.PowerShell.Core
Microsoft.PowerShell.Utility
Microsoft.PowerShell.Host
Microsoft.PowerShell.Management
Microsoft.PowerShell.Security
Microsoft.WSMan.Management
VMware.VimAutomation.Cloud
VMware.VimAutomation.Core
VMware.VimAutomation.License
VMware.VumAutomation

The Veeam PS snapin is not loading... If I do it manually in PowerShell, it works... So I guess I am asking if anyone has tried to do it the same way as me and successed? Or is there something different about the Veeam snapin that I am missing...

Thanks!

Shane...
oneillsdm
Novice
 
Posts: 3
Liked: never
Joined: Thu Feb 20, 2014 4:13 pm
Full Name: Shane O'Neill

Re: PowerShell via C# Questions

Veeam Logoby v.Eremin » Fri Feb 21, 2014 1:17 pm

From my perspective, there are several things you can do:

1) Check whether the corresponding snap-in is loaded, indeed, or it's just not list among others.
2) I don't know for sure, but there has to be some kind of C# log, where you can check and see whether there are any issues with snap-in adding.
3) You can start from the different angle, executing C# commands from the PS. This way, you will be able to use both Veeam PSsnapin and C# commands.

Anyway, it will be appreciated if you provide a little bit more details regarding the task you're trying to achieve.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: PowerShell via C# Questions

Veeam Logoby oneillsdm » Tue Feb 25, 2014 5:24 pm

I'm feeling a bit stupid right about now... I figured out what was going wrong with this... I never updated my C# project to x64, it was using x86... So it was creating a 32-bit PS object which was then trying to load a 64-bit PS snapin... :oops:

It is working perfectly now...

Thanks!
oneillsdm
Novice
 
Posts: 3
Liked: never
Joined: Thu Feb 20, 2014 4:13 pm
Full Name: Shane O'Neill

Re: PowerShell via C# Questions

Veeam Logoby tsightler » Tue Feb 25, 2014 5:40 pm

I'm feeling the same because I thought about asking that very question and then decided that was probably too obvious. How is it always those little things that get you.
tsightler
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 4 guests