我要解释我在这里要做的事情。然后分享我到目前为止所尝试的内容......
我有2个csv文件......
CSV File1(ContractorDomain.csv)看起来像这样...
"SamAccountName","FirstName","LastName"
"2v2390s" ,"Jr.,John" ,"Doe"
"4gsi2f2" ,"Peter" ,"Pan"
"asdfds9" ,"Leo" ,"Turtle"
"2fsa02f" ,"Chuck" ,"Findly"
"4gas5a3" ,"Stan" ,"Garcia"
"1asfd32" ,"Linsey" ,"Silver"
我只想获得专栏SamAccountName
现在csv file2(employeeList.csv)看起来像这样......
"DomainNamesFromAD"
"PermDomain"
"PermDomain"
"PermDomain"
"PermDomain"
"ContractorDomain"
"ContractorDomain"
"ContractorDomain"
"ContractorDomain"
"ContractorDomain"
"ContractorDomain"
最终目标是使csv file2(employeeList.csv)看起来像这样......
"DomainNamesFromAD","SamAccountNameFromAD"
"PermDomain"
"PermDomain"
"PermDomain"
"PermDomain"
"ContractorDomain","2v2390s"
"ContractorDomain","4gsi2f2"
"ContractorDomain","asdfds9"
"ContractorDomain","2fsa02f"
"ContractorDomain","4gas5a3"
"ContractorDomain","1asfd32"
我觉得我差不多了,但我需要一些帮助。这是我的代码......
$ContractorSamAccountNames = Import-Csv ContractorDomain.csv |
Select-Object -Expand SamAccountName
$csvFormatContractorSamAccountNames = $ContractorSamAccountNames | Select-Object @{Name='SamAccountName';Expression={$_}}
现在,如果我将$csvFormatContractorSamAccountNames
导出到csv文件,它会显示正确的列和值。所以我知道我正在获得价值观。现在我在这里尝试将它们插入文件2 ...
Import-Csv employeeList.csv |
Select-Object -Property *, @{n='SamAccountNameFromAD';e={
if ($_.DomainNamesFromAD -eq "ContractorDomain") {"Found"}}} |
Export-Csv "employeeListTemp.csv" -NoTypeInformation
我在csv file2(employeeList.csv)中选择了正确的行。但是,我需要以"Found"
中的值替换$csvFormatContractorSamAccountNames
(使用for循环可能?)。
我也试过这个:
$num = 0
Import-Csv employeeList.csv |
Select-Object -Property *, @{n='SamAccountNameFromAD';e={
if ($_.DomainNamesFromAD -eq "ContractorDomain")
{
$ContractorSamAccountNames[$num]
$num++
}}} |
Export-Csv "employeeListTemp.csv" -NoTypeInformation
但这只会填充数组$ContractorSamAccountNames
的第一个值。换句话说,该文件如下所示......
"DomainNamesFromAD","SamAccountNameFromAD"
"PermDomain"
"PermDomain"
"PermDomain"
"PermDomain"
"ContractorDomain","2v2390s"
"ContractorDomain","2v2390s"
"ContractorDomain","2v2390s"
"ContractorDomain","2v2390s"
"ContractorDomain","2v2390s"
"ContractorDomain","2v2390s"
我不确定为什么$ num ++不会增加。任何可以提供帮助的人都非常感谢。我已经在这里待了5个小时......
答案 0 :(得分:0)
为什么要在导入文件的文件名中包含域名时加入两个csv文件。我用两个导入文件进行了测试...
像你的CSV文件1(ContractorDomain.csv)
CSV File2(PermDomain.csv)
"SamAccountName","FirstName","LastName"
"JCarter" ,"Jimmy" ,"Carter"
"RReagan" ,"Ronald" ,"Reagan"
"GBush" ,"George" ,"Bush"
"BClinton" ,"Bill" ,"Clinton"
"GWBush" ,"George W.","Bush"
"BObama" ,"Barack" ,"Obama"
剧本..
$Local:SourceFilesPathes = @("C:\temp\stackoverflow.com_34245941\ContractorDomain.csv", "C:\temp\stackoverflow.com_34245941\PermDomain.csv")
$Local:OutFilePath = "C:\temp\stackoverflow.com_34245941\employeeList.csv"
$Local:OutFileObjectList = @()
$SourceFilesPathes | ForEach-Object {
$Current_SourceFilePath = $_
if ( (Test-Path -Path $Current_SourceFilePath -PathType leaf) ) {
$Local:Current_DomainName = (Get-Item -Path $Current_SourceFilePath).BaseName # Domain name from file name
$Local:Current_SourceFileContent = Import-Csv -Path $Current_SourceFilePath -Delimiter ","
$Current_SourceFileContent.ForEach({
$Local:Current_OutFileEntryObject = New-Object -TypeName System.Management.Automation.PSObject
$Current_OutFileEntryObject | Add-Member -MemberType "NoteProperty" -Name "DomainNamesFromAD" -Value $Current_DomainName
$Current_OutFileEntryObject | Add-Member -MemberType "NoteProperty" -Name "SamAccountNameFromAD" -Value ($_.SamAccountName)
$OutFileObjectList += $Current_OutFileEntryObject
})
}
}
# Select-Object to order the properties inside CSV. From left "DomainNamesFromAD", "SamAccountNameFromAD"
# Sort-Object to order the entries
$OutFileObjectList | Select-Object -Property DomainNamesFromAD, SamAccountNameFromAD | Sort-Object -Property DomainNamesFromAD, SamAccountNameFromAD | Export-Csv -Path $OutFilePath -Delimiter "," -Force -NoTypeInformation
输出档案......
"DomainNamesFromAD","SamAccountNameFromAD"
"ContractorDomain","1asfd32"
"ContractorDomain","2fsa02f"
"ContractorDomain","2v2390s"
"ContractorDomain","4gas5a3"
"ContractorDomain","4gsi2f2"
"ContractorDomain","asdfds9"
"PermDomain","BClinton"
"PermDomain","BObama"
"PermDomain","GBush"
"PermDomain","GWBush"
"PermDomain","JCarter"
"PermDomain","RReagan"
答案 1 :(得分:0)
我发现了......
$ContractorSamAccountNames = Import-Csv ContractorDomain.csv |
Select-Object -Expand SamAccountName
$script:numOfContractorSamAccountNames = 0
Import-Csv employeeList.csv |
Select-Object -Property *, @{n='SamAccountNameFromAD';e={
if ($_.DomainNamesFromAD -eq "ContractorDomain") {$ContractorSamAccountNames[$script:numOfContractorSamAccountNames];$script:numOfContractorSamAccountNames++}}} |
Export-Csv "employeeListTemp.csv" -NoTypeInformation
答案 2 :(得分:0)
这是您的解决方案......
{{1}}
我尝试了以“$ num = 0”开头的示例。关键是运行脚本后$ num仍为0。换句话说:没有foreach循环就没有办法。
另一个问题是,两个文件中的每个域都需要完全相同数量的条目。