Powershell在导出之前以CSV格式编辑标题

问题描述 投票:1回答:2

您好我正在尝试制作一个小PS脚本,从一个OU和一些Headers获取用户,然后在保存之前编辑标题(以便我可以将它们导入Office365)。

$Select = @{
'Filter' = '*'
'searchbase'='OU=TESTOU,DC=corp,DC=test,DC=com'
'Properties'= @("GivenName","SurName","DisplayName","samaccountname","Streetaddress","state","City","PostalCode","Country","Title","Company","Department","Office","telePhonenumber")
}

Get-ADUser @select|Select-Object -Property $propertyTranslation|export-csv c:\Test0218.csv -notype

$propertyTranslation = @(
    @{ Name = 'UserPrincipalName'; Expression = { $_.'User Name' } }
    @{ Name = 'GivenName';   Expression = { $_.'First Name'  } }
    @{ Name = 'SurName';   Expression = { $_.'Last Name'  } }
    @{ Name = 'DisplayName';   Expression = { $_.'Display Name'  } }
    @{ Name = 'Title';   Expression = { $_.'Job Title'  } }
    @{ Name = 'Department';   Expression = { $_.'Department'  } }
    @{ Name = 'telePhonenumber';   Expression = { $_.'Office Phone'  } }
    @{ Name = 'StreetAddress';   Expression = { $_.'Address'  } }
    @{ Name = 'City';   Expression = { $_.'City'  } }
    @{ Name = 'PostalCode';   Expression = { $_.'Zip or Postal Code'  } }
    @{ Name = 'Country';   Expression = { $_.'Country or Region'  } }
)

(Import-Csv -Path 'c:\Test0218.csv') |
Select-Object -Property $propertyTranslation |
Export-Csv c:\Test0230.csv -NoTypeInformation

这不起作用,因为文件的上下文是废话:( qazxsw poi

正如您所看到的那样,文件标题保持不变,现在没有任何信息(只有2而不是11)我填写了所有信息,当只是使用AD标题导出CSV时,它就具有所需的信息。有人能帮我吗?

BR

powershell csv active-directory office365
2个回答
0
投票

我会在ISE或VSCode中调试它并展开您的管道以协助这项工作。

作为你想要做的事情的模拟,我使用一些预定义的csv数据创建了以下内容,我发现这有助于调试任何与csv相关的工作流程。

enter image description here

得到:

$csvdata = @'
"Header One", "Header Two"
foo, bar
baz, boo
'@

$xlate = @(
    @{Name = 'H1'; Expression = {$_.'Header One'} }
    @{Name = 'H2'; Expression = {$_.'Header Two'} }
)

$csv = ConvertFrom-Csv $csvdata

$newcsv = $csv | Select -Property $xlate | ConvertTo-Csv -NoTypeInformation

"csv----------------------"
$csv

"newcsv----------------------"
$newcsv

0
投票

看起来你的财产名称翻译倒退了:

  • 您将提供原始属性已有的新属性名称(例如,csv---------------------- Header One Header Two ---------- ---------- foo bar baz boo newcsv---------------------- "H1","H2" "foo","bar" "baz","boo"
  • 您正尝试使用它们没有的名称(例如,GivenName)访问输入对象上的属性。

如果要重命名给定输入对象的属性,请使用以下方法:

First Name

也就是说,将新名称分配给$co = [pscustomobject] @{ one = 1 } # sample input object $co | Select-Object @{ n = 'eins'; e = 'one' } # rename 'one' to 'eins' n)键,并在Namee)键中引用原始属性名称。

© www.soinside.com 2019 - 2024. All rights reserved.