powershell 5.0 - How to get count of PSObject when there is only one object left - Stack Overflow

admin2025-04-17  3

I have the below code which produces the PSObject:

$Installs = foreach($CVAFile in $CVAFiles)
{
  $CVAFileContents = get-content $($CVAFile).fullname -raw

  $CVAFileContents -match '(?s).*?\[General\].*?SystemMustBeRebooted=(\d).*?\[Install Execution\].*?SilentInstall=(.*?)\n.*' | out-null

  $s = $CVAFile.fullname.split("\")[-1].split(".")[0]  

  [pscustomobject]@{
    SP            = $s
    NeedsRestart  = $Matches[1]
    SilentInstall = $Matches[2]
  }
}

Then use the below to write it out for resumption (with other code that successfully removes installs as they are done):

$Installs | export-csv $ResumeHPImagingAssistantInstallsCSVFile -notypeinformation

Importing the CSV using this later:

$Installs = import-csv $ResumeHPImagingAssistantInstallsCSVFile

However, get-member shows then following when there is 2+ data lines in the CSV:

PS C:> get-member -inputobject $Installs

TypeName: System.Object[]

Name           MemberType            Definition                                                                                                                          
----           ----------            ----------                                                                                                                          
Count          AliasProperty         Count = Length                                                                                                                      
Add            Method                int IList.Add(System.Object value)                                                                                                  
Address        Method                System.Object&, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Address(int )                           
Clear          Method                void IList.Clear()                                                                                                                  
Clone          Method                System.Object Clone(), System.Object ICloneable.Clone()                                                                             
CompareTo      Method                int IStructuralComparable.CompareTo(System.Object other, System.Collections.IComparer comparer)                                     
Contains       Method                bool IList.Contains(System.Object value)                                                                                            
CopyTo         Method                void CopyTo(array array, int index), void CopyTo(array array, long index), void ICollection.CopyTo(array array, int index)          
Equals         Method                bool Equals(System.Object obj), bool IStructuralEquatable.Equals(System.Object other, System.Collections.IEqualityComparer comparer)
Get            Method                System.Object Get(int )                                                                                                             
GetEnumerator  Method                System.Collections.IEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerator()                          
GetHashCode    Method                int GetHashCode(), int IStructuralEquatable.GetHashCode(System.Collections.IEqualityComparer comparer)                              
GetLength      Method                int GetLength(int dimension)                                                                                                        
GetLongLength  Method                long GetLongLength(int dimension)                                                                                                   
GetLowerBound  Method                int GetLowerBound(int dimension)                                                                                                    
GetType        Method                type GetType()                                                                                                                      
GetUpperBound  Method                int GetUpperBound(int dimension)                                                                                                    
GetValue       Method                System.Object GetValue(Params int[] indices), System.Object GetValue(int index), System.Object GetValue(int index1, int index2), ...
IndexOf        Method                int IList.IndexOf(System.Object value)                                                                                              
Initialize     Method                void Initialize()                                                                                                                   
Insert         Method                void IList.Insert(int index, System.Object value)                                                                                   
Remove         Method                void IList.Remove(System.Object value)                                                                                              
RemoveAt       Method                void IList.RemoveAt(int index)                                                                                                      
Set            Method                void Set(int , System.Object )                                                                                                      
SetValue       Method                void SetValue(System.Object value, int index), void SetValue(System.Object value, int index1, int index2), void SetValue(System.O...
ToString       Method                string ToString()                                                                                                                   
Item           ParameterizedProperty System.Object IList.Item(int index) {get;set;}                                                                                      
IsFixedSize    Property              bool IsFixedSize {get;}                                                                                                             
IsReadOnly     Property              bool IsReadOnly {get;}                                                                                                              
IsSynchronized Property              bool IsSynchronized {get;}                                                                                                          
Length         Property              int Length {get;}                                                                                                                   
LongLength     Property              long LongLength {get;}                                                                                                              
Rank           Property              int Rank {get;}                                                                                                                     
SyncRoot       Property              System.Object SyncRoot {get;}      

and the following when there is one data line left. What the heck happened to all the methods and properties!!

PS C:> get-member -inputobject $Installs

TypeName: System.Management.Automation.PSCustomObject

Name          MemberType   Definition                       
----          ----------   ----------                       
Equals        Method       bool Equals(System.Object obj)   
GetHashCode   Method       int GetHashCode()                
GetType       Method       type GetType()                   
ToString      Method       string ToString()                
NeedsRestart  NoteProperty string NeedsRestart=0            
SilentInstall NoteProperty string SilentInstall="HPUP.exe...
SP            NoteProperty string SP=sp155005

I need to be able to using count (or length) regardless of whether I have 0, 1, or 2+ PSObjects in the $Installs variable. How?

I have the below code which produces the PSObject:

$Installs = foreach($CVAFile in $CVAFiles)
{
  $CVAFileContents = get-content $($CVAFile).fullname -raw

  $CVAFileContents -match '(?s).*?\[General\].*?SystemMustBeRebooted=(\d).*?\[Install Execution\].*?SilentInstall=(.*?)\n.*' | out-null

  $s = $CVAFile.fullname.split("\")[-1].split(".")[0]  

  [pscustomobject]@{
    SP            = $s
    NeedsRestart  = $Matches[1]
    SilentInstall = $Matches[2]
  }
}

Then use the below to write it out for resumption (with other code that successfully removes installs as they are done):

$Installs | export-csv $ResumeHPImagingAssistantInstallsCSVFile -notypeinformation

Importing the CSV using this later:

$Installs = import-csv $ResumeHPImagingAssistantInstallsCSVFile

However, get-member shows then following when there is 2+ data lines in the CSV:

PS C:> get-member -inputobject $Installs

TypeName: System.Object[]

Name           MemberType            Definition                                                                                                                          
----           ----------            ----------                                                                                                                          
Count          AliasProperty         Count = Length                                                                                                                      
Add            Method                int IList.Add(System.Object value)                                                                                                  
Address        Method                System.Object&, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Address(int )                           
Clear          Method                void IList.Clear()                                                                                                                  
Clone          Method                System.Object Clone(), System.Object ICloneable.Clone()                                                                             
CompareTo      Method                int IStructuralComparable.CompareTo(System.Object other, System.Collections.IComparer comparer)                                     
Contains       Method                bool IList.Contains(System.Object value)                                                                                            
CopyTo         Method                void CopyTo(array array, int index), void CopyTo(array array, long index), void ICollection.CopyTo(array array, int index)          
Equals         Method                bool Equals(System.Object obj), bool IStructuralEquatable.Equals(System.Object other, System.Collections.IEqualityComparer comparer)
Get            Method                System.Object Get(int )                                                                                                             
GetEnumerator  Method                System.Collections.IEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerator()                          
GetHashCode    Method                int GetHashCode(), int IStructuralEquatable.GetHashCode(System.Collections.IEqualityComparer comparer)                              
GetLength      Method                int GetLength(int dimension)                                                                                                        
GetLongLength  Method                long GetLongLength(int dimension)                                                                                                   
GetLowerBound  Method                int GetLowerBound(int dimension)                                                                                                    
GetType        Method                type GetType()                                                                                                                      
GetUpperBound  Method                int GetUpperBound(int dimension)                                                                                                    
GetValue       Method                System.Object GetValue(Params int[] indices), System.Object GetValue(int index), System.Object GetValue(int index1, int index2), ...
IndexOf        Method                int IList.IndexOf(System.Object value)                                                                                              
Initialize     Method                void Initialize()                                                                                                                   
Insert         Method                void IList.Insert(int index, System.Object value)                                                                                   
Remove         Method                void IList.Remove(System.Object value)                                                                                              
RemoveAt       Method                void IList.RemoveAt(int index)                                                                                                      
Set            Method                void Set(int , System.Object )                                                                                                      
SetValue       Method                void SetValue(System.Object value, int index), void SetValue(System.Object value, int index1, int index2), void SetValue(System.O...
ToString       Method                string ToString()                                                                                                                   
Item           ParameterizedProperty System.Object IList.Item(int index) {get;set;}                                                                                      
IsFixedSize    Property              bool IsFixedSize {get;}                                                                                                             
IsReadOnly     Property              bool IsReadOnly {get;}                                                                                                              
IsSynchronized Property              bool IsSynchronized {get;}                                                                                                          
Length         Property              int Length {get;}                                                                                                                   
LongLength     Property              long LongLength {get;}                                                                                                              
Rank           Property              int Rank {get;}                                                                                                                     
SyncRoot       Property              System.Object SyncRoot {get;}      

and the following when there is one data line left. What the heck happened to all the methods and properties!!

PS C:> get-member -inputobject $Installs

TypeName: System.Management.Automation.PSCustomObject

Name          MemberType   Definition                       
----          ----------   ----------                       
Equals        Method       bool Equals(System.Object obj)   
GetHashCode   Method       int GetHashCode()                
GetType       Method       type GetType()                   
ToString      Method       string ToString()                
NeedsRestart  NoteProperty string NeedsRestart=0            
SilentInstall NoteProperty string SilentInstall="HPUP.exe...
SP            NoteProperty string SP=sp155005

I need to be able to using count (or length) regardless of whether I have 0, 1, or 2+ PSObjects in the $Installs variable. How?

Share Improve this question asked Jan 30 at 16:46 user66001user66001 9151 gold badge15 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Seems one way to solve this is to force $Installs to be an array, even if only one element is contained within it.

Using [array]$Installs =

转载请注明原文地址:http://anycun.com/QandA/1744904207a89266.html