0

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

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
4

1 回答 1

0

如果您知道属性名称,请使用以下命令:

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

如果您不知道属性的名称,请尝试:

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

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

我对您的脚本进行了一些其他修改,以使其更高效且更易于阅读:

$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
于 2014-05-18T10:39:18.987 回答