gpt4 book ai didi

excel - 使用每个列出的 OU 的所有计算机创建 excel csv 文件

转载 作者:行者123 更新时间:2023-12-04 20:22:15 25 4
gpt4 key购买 nike

$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 和 $fname= $i.Substring(3,9) + ".csv"由于 ou 名称中的点而不给出 excel csv 文件。
问题 :是否可以生成 1 个具有不同工作表的 excel 文件?并将数据直接转换成excel表格?

最佳答案

$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 中的数据表格。

    关于excel - 使用每个列出的 OU 的所有计算机创建 excel csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69789619/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com