【问题标题】:Generate word's list from two CSV files and export to CSV从两个 CSV 文件生成单词列表并导出为 CSV
【发布时间】:2018-05-03 21:16:52
【问题描述】:

我正在尝试使用 PowerShell 导入两个 CSV 文件,一个 CSV 称为 accts,一个称为名称。 accts 文件有 8 个数字,names 文件有 4 个名称。我需要输出一个组合,最终目标是输出一个带有 STX 前缀的 32 个单词的列表。我将使用这个 32 个列表并创建 32 个 AD 对象。理想情况下,我还需要检查单词/AD 对象是否已经存在。

账号:12345 23456 34567 45678 56789 67890 78901 89012 姓名:约翰·戴夫·乔·迈克

输出应该是:

stx-12345-约翰 stx-23456-约翰 stx-34567-约翰 stx-45678-约翰 stx-56789-约翰 stx-67890-约翰 stx-78901-约翰 stx-89012-约翰 stx-12345-戴夫 stx-23456-戴夫 stx-34567-戴夫 stx-45678-戴夫 stx-56789-戴夫 stx-67890-戴夫 stx-78901-戴夫 stx-89012-戴夫 等等

我曾考虑过尝试使用数组,但这可能会很复杂。有什么建议吗?

【问题讨论】:

  • 您遇到问题的代码在哪里?
  • 首先:ForEach ( $Name in (Import-Csv Names.csv)){ForEach ( $Acct in (Import-Csv Accts.csv)){"stx-{0}-{1}" -f $Acct.Accts,$Name.Names}}

标签: powershell csv


【解决方案1】:

嗯,我觉得你不应该在使用 Powershell 的 AD 上工作,如果这对你来说太过分了。是的,你会使用数组并循环遍历它们。

所以我已经尽可能多地分解了我的代码,这样你就可以了解它背后的逻辑。

$accts = @("12345","23456","34567","45678","56789","67890","78901","89012")
$names = @("john","dave","joe","mike")
$adobject = @()

foreach($n in $names){
    # for each name, go through the accts and add the prefix
    foreach($a in $accts){
        #store all items in an array
        $adobject += "stx-"+$a+"-"+$n 
    }
}
cls
# go through your array you just created
foreach($o in $adobject){
    if(get-aduser $o){
        Write-host "user $o already exists"
    }else{
        new-aduser $o #create user
    }
}

像这样的小任务是学习编码和逻辑的最佳方式。您应该能够将其转换为使用 CSV。

【讨论】:

  • 我同意这就是为什么我要先弄清楚组名的创建,然后再搞乱 AD。
  • 感谢您帮助我度过了脑抽筋。 :)
  • 为什么添加以下内容会导致屏幕输出/csv 文件中出现多个同名副本:$mergedfile = $adobject |添加内容-路径“C:\mergedlist.csv”
猜你喜欢
  • 2013-06-22
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多