无法使用powershell.Invoke()从powershell检索所有数据

时间:2013-12-24 09:08:45

标签: c# sql powershell

我得到了SCVMM机器上托管的所有VM的列表。但是,我没有得到所有VM的列表。只显示了100-110。我正在从VM列表中检索一些特定的属性。将它们存储在数据库中,数据库又将在门户中显示信息.EXECUTEPS是一个显示VM列表并将其存储在Collection变量中的函数.Below是我的代码,

Collection<PSObject> objVMDetailArrayCollection = ExecutePS("VMDETAILS", "");
string[] VMName=new string[objVMDetailArrayCollection.Count()];
int VMNumber=0;
foreach (PSObject objPSObject in objVMDetailArrayCollection)
{
    VMName[VMNumber] = objPSObject.Properties["Name"].Value.ToString();
    double ts = double.Parse(objPSObject.Properties["TotalSize"].Value.ToString());
    string vms = objPSObject.Properties["VirtualMachineState"].Value.ToString();
    double ma = double.Parse(objPSObject.Properties["MemoryAssignedMB"].Value.ToString());
    string status = objPSObject.Properties["Status"].Value.ToString();
    int cu = int.Parse(objPSObject.Properties["CPUUtilization"].Value.ToString());
    int pcu = int.Parse(objPSObject.Properties["PerfCPUUtilization"].Value.ToString());
    int pf = int.Parse(objPSObject.Properties["PerfMemory"].Value.ToString());
    int pdbr = int.Parse(objPSObject.Properties["PerfDiskBytesRead"].Value.ToString());
    int pnbw = int.Parse(objPSObject.Properties["PerfNetworkBytesWrite"].Value.ToString());
    DateTime ct = DateTime.Parse(objPSObject.Properties["CreationTime"].Value.ToString());
    string os = objPSObject.Properties["OperatingSystem"].Value.ToString();
    int cc = int.Parse(objPSObject.Properties["CPUCount"].Value.ToString());
    int memory = int.Parse(objPSObject.Properties["Memory"].Value.ToString());
    string hn = objPSObject.Properties["HostName"].Value.ToString();
    DateTime at = DateTime.Parse(objPSObject.Properties["AddedTime"].Value.ToString());

    for(int i=0;i<objArrayHosts1.Length;i++)
    {
        if(objArrayHosts1[i].Name==hn.Split('.')[0])
        {
            VMDetails[] objArrayVMDetails = objVMDetailsClient.GetVMDetailsByHostId(objArrayHosts1[i].HostId);
            if (objArrayVMDetails.Where(h => h.Name == VMName[VMNumber]).Count() > 0)
            {
                objVMDetailsClient.UpdateVMDetails(new VMDetails() { 
                    VMId = (objArrayVMDetails.Where(h => h.Name == VMName[VMNumber]).ToList())[0].VMId, 
                    HostId = objArrayHosts1[i].HostId, 
                    HostName = objArrayHosts1[i].Name, 
                    Name = VMName[VMNumber], 
                    VirtualMachineState = vms, 
                    TotalSize = ts, 
                    MemoryAssignedMB = ma, 
                    Status = status, 
                    CPUUtilization = cu, 
                    PerfCPUUtilization = pcu, 
                    PerfMemory = pf, 
                    PerfDiskBytesRead = pdbr, 
                    PerfNetworkBytesWrite = pnbw, 
                    CreationTime = ct, 
                    OperatingSystem = os, 
                    CPUCount = cc, 
                    Memory = memory, 
                    AddedTime = at });
            }
            else
            {
                objVMDetailsClient.CreateVMDetails(new VMDetails() { 
                    HostId = objArrayHosts1[i].HostId, 
                    HostName = objArrayHosts1[i].Name, 
                    Name = VMName[VMNumber], 
                    VirtualMachineState = vms, 
                    TotalSize = ts, 
                    MemoryAssignedMB = ma, 
                    Status = status, 
                    CPUUtilization = cu, 
                    PerfCPUUtilization = pcu, 
                    PerfMemory = pf, 
                    PerfDiskBytesRead = pdbr, 
                    PerfNetworkBytesWrite = pnbw, 
                    CreationTime = ct, 
                    OperatingSystem = os, 
                    CPUCount = cc, 
                    Memory = memory, 
                    AddedTime = at });
            }
        }
    }
    VMNumber++;
}

1 个答案:

答案 0 :(得分:0)

如果您使用的是PowerShell V3或更高版本,则可以通过利用PSObject支持动态的事实来简化代码。

foreach (dynamic psobj in objVMDetailArrayCollection)
{
    VMName[VMNumber] = psobj.Name.Value.ToString();
    double ts = double.Parse(psobj.TotalSize.Value.ToString());
    string vms = psobj.VirtualMachineState.Value.ToString();
    double ma = double.Parse(psobj.MemoryAssignedMB.Value.ToString());
    ...