gpt4 book ai didi

powershell - 连接/合并数组的属性

转载 作者:行者123 更新时间:2023-12-03 16:28:50 25 4
gpt4 key购买 nike

我有2个我想合并的对象,但似乎找不到解决方案

Messages                                   Name                                                                           Error
-------- ---- -----
{\\APPS-EUAUTO1\C$\Users\xautosqa\AppDa... test 1 True
{[APPS-EUAUTO1] [prep] Setting agent op... test 2 False

TestPlan Script TestCase TestData ErrorCount ErrorText DateTime Elapsed
-------- ------ -------- -------- ---------- --------- -------- -------
D:\XHostMach... D:\XHostMach... rt1 1,\a\"" 1 [#ERROR#][AP... 2014-03-28 1... 0:00:18
D:\XHostMach... D:\XHostMach... rt2 1,\a\"" 0 2014-03-28 1... 0:00:08

我试过了 :
 function Join-Object {
Param(
[Parameter(Position=0)]
$First
,
[Parameter(Position=1,ValueFromPipeline=$true)]
$Second
)
BEGIN {
[string[]] $p1 = $First | gm -type Properties | select -expand Name
}
Process {
$Output = $First | Select $p1
foreach($p in $Second | gm -type Properties | Where { $p1 -notcontains $_.Name } | select -expand Name) {
Add-Member -in $Output -type NoteProperty -name $p -value $Second."$p"
}
$Output
}
} # End: Function Join-Object

$TestCases = Join-Object $TxtTestcases $RexTestcases | Select Name, TestPlan, TestCase, Script, TestData, Error, ErrorCount, ErrorText, Messages, DateTime, Elapsed

但是第二个对象不存在:
Name         TestPlan     TestCase     Script       TestData            Error ErrorCount   ErrorText    Messages    DateTime   
---- -------- -------- ------ -------- ----- ---------- --------- -------- --------
test 1 True {\\APPS-...
test 2 False {[APPS-E...

我也尝试过:
Function Merge-Testcase {
Param ($TxtTestcase, $RexTestcase)
$Fields = @{
Name = $TxtTestcase.Name
TestPlan = $RexTestcase.TestPlan
TestCase = $RexTestcase.TestCase
Script = $RexTestcase.Script
TestData = $RexTestcase.TestData
IsError = $TxtTestcase.Error
ErrourCount = $RexTestcase.ErrorCount
ErrorText = $RexTestcase.ErrorText
Messages = $TxtTestcase.Messages
DateTime = $RexTestcase.DateTime
Elapsed = $RexTestcase.Elapsed
}
New-Object PSObject -Property $Fields | ConvertTo-Csv -NoTypeInformation
} # End: Function Merge-Testcase
$TestCases = Merge-Testcase -TxtTestcase $TxtTestcases -RexTestcas $RexTestcase
$TestCases | Format-Table

但我得到这个
"Script","Name","TestCase","TestPlan","ErrorText","TestData","ErrourCount","IsError","Elapsed","DateTime","Messages"
,,,,,,,,,,

关于如何加入2个对象的任何想法吗?

最佳答案

选项1

一种选择是将两个对象都放入HashTable中,然后将其转换为PSCustomObject

 $Obj1 = (Get-Process)[0]; # Get a process
$Obj2 = (Get-Service)[0]; # Get a service
($Result = [PSCustomObject]@{ Obj1 = $Obj1; Obj2 = $Obj2; });

$Result.Obj1.Name;
$Result.Obj2.Name;

注意:这将创建您必须钻取的附加“级别”,因此虽然不理想,但可以使用。

选项#2

第二个选项是遍历“第二个对象”的所有属性,并使用 Add-Member将它们添加到“第一个对象”中,您的示例已经显示了。

创建一个名为 c:\test\test.txt的空文件,然后运行以下代码:
# Get a couple of objects (with different property names)
$Object1 = Get-Service -Name Dnscache;
$Object2 = Get-Item c:\test\test.txt;

# Get a list of the properties on both objects
$PropertyList1 = @(Get-Member -InputObject $Object1 -MemberType Properties).Name;
$PropertyList2 = Get-Member -InputObject $Object2 -MemberType Properties | Where-Object -FilterScript { $PropertyList1 -notcontains $PSItem.Name; };

# Add the properties, from the second object, to the first object
foreach ($Property in $PropertyList2) {
Write-Host ('Adding property: {0}' -f $Property.Name);
Add-Member -InputObject $Object1 -Name $Property.Name -MemberType NoteProperty -Value $Object2.$($Property.Name);
}

# Output the object
$Object1 | select *;

输出如下所示:
Mode                : -a---
PSChildName : test.txt
PSDrive : C
PSIsContainer : False
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\test
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\test\test.txt
PSProvider : Microsoft.PowerShell.Core\FileSystem
Attributes : Archive
CreationTime : 3/11/2014 3:06:43 PM
CreationTimeUtc : 3/11/2014 8:06:43 PM
Directory : C:\test
DirectoryName : C:\test
Exists : True
Extension : .txt
FullName : C:\test\test.txt
IsReadOnly : False
LastAccessTime : 3/11/2014 3:06:43 PM
LastAccessTimeUtc : 3/11/2014 8:06:43 PM
LastWriteTime : 3/11/2014 3:06:29 PM
LastWriteTimeUtc : 3/11/2014 8:06:29 PM
Length : 0
BaseName : test
VersionInfo : File: C:\test\test.txt
InternalName:
OriginalFilename:
FileVersion:
FileDescription:
Product:
ProductVersion:
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language:

Name : Dnscache
RequiredServices : {nsi, Tdx}
CanPauseAndContinue : False
CanShutdown : False
CanStop : True
DisplayName : DNS Client
DependentServices : {NcaSvc}
MachineName : .
ServiceName : Dnscache
ServicesDependedOn : {nsi, Tdx}
ServiceHandle :
Status : Running
ServiceType : Win32ShareProcess
Site :
Container :

看看服务和文件的属性如何都在一个对象上?

关于powershell - 连接/合并数组的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22794949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com