问题描述
我有一个脚本,可以扫描我域中的所有服务器并将其输出到两个单独的CSV文件-一个简单文件,另一个扩展文件.
I have a script that scans all my servers in my domains and outputs to two separate CSV files - one simple and one extensive.
我一次在csv中写了一行.这导致成千上万个文件打开和文件关闭.在脚本末尾扫一扫.但是,如何使用export-csv(最好使用函数)来做到这一点?
I write one line at the time to my csv.. This results in thousands of file-open and file-close.. I've lurked around and understand that I should first gather all the info and write it all in one sweep at the end of the script. But how do I do this with export-csv (preferably using a function)?
有什么办法可以对短列表和长列表使用相同的功能?
And is there a way I can use the same function for short and long list?
该脚本在每个域/服务器上执行许多任务,但是为了您的观看乐趣,我将其精简为以下内容:
The script performs numerous tasks on each domain/server, but I've trimmed it down to this for your viewing pleasure:
$domains = "no","se","dk"
# Loop through specified forests
foreach ($domain in $domains) {
# List all servers
$servers = Get-QADComputer
# Looping through the servers
foreach ($server in $servers) {
# GENERATE LONGLIST #
# Ping server
if (Test-Connection -ComputerName $server.name -count 1 -Quiet )
{
$Reachable = "Yes"
# Fetch IP address
$ipaddress = [System.Net.Dns]::GetHostAddresses($Server.name)|select-object IPAddressToString -expandproperty IPAddressToString
# Formatting output and export all info to CSV
New-Object -TypeName PSObject -Property @{
SystemName = ($server.name).ToLower()
Reachable = $Reachable
Domain = $server.domain
IPAddress = $IPAddress
} | Select-Object SystemName,Domain,IPAddress| Export-Csv -Path "longexport.csv" -append
}
else # Can't reach server
{
$reachable = "No"
$IPAddress = "Unknown"
# Formatting output and export all info to CSV
New-Object -TypeName PSObject -Property @{
SystemName = ($server.name).ToLower()
Reachable = $Reachable
Domain = $server.domain
} | Select-Object SystemName,Domain,IPAddress| Export-Csv -Path "shortexport.csv" -append
}
}
}
(而且,我只是说我知道我不能做-加上export-csv,但是我正在使用一个让我执行此功能的函数.)
(and let me just say that I know that I cannot do -append with export-csv, but I am using a function that let's me do this..)
推荐答案
您正在将相同数量的属性导出到每个文件,所以我不确定我为什么理解其中一个被认为长而又短的原因.无论如何,我建议以下情况,不要将所有计算机都分配给一个变量,它会占用大量RAM,而是使用流式传输方式(一次一个对象)并使用foreach-object.另外,由于我发现文件中没有差异,因此我在每个域操作结束时输出到该文件(每个域一个).而且使用另一种方式,您只能向该文件写入一次.
You are exporting the same amount of properties to each file so I'm not sure I understand why one of them is considered long and one short. Anyway, I suggest the following, don't assign all computers to a variable, it can take up a lot of RAM, instead use a streaming way (one object at a time) and use foreach-object. Also, since I find no difference in the files I output to the file at the end of each domain operation (one per domain). And with another twick you could write only once to the file.
$domains = "no","se","dk"
foreach ($domain in $domains) {
Get-QADComputer -Service $domain -SizeLimit 0 | Foreach-Object {
$reachable = Test-Connection -ComputerName $_.Name -count 1 -Quiet
if($reachable)
{
$IPAddress = [System.Net.Dns]::GetHostAddresses($_.Name)|select-object IPAddressToString -expandproperty IPAddressToString
}
else
{
$IPAddress = $null
}
New-Object -TypeName PSObject -Property @{
SystemName = $_.Name.ToLower()
Reachable = $reachable
Domain = $_.Domain
IPAddress = $IPAddress
} | Select-Object SystemName,Domain,IPAddress
} | Export-Csv -Path export.csv -Append
}
这篇关于通过几个foreach收集信息,然后在脚本末尾导出export-csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!