shell表示計算機操作系統中的殼層,與之相對的是內核,內核不能與用戶直接交互,而是通過shell為用戶提供操作界面,shell分為兩類,一種提供命令行界面,一種提供圖形界面。windows powershell第一個版本是在2006年,提供類似unix系統的命令行殼程程序。powershell是建立在.net framework基礎之上的,它內置一百多種cmdlet工具,它不僅可以像傳統cmd命令一樣管理操作系統,還可以管理針對.net架構下開發的程序,比如system center virtual machine manager內置powershell。
下面我們來敲入兩條命令,第一個獲取當前系統時間,第二個獲取windows以p開頭的進程,第三個字母大小寫轉換。
我們還以用工具集來連接遠程計算機。第一個行獲取遠程計算機登錄權限,第二步來檢查該計算機sql server服務是否啟動。
1 $c=get-credential (get-credential -credential domain\administrator) 2 Get-WmiObject -Query "select * from win32_service where name='mssqlserver'" -computername 192.168.0.181 -credential $c
那么如何用powershell來管理scvmm,這里需要導入scvmm管理模塊。可以用get-vm來獲取hyperv中某一臺名為linux的虛機。
更多的命令工具可以調用get-command來查看。
1 add-pssnapin "Microsoft.SystemCenter.VirtualMachineManager" 2 get-vm -name linux -vmmserver 192.168.0.223
那么如果用代碼去實現就需要用到System.Management.Automation.dll工具包。
可以在scvmm安裝目錄中找到或者C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell類似路徑下找到該程序集。
我們用它寫一段代碼。如果調用scvmm組件,可以將其用wcf部署在同一環境中,否則找不到依賴的組件。
1 public List<object> InvokeCmdByParamsWithReturn(string command, Dictionary<string, object> parameters, string module) 2 { 3 if (string.IsNullOrEmpty(command)) return null; 4 5 List<object> returnValue = new List<object>(); 6 try 7 { 8 if (conn != null && conn.IsConnected) 9 { 10 System.Management.Automation.Runspaces.PSSnapInException warning; 11 System.Management.Automation.Runspaces.RunspaceConfiguration config = System.Management.Automation.Runspaces.RunspaceConfiguration.Create(); 12 if (!string.IsNullOrEmpty(module)) 13 config.AddPSSnapIn(module, out warning); 14 System.Management.Automation.Runspaces.Runspace run = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(config); 15 run.Open(); 16 using (System.Management.Automation.Runspaces.Pipeline pipeLine = run.CreatePipeline()) 17 { 18 System.Management.Automation.Runspaces.Command cmd = new System.Management.Automation.Runspaces.Command(command); 19 if ((parameters != null || parameters.Count > 0)) 20 { 21 foreach (var p in parameters) 22 { 23 cmd.Parameters.Add(p.Key, p.Value); 24 } 25 } 26 pipeLine.Commands.Add(cmd); 27 var result = pipeLine.Invoke(); 28 if (result != null && result.Count > 0) 29 { 30 foreach (var obj in result) 31 { 32 StringBuilder sb = new StringBuilder(); 33 foreach (var item in obj.Members) 34 { 35 if (item.MemberType == System.Management.Automation.PSMemberTypes.Property) 36 sb.Append(string.Format("{0}:{1},", item.Name, item.Value)); 37 } 38 returnValue.Add(sb.ToString().Remove(sb.Length - 1, 1)); 39 } 40 return returnValue; 41 } 42 } 43 run.Close(); 44 } 45 return null; 46 } 47 catch (Exception ex) 48 { 49 return null; 50 throw new HostingManagementException(ex.Message, ex); 51 } 52 }
下面我們在客戶端來調用它。第一個是獲取bios環境信息,第二個我們來獲取主機相關信息。
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 ServiceReference1.VirtualMachineManagementServiceClient client = new ServiceReference1.VirtualMachineManagementServiceClient(); 4 client.Connect("192.168.0.223", 8100, "hf01\\administrator", "P@ssw0rd110"); 5 6 Dictionary<string, object> args = new Dictionary<string, object>(); 7 args.Add("class", "Win32_BIOS"); 8 var query = client.InvokeCmdByParamsWithReturn("Get-WmiObject", args, ""); 9 10 args = new Dictionary<string, object>(); 11 args.Add("vmmserver", "192.168.0.223"); 12 var query2 = client.InvokeCmdByParamsWithReturn("get-vmhost", args, "Microsoft.SystemCenter.VirtualMachineManager"); 13 14 client.Disconnect(); 15 client.Close(); 16 }
運行結果。
文章列表