简体   繁体   English

对象Powershell的排序属性

[英]Sort Properties of Object Powershell

All i'm using this code to get data from SharePoint List and export it to txt file as you see i'm using New-Object PSObject to get this 我所使用的所有代码均从SharePoint列表中获取数据并将其导出到txt文件,如您所见,我正在使用New-Object PSObject来获取此数据

my question is how can i sort the the properties by the name i gave or how to export this items sorted by those name Thanks in advance 我的问题是如何按我给定的名称对属性进行排序,或者如何导出按那些名称排序的项目

$MyWeb = Get-SPWeb "http://ilike-eg.suz.itcgr.net/SM"
$MyList = $MyWeb.Lists["SCGC"] 
$exportlist = @()
$Mylist.Items |  foreach {
$obj =   New-Object PSObject -property @{ 
        "A"="   "+$_["AACCOUNT_ID"]
        "B"="   "+$_["BTRANSACTION_ID"]
        "C"="          "+$_["CDATE"] 
        "D"="      "+$_["DCUSTOMER_ID"]
        "E"="     "+$_["ECUSTOMER_NAME"]
        "F"=" "+$_["FAMOUNT"]
        "G"=$_["GCLASS"] 
} 
$exportlist += $obj | Sort-Object -descending   
$DateStamp = get-date -uformat "%Y-%m-%d@%H-%M-%S"
$NameOnly = "CDP" 
$exportlist | Export-Csv -Delimiter "`t"-path "$NameOnly.txt" 
}
$a, ${d:CDP.txt} = Get-Content .\CDP.txt
$a, ${d:CDP.txt} = Get-Content .\CDP.txt
(Get-Content D:\CDP.txt) | 
Foreach-Object {$_ -replace $([char]34), ""} | 
Set-Content D:\CDP.txt
(Get-Content D:\CDP.txt) | 
Foreach-Object {$_ -replace "/", "-"} | 
Set-Content D:\CDP.txt
(Get-Content D:\CDP.txt) | 
Foreach-Object {$_ -replace "`t", ""} | 
Set-Content D:\CDP.txt

If you know the propertynames, then use the following: 如果您知道属性名称,请使用以下命令:

$exportlist |
Select-Object A,B,C,D,E,F,G |
Export-Csv -Delimiter "`t"-path "$NameOnly.txt"

If you don't know the name of the properties, try: 如果您不知道属性的名称,请尝试:

$properties = $exportlist |
Foreach-Object { $_.psobject.Properties | Select-Object -ExpandProperty Name } |
Sort-Object -Unique

$exportlist |
Select-Object $properties |
Export-Csv -Delimiter "`t"-path "$NameOnly.txt"

I made a few other modifications to your script to make it more efficient and easier to read: 我对您的脚本进行了其他一些修改,以使其更高效,更易于阅读:

$MyWeb = Get-SPWeb "http://ilike-eg.suz.itcgr.net/SM"
$MyList = $MyWeb.Lists["SCGC"] 
$exportlist = @()

$Mylist.Items |  ForEach-Object {
    $obj =   New-Object PSObject -property @{ 
            "A"="   "+$_["AACCOUNT_ID"]
            "B"="   "+$_["BTRANSACTION_ID"]
            "C"="          "+$_["CDATE"] 
            "D"="      "+$_["DCUSTOMER_ID"]
            "E"="     "+$_["ECUSTOMER_NAME"]
            "F"=" "+$_["FAMOUNT"]
            "G"=$_["GCLASS"] 
    }

    #Remove unnecessary sort
    $exportlist += $obj   
    $DateStamp = get-date -uformat "%Y-%m-%d@%H-%M-%S"
    $NameOnly = "CDP" 

    #Exporting with sorted properties
    $exportlist |
    Select-Object A,B,C,D,E,F,G |
    Export-Csv -Delimiter "`t"-path "$NameOnly.txt"
}

#Removed duplicate get-content line
$a, ${d:CDP.txt} = Get-Content .\CDP.txt

#Combined replace statements to avoid multiple read/writes
(Get-Content D:\CDP.txt) |
Foreach-Object {$_ -replace $([char]34) -replace "`t" -replace '/', ''} |
Set-Content D:\CDP.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM