2016-04-26 50 views
0

對象($var)使用[pscustomObject]@{...}創建。在我的開發PC(Win7的X64,PS 4.0)一切都很好,自定義對象:PS 4.0 Windows 7/Windows 2012 R2相同腳本不同結果

PS C:\Windows\system32> $var | gm 


    TypeName: Deserialized.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()                       
AddinExists  NoteProperty AddinExists=null                       
AddinName   NoteProperty Deserialized.System.Management.Automation.PSCustomObject AddinName=           
ComputerName  NoteProperty System.String ComputerName=xxxxxx                 
Description  NoteProperty Description=null                       
FriendlyName  NoteProperty FriendlyName=null                       
PSComputerName  NoteProperty System.String PSComputerName=xxxxxx                 
PSPath    NoteProperty PSPath=null                        
PSShowComputerName NoteProperty System.Boolean PSShowComputerName=True                  
RunspaceId   NoteProperty System.Guid RunspaceId=xxxx4-015f-xxxx-a5a0-6a1861d0f164             
SearchScope  NoteProperty System.String SearchScope=HKU:\S-1-5-21-13434734552-3593029396-1545345-1234513\SOFTWARE\Microsoft\Office\... 
UserLoggedOn  NoteProperty System.Boolean UserLoggedOn=True                   
UserName   NoteProperty System.String UserName=John 

但2012 R2服務器(PS 4.0)就可以 「看到」 它作爲一個hastable:

PS C:\Windows\system32> $var | gm 


    TypeName: Deserialized.System.Collections.**Hashtable** 

Name    MemberType   Definition                                   
----    ----------   ----------                                   
Add    Method    void Add(System.Object key, System.Object value), void IDictionary.Add(System.Object key, System.Object value)          
Clear    Method    void Clear(), void IDictionary.Clear()                            
Clone    Method    System.Object Clone(), System.Object ICloneable.Clone()                        
Contains   Method    bool Contains(System.Object key), bool IDictionary.Contains(System.Object key)                  
ContainsKey  Method    bool ContainsKey(System.Object key)                             
ContainsValue  Method    bool ContainsValue(System.Object value)                            
CopyTo    Method    void CopyTo(array array, int arrayIndex), void ICollection.CopyTo(array array, int index)               
Equals    Method    bool Equals(System.Object obj)                              
GetEnumerator  Method    System.Collections.IDictionaryEnumerator GetEnumerator(), System.Collections.IDictionaryEnumerator IDictionary.GetEnumerator(), System.Collections... 
GetHashCode  Method    int GetHashCode()                                  
GetObjectData  Method    void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISerializable... 
GetType   Method    type GetType()                                  
OnDeserialization Method    void OnDeserialization(System.Object sender), void IDeserializationCallback.OnDeserialization(System.Object sender)         
Remove    Method    void Remove(System.Object key), void IDictionary.Remove(System.Object key)                   
ToString   Method    string ToString()                                 
PSComputerName  NoteProperty   System.String PSComputerName=LDNXD6002520                           
PSShowComputerName NoteProperty   System.Boolean PSShowComputerName=True                            
RunspaceId   NoteProperty   System.Guid RunspaceId=basaaf35e-cdbb-4b5c-97bc-c9asdcaf8                       
Item    ParameterizedProperty System.Object Item(System.Object key) {get;set;}                          
Count    Property    int Count {get;}                                  
IsFixedSize  Property    bool IsFixedSize {get;}                                
IsReadOnly   Property    bool IsReadOnly {get;}                                
IsSynchronized  Property    bool IsSynchronized {get;}                               
Keys    Property    System.Collections.ICollection Keys {get;}                           
SyncRoot   Property    System.Object SyncRoot {get;}                               
Values    Property    System.Collections.ICollection Values {get;} 

有沒有人有任何想法是什麼造成的異常,我可能如何糾正它?這讓我瘋狂,搞砸了我的Export-Csv

這裏是$var是如何定義的:

$var = [PSCustomObject]@{ 
        ComputerName = $ComputerName 
        UserName = $UserName 
        AddinName = $(if($v1){Split-Path $v1.PSPath -leaf}) 
        FriendlyName = $v1.FriendlyName 
        Description = $v1.Description 
        PSPath = $v1.PSPath 
        UserLoggedOn = $False 
        PathExists = $v2 
        SearchScope = $searchScopes[$i] 
       } 

回答

0

好了,我不知道爲什麼,但$等等= [PSCustomObject] @ {...}方法是做不到的。我不得不求助於PS 2.0方法。

我改變:

$i = -1 
      $searchScopes | ForEach-Object{ 
       $i++ 
       #Write-host $_ -ForegroundColor Green 
       try 
       { 
        Get-ChildItem -Path $_ -ErrorAction stop| ForEach-Object{ 
         try 
         { 
          $v1 = Get-ItemProperty -Path $_.PSPath -ErrorAction Stop 
          $v2 = $True #$searchScopes[$i] 
         } 
         catch 
         { 
          $v2 = $False #"Addin/product not found" 
         } 
        } 
       }  
       catch 
       { 
        Write-Verbose "$($error[0].Exception.Message) User: $Username, Computer: $ComputerName" 
       }  

       $oRes2 = [PSCustomObject]@{ 
        ComputerName = $ComputerName 
        UserName = $UserName 
        AddinName = $(if($v1){Split-Path $v1.PSPath -leaf}) 
        FriendlyName = $v1.FriendlyName 
        Description = $v1.Description 
        PSPath = $v1.PSPath 
        UserLoggedOn = $True 
        AddinExists = $v2 
        SearchScope = $searchScopes[$i] 

       } 
      $oRes2 
      } 

     } #End ScriptBlock 


     $SearchResults = Invoke-Command -ScriptBlock $scriptblock -ArgumentList $ObjectSid, $ComputerName, $UserName -ComputerName $ComputerName #-ErrorAction Stop 
     Write-Host "=================" 

    $SearchResults 

要這樣:

$oRes5 = @() 
      $i = -1 
      $searchScopes | ForEach-Object{ 
       $v2 = $null 
       $i++ 
       #Write-host $_ -ForegroundColor Green 
       try 
       { 
        Get-ChildItem -Path $_ -ErrorAction stop| ForEach-Object{ 
         try 
         { 
          $v1 = Get-ItemProperty -Path $_.PSPath -ErrorAction Stop 
          $v2 = $True #$searchScopes[$i] 
         } 
         catch 
         { 
          $v2 = $False #"Addin/product not found" 
         } 
        } 
       }  
       catch 
       { 
        Write-Verbose "$($error[0].Exception.Message) User: $Username, Computer: $ComputerName" 
       }  

       $oRes4 = @{ 
        ComputerName = $ComputerName 
        UserName = $UserName 
        #AddinName = $(if($v1){Split-Path $v1.PSPath -leaf}) 
        FriendlyName = $v1.FriendlyName 
        Description = $v1.Description 
        PSPath = $v1.PSPath 
        UserLoggedOn = $True 
        AddinExists = $v2 
        SearchScope = $searchScopes[$i] 
       } 
      #$oRes4 
      $oRes5 += New-Object -TypeName PSObject -Property $oRes4 
      } #End foreach 
      $oRes5 
     } #End ScriptBlock 


     $SearchResults = Invoke-Command -ScriptBlock $scriptblock -ArgumentList $ObjectSid, $ComputerName, $UserName -ComputerName $ComputerName #-ErrorAction Stop 
     Write-Host "=================" 

    return $SearchResults 

有點亂,但我不能花更多的時間就可以了。

相關問題