【问题标题】:Create excel csv files with all computers of each Listed OU使用每个列出的 OU 的所有计算机创建 excel csv 文件
【发布时间】:2021-12-15 18:04:25
【问题描述】:
$OUpath = @(
  "OU=example,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=exam.ple,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example1,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example2,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example3,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example4,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example5,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example6,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example7,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example8,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example9,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=CA,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=CAB,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=Ara,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=Gil,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=Kbi,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=Cieux,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=liz,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=mes,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=IO,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=SE,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=SC,OU=Parent OU,DC=domain name,DC=domain prefix")

foreach ($i in $OUpath){

$fname= $i.Substring(3,9) + ".csv"
Get-ADComputer -Filter * -Property * -SearchBase $i.toString() | 
Select-Object Name,OperatingSystem,OperatingSystemVersion | 
Export-CSV $fname -NoTypeInformation -Encoding UTF8
}

备注:OU=exam.ple with $fname= $i.Substring(3,9) + ".csv" 由于 ou 名称中的点,所以不会给出 excel csv 文件。
问题:是否可以生成 1 个具有不同工作表的 excel 文件?并将数据直接转换成excel表格?

【问题讨论】:

  • 所以您的实际问题是如何从您发布的 OU 列表中获取有效的文件名?如果是这样...请为每个输入列表项发布您想要的结果。
  • 欢迎来到 Code Review@SE。 Nikso,重要的事情值得命名。虽然有用的命名并不是从建模到编码最简单的部分,但根本没有名称(文字)根本没有帮助,会产生贬义的名称,例如 naked constantsmagic numbers i>:39有什么意义?
  • @Lee_Dailey 我的问题是是否可以在 excel 工作表中获取结果,而不是生成多个 excel 文件。该代码实际上正在工作。它会生成 excel 文件,除了 ou 名称中带有点的文件,它会生成另一种奇怪的文件类型。我只需要手动删除点来修复它。对于输出,我想将所有返回的计算机重新组合到一个 excel 文件中,并在不同的 excel 工作表中分隔。谢谢你的建议,亲切的问候。
  • 请随意to self-answer your question(稍后也接受)。
  • .Substring() 的第二个参数是指定的长度,而不是结束位置。另外,如果你只想要 3 个属性,不要做-Property *,而只要求默认不返回的属性,比如Name。使用星号将使 cmdlet 收集 所有 属性,这会耗费时间和内存。

标签: excel powershell active-directory ldap


【解决方案1】:
$OUpath = @("OU=example,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=exam.ple,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example1,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example2,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example3,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example4,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example5,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example6,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example7,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example8,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=example9,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=CA,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=CAB,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=Ara,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=Gil,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=Kbi,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=Cieux,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=liz,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=mes,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=IO,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=SE,OU=Parent OU,DC=domain name,DC=domain prefix",
  "OU=SC,OU=Parent OU,DC=domain name,DC=domain prefix")

foreach ($distinguishedName in $OUpath)
{
$csvFileName = ($distinguishedName -replace "OU","" -replace "=","" -replace ",","" -replace "Parent","" -replace "DC","" -replace "domain name","" -replace "prefix","") +".csv"


Get-ADComputer -Filter * -Property Name,OperatingSystem,OperatingSystemVersion -SearchBase $i.toString() | Select-Object Name,OperatingSystem,OperatingSystemVersion | Export-CSV $csvFileName -NoTypeInformation -Encoding UTF8
  
}

Function Merge-CSVFiles
{
Param(
$CSVPath = "C:\Users\Haddouch_Fadil\excelSource",
$XLOutput="C:\Users\Haddouch_Fadil\exceloutput\temp.xls" 
)

$csvFiles = Get-ChildItem ("$CSVPath\*") -Include *.csv
$Excel = New-Object -ComObject Excel.Application 
$Excel.visible = $false
$Excel.sheetsInNewWorkbook = $csvFiles.Count
$workbooks = $excel.Workbooks.Add()
$CSVSheet = 1

Foreach ($CSV in $Csvfiles){

$worksheets = $workbooks.worksheets
$CSVFullPath = $CSV.FullName
$SheetName = ($CSV.name -split "\.")[0]
$worksheet = $worksheets.Item($CSVSheet)
$worksheet.Name = $SheetName
$TxtConnector = ("TEXT;" + $CSVFullPath)
$CellRef = $worksheet.Range("A1")
$Connector = $worksheet.QueryTables.add($TxtConnector,$CellRef)
$worksheet.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True
$worksheet.QueryTables.item($Connector.name).TextFileParseType  = 1
$worksheet.QueryTables.item($Connector.name).Refresh()
$worksheet.QueryTables.item($Connector.name).delete()
$worksheet.UsedRange.EntireColumn.AutoFit()
$CSVSheet++

}

$workbooks.SaveAs($XLOutput,51)
$workbooks.Saved = $true
$workbooks.Close()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbooks) | Out-Null
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

}

Merge-CSVFiles -CSVPath C:\Users\Haddouch_Fadil\excelSource -XLOutput C:\Users\Haddouch_Fadil\exceloutput\temp.xls

Remove-Item * -Include *.csv

这是我的最终代码。

  • 脚本返回 OU 内的所有计算机对象。它使用列表中 OU 的可分辨名称进行搜索。

  • 为每个 OU 生成包含此信息的 .csv 文件;文件名的正确输出。 (删除了 OU 可分辨名称中的所有重复内容)这对于创建具有正确名称的工作表很重要。否则我必须手动完成。

  • 通过调用 Merge-CSVFiles 函数将所有 csv 文件转换为表格并将它们放在 1 个 excel 文件中的不同工作表中。

-删除当前PowerShell目录末尾生成的csv文件

-> 我的下一步是使用 VB 脚本将工作表中的表格转换为 Excel 中的数据表格。

【讨论】:

    猜你喜欢
    • 2019-06-05
    • 2015-02-07
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多