使用$var
创建的对象([pscustomObject]@{...}
)。在我的开发PC(Win7 x64,PS 4.0)上一切都很好,一个自定义对象:
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 :(得分:0)
好吧,我不知道为什么,但$ blah = [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
有点乱,但我不能再花时间了。