Wednesday, September 18, 2013

microsoft system center 2012 sp1 – used experience 2 vmm powershell

1.todo

program with vmm powershell

2.analyze

vmm powershell:

wmic process where caption="powershell.exe" get caption,commandline /value




output:

Caption=powershell.exe
CommandLine="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit
-Command $ep = Get-ExecutionPolicy; if ( $ep -eq ([Microsoft.PowerShell.Executio
nPolicy]::Restricted) ) { Set-ExecutionPolicy RemoteSigned -Scope Process -Force
} "Import-Module 'D:\Program Files\Microsoft System Center 2012\Virtual Machine
Manager\Bin\psModules\virtualmachinemanager\virtualmachinemanager.psd1'; cd $ho
me; $host.UI.RawUI.WindowTitle = 'Windows PowerShell - Virtual Machine Manager';
$vmmserver_VAR=Get-SCVMMServer localhost -UserRoleName '管理员';"





desktop powershell:

power shell: wmic process where caption="powershell.exe" get cap
tion,commandline /value




output:

Caption=powershell.exe
CommandLine="C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe"



3.test cmdline


3.1.in desktop powershell

PS C:\Users\administrator.CLOUD> New-SCVirtualMachine
New-SCVirtualMachine : 无法将“New-SCVirtualMachine”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼
写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 1
+ New-SCVirtualMachine
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (New-SCVirtualMachine:String) [], CommandNotFoundException




3.2.desktop powershell build vmm environment

PS C:\Users\administrator.CLOUD> $ep = Get-ExecutionPolicy;
PS C:\Users\administrator.CLOUD> if ( $ep -eq ([Microsoft.PowerShell.ExecutionPolicy]::Restricted) ) { Set-ExecutionPoli
cy RemoteSigned -Scope Process -Force }
PS C:\Users\administrator.CLOUD> Import-Module 'D:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\Bi
n\psModules\virtualmachinemanager\virtualmachinemanager.psd1'
; cd $home; $host.UI.RawUI.WindowTitle = 'Windows PowerShel
l - Virtual Machine Manager'
; $vmmserver_VAR=Get-SCVMMServer localhost -UserRoleName '管理员';
PS C:\Users\administrator.CLOUD>

3.3 continue test

PS C:\Users\administrator.CLOUD> New-SCVirtualMachine
New-SCVirtualMachine : 无法使用指定的命名参数解析参数集。
所在位置 行:1 字符: 1
+ New-SCVirtualMachine
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-SCVirtualMachine],ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.NewVmCmdlet



3.4 result


pass this step!


4. c# code test


4.1 todo


c# code call powershell script to create virtual machine instance


4.2 Form1.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestVMM1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public static void InvokeSystemPS(string cmd)
{
List<String> ps = new List<String>();
ps.Add("Set-ExecutionPolicy Unrestricted");
ps.Add("Set-ExecutionPolicy -ExecutionPolicy Unrestricted");
ps.Add("& " + cmd);
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
foreach (var scr in ps)
{
pipeline.Commands.AddScript(scr);
}
try
{
pipeline.Invoke();//Execute the ps script
runspace.Close();
}
catch (Exception ex)
{
throw ex;
}
}

public static Pipeline prepareInvokeVMMPS(List<String> psOther = null)
{
RunspaceConfiguration rconfig = RunspaceConfiguration.Create();
PSSnapInException Pwarn = new PSSnapInException();

Runspace runspace = RunspaceFactory.CreateRunspace(rconfig);
runspace.Open();

RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
scriptInvoker.Invoke(@"Set-ExecutionPolicy Unrestricted -Scope Process -Force;");
scriptInvoker.Invoke(@"Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Force;");
scriptInvoker.Invoke(@"Import-Module -Name virtualmachinemanager;");


//pipeline.Commands.AddScript(@"$ep = Get-ExecutionPolicy;");
//pipeline.Commands.AddScript(@"if ( $ep -eq ([Microsoft.PowerShell.ExecutionPolicy]::Restricted) ) { Set-ExecutionPolicy RemoteSigned -Scope Process -Force };");
//pipeline.Commands.AddScript("Set-ExecutionPolicy Unrestricted -Scope Process -Force;");
//pipeline.Commands.AddScript("Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Force;");
//pipeline.Commands.AddScript(@"Import-Module -Name virtualmachinemanager;");
//pipeline.Commands.AddScript(@"$vmmserver_VAR=Get-SCVMMServer localhost -UserRoleName '管理员';"); // Administrator
if ((psOther != null) && psOther.Count > 0)
{
Pipeline pipeline = runspace.CreatePipeline();
foreach (var i in psOther)
{
pipeline.Commands.AddScript(i);
}
var results = pipeline.Invoke();
}
Pipeline result;
try
{
result = runspace.CreatePipeline();
if ((psOther != null) && psOther.Count > 0)
{
foreach (var i in psOther)
{
result.Commands.AddScript(i);
}
}
}
catch (Exception ex)
{
throw ex;
}
return result;
}

public static Collection<PSObject> testCallGetVm01()
{
Collection<PSObject> result;
Pipeline pipe = prepareInvokeVMMPS();
try
{
if (pipe == null)
return null;
//Get-VM -Name vm001
Command cmd = new Command("Get-VM");
cmd.Parameters.Add("Name", "VM01");
pipe.Commands.Add(cmd);
result = pipe.Invoke();
}
catch (Exception ex)
{
throw ex;
}
return result;
}

private void CreateVm(string vmname)
{
Collection<PSObject> result;
List<string> otherparams = new List<string>();
Pipeline pipe = prepareInvokeVMMPS(otherparams);
try
{
if (pipe == null)
return;
Command cmd = new Command("D:/share/soft/createVM.ps1");
cmd.Parameters.Add(@"VMNAME", vmname);
pipe.Commands.Add(cmd);
result = pipe.Invoke();
}
catch (Exception ex)
{
throw ex;
}
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
testCallGetVm01();
}

private void button2_Click(object sender, EventArgs e)
{
CreateVm("VM02");
}
}
}



4.3 createVM.ps1

param (
[string]$VMNAME = "VM01"
)

$VHD = Get-SCVirtualHardDisk -Name "empty-small.vhd"
$VMHost = Get-SCVMHost -ComputerName "SCVMM.cloud.com"
New-SCVirtualMachine -Name $VMNAME -VirtualHardDisk $VHD -VMHost $VMHost -Path "D:\virtvm" -RunAsynchronously

No comments: