scvmm sdk之powershell(一)

时间:2023-03-09 23:43:40
scvmm sdk之powershell(一)

shell表示计算机操作系统中的壳层,与之相对的是内核,内核不能与用户直接交互,而是通过shell为用户提供操作界面,shell分为两类,一种提供命令行界面,一种提供图形界面。windows powershell第一个版本是在2006年,提供类似unix系统的命令行壳程程序。powershell是建立在.net framework基础之上的,它内置一百多种cmdlet工具,它不仅可以像传统cmd命令一样管理操作系统,还可以管理针对.net架构下开发的程序,比如system center virtual machine manager内置powershell。

下面我们来敲入两条命令,第一个获取当前系统时间,第二个获取windows以p开头的进程,第三个字母大小写转换。

scvmm sdk之powershell(一)

我们还以用工具集来连接远程计算机。第一个行获取远程计算机登录权限,第二步来检查该计算机sql server服务是否启动。

 $c=get-credential (get-credential -credential domain\administrator)
Get-WmiObject -Query "select * from win32_service where name='mssqlserver'" -computername 192.168.0.181 -credential $c

scvmm sdk之powershell(一)

那么如何用powershell来管理scvmm,这里需要导入scvmm管理模块。可以用get-vm来获取hyperv中某一台名为linux的虚机。

更多的命令工具可以调用get-command来查看。

 add-pssnapin "Microsoft.SystemCenter.VirtualMachineManager"
get-vm -name linux -vmmserver 192.168.0.223

scvmm sdk之powershell(一)

那么如果用代码去实现就需要用到System.Management.Automation.dll工具包。

可以在scvmm安装目录中找到或者C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell类似路径下找到该程序集。

我们用它写一段代码。如果调用scvmm组件,可以将其用wcf部署在同一环境中,否则找不到依赖的组件。

 public List<object> InvokeCmdByParamsWithReturn(string command, Dictionary<string, object> parameters, string module)
{
if (string.IsNullOrEmpty(command)) return null; List<object> returnValue = new List<object>();
try
{
if (conn != null && conn.IsConnected)
{
System.Management.Automation.Runspaces.PSSnapInException warning;
System.Management.Automation.Runspaces.RunspaceConfiguration config = System.Management.Automation.Runspaces.RunspaceConfiguration.Create();
if (!string.IsNullOrEmpty(module))
config.AddPSSnapIn(module, out warning);
System.Management.Automation.Runspaces.Runspace run = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(config);
run.Open();
using (System.Management.Automation.Runspaces.Pipeline pipeLine = run.CreatePipeline())
{
System.Management.Automation.Runspaces.Command cmd = new System.Management.Automation.Runspaces.Command(command);
if ((parameters != null || parameters.Count > ))
{
foreach (var p in parameters)
{
cmd.Parameters.Add(p.Key, p.Value);
}
}
pipeLine.Commands.Add(cmd);
var result = pipeLine.Invoke();
if (result != null && result.Count > )
{
foreach (var obj in result)
{
StringBuilder sb = new StringBuilder();
foreach (var item in obj.Members)
{
if (item.MemberType == System.Management.Automation.PSMemberTypes.Property)
sb.Append(string.Format("{0}:{1},", item.Name, item.Value));
}
returnValue.Add(sb.ToString().Remove(sb.Length - , ));
}
return returnValue;
}
}
run.Close();
}
return null;
}
catch (Exception ex)
{
return null;
throw new HostingManagementException(ex.Message, ex);
}
}

下面我们在客户端来调用它。第一个是获取bios环境信息,第二个我们来获取主机相关信息。

 protected void Page_Load(object sender, EventArgs e)
{
ServiceReference1.VirtualMachineManagementServiceClient client = new ServiceReference1.VirtualMachineManagementServiceClient();
client.Connect("192.168.0.223", , "hf01\\administrator", "P@ssw0rd110"); Dictionary<string, object> args = new Dictionary<string, object>();
args.Add("class", "Win32_BIOS");
var query = client.InvokeCmdByParamsWithReturn("Get-WmiObject", args, ""); args = new Dictionary<string, object>();
args.Add("vmmserver", "192.168.0.223");
var query2 = client.InvokeCmdByParamsWithReturn("get-vmhost", args, "Microsoft.SystemCenter.VirtualMachineManager"); client.Disconnect();
client.Close();
}

运行结果。

scvmm sdk之powershell(一)