invoke powershell with .net

PowerShell script exchange

invoke powershell with .net

Veeam Logoby pengjun721 » Mon Jun 25, 2012 9:59 am

hello

First , use of VS2010 to build a project, reference System.Management.Automation.dll,
and then call the Veeam cmdlet:"Get-VBRBackup" and "Get-VBRServer" returned values are correct, :D
but the implementation of "Find-VBRResourcePool" access resource pool, value iempty :cry:

Code: Select all
            using (Runspace runspace = RunspaceFactory.CreateRunspace())
            {
                runspace.Open();

                //Create Pipeline           
                using (Pipeline pipeline = runspace.CreatePipeline())
                {
                    pipeline.Commands.AddScript("Add-PSSnapin VeeamPSSnapIn");
                    pipeline.Commands.AddScript("$rp = Get-VBRBackup | where {$_.JobName -eq 'BackupJob1'} | Get-VBRRestorePoint|where {$_.VMName -eq 'vm--test-Win2K3R2wSP2'}");
                    pipeline.Commands.AddScript("$server = Get-VBRServer | where {$_.Name -eq 'hke-ssvr1.dyxnet.com'} ");                   
                    pipeline.Commands.AddScript("$server ");//can get data
                    pipeline.Commands.AddScript("$res = Find-VBRResourcePool -Server ($server) | where {$_.Name -eq 'vctest2'} ");
                    pipeline.Commands.AddScript("$res");//empty
                    pipeline.Commands.AddScript("$datast = Find-VBRDatastore -Server ($server) | where {$_.Name -eq 'stogp1_m800tnas1'}");
                    pipeline.Commands.AddScript("Start-VBRRestoreVM -ResotePoint $rp -Server ($server) -ResourcePool ($res) -Datastore ($datast) -VMName 'vm--test-Win2K3R2wSP2' -DiskType 'thin' -PowerUp $TRUE");

                    //out-string
            pipeline.Commands.Add("Out-String");

                    //get return string
            Collection<PSObject> results = pipeline.Invoke();

                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (PSObject obj in results)
                    {
                        stringBuilder.AppendLine(obj.ToString());
                    }
                    runspace.Close();
                    return stringBuilder.ToString();//return
                }
            }


use "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" or "veeam in powershell toolkit" to call this cmdlet,return value is right
Only when the call with System.Management.Automation.dll to call "Find-VBRResourcePool" or " Find-VBRDatastore", the value is null
:cry: :cry: :cry: :cry:
pengjun721
Lurker
 
Posts: 2
Liked: never
Joined: Mon Jun 25, 2012 6:42 am
Full Name: pengjun

Re: invoke powershell with .net

Veeam Logoby Sethbartlett » Mon Jun 25, 2012 1:04 pm

And you stated that this line works outside of doing it through .net:

Code: Select all
Find-VBRResourcePool -Server ($server) | where {$_.Name -eq 'vctest2'}


This should work, I don't see why it wouldn't. It's not giving any errors or anything and it just runs smoothly?
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
Sethbartlett
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: invoke powershell with .net

Veeam Logoby pengjun721 » Tue Jun 26, 2012 2:19 pm

thaks for your help :D :D
oh, I check the Help document :the "Find-VBRResourcePool" cmdlet "Server" parameter is not acceptable "Pipeline
Input" way to pass values :shock:

Find-VBRResourcePool
This cmdlet returns a list of all resource pools on the specified ESX(i) host.
Syntax
Find-VBRResourcePool [-Server] <CHost>
Parameters :
Parameter Description Required Pipeline Input
Server Specify an object of the ESX(i) host True False


so...How to call it in. NET :(

I try to use the following method to carry out, the result still empty,code:

Code: Select all
 Pipeline pipeline_rp = runspace.CreatePipeline();
                    Command cmd_rp = new Command("Find-VBRResourcePool");
                    cmd_rp.Parameters.Add("Server", ps_vbrserver);
                    pipeline_rp.Commands.Add(cmd_rp);
                    Collection<PSObject> vbrResourcePools = pipeline_rp.Invoke();

                    //find vctest2 ResourcePool
            IEnumerable<PSObject> find_vctest2 = from ps in vbrResourcePools
                                                                              where ps.Members["Name"].Value.ToString() == "vctest2"
                                                                              select ps;

                    //save the first Resource Pool                   
           if (find_vctest2.Count() == 0)//it's empty
                        throw new Exception("can't not find VBRResourcePool");
                    PSObject ps_vbrResourcePool = find_vctest2.ElementAt(0);
pengjun721
Lurker
 
Posts: 2
Liked: never
Joined: Mon Jun 25, 2012 6:42 am
Full Name: pengjun

Re: invoke powershell with .net

Veeam Logoby Sethbartlett » Thu Jun 28, 2012 1:28 am

I guess one important thing, have it output ALL variables to a file to see if $Rp is getting data, etc. You could try putting them on one line. So these lines:

Code: Select all
                    pipeline.Commands.AddScript("$server = Get-VBRServer | where {$_.Name -eq 'hke-ssvr1.dyxnet.com'} ");                   
                    pipeline.Commands.AddScript("$server ");//can get data
                    pipeline.Commands.AddScript("$res = Find-VBRResourcePool -Server ($server) | where {$_.Name -eq 'vctest2'} ");


Turn this into:

Code: Select all
pipeline.Commands.AddScript("$server = Get-VBRServer | where {$_.Name -eq 'hke-ssvr1.dyxnet.com'};$res = Find-VBRResourcePool -Server ($server) | where {$_.Name -eq 'vctest2'} ")


This puts it all on one line. You could also maybe try:

Code: Select all
 pipeline.Commands.AddScript("$res = Find-VBRResourcePool -Server (Get-VBRServer | where {$_.Name -eq 'hke-ssvr1.dyxnet.com'}) | where {$_.Name -eq 'vctest2'} ");
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
Sethbartlett
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 6 guests