导入带标题的CSV,在远程服务器上扫描日志,将数据添加到数组,将数组导出为CSV [英] Import CSV with headers, scan for log on remote servers, add data to array, export array to CSV
问题描述
我需要回到绘图板,使用一个模板,我使用大约一年,从.csv头读取值,打开hashtable数组,更新服务器上的各种属性,因为扫描运行(ping,logs,services等),最后导出到.csv或gridview。
I need to go back to the drawing board with a template I've been using for about a year that reads values from a .csv with headers, opens a hashtable array, updates various properties on the servers as the scans are run (ping, logs, services, etc) and finally exports to .csv or gridview.
基于散列表的数组模板对于过去的各种功能都很好,但我发现它不工作,当尝试对多个服务器进行大规模的GCI扫描。所以今晚,我试图重写使用常规 @()
数组。但是,有两个问题:
The hashtable-based array template was working fine for various functions over the past while, but I've found it doesn't work when attempting to do mass GCI scans against multiple servers. So tonight, I tried to re-write using regular @()
arrays. However, there are two issues:
- 输入CSV上的标题是server和platform在输出。
-
if(GCI ...)
代码不工作,我不知道为什么。
- The headers on the input CSV are "server" and "platform", but they aren't reflected in the output. Instead the column header for the first entry is shown.
- The
if (GCI ...)
code isn't working, and I'm not sure why.
这里是代码:
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
$ShortDate = (Get-Date).ToString('MM/dd/yyyy')
$InputCSV = @(Import-csv $Dir\Masters.csv)
function PingEM {
Param($_)
$Error.Clear()
try {
$Ping = New-Object System.Net.NetworkInformation.Ping
$PingReturn = $Ping.Send($_, 1000) | select Status -ExpandProperty Status
} catch {}
if (($Error) -or ($PingReturn -ne "Success")) {
Write-Host "$_ is offline. Remaining checks will be skipped" -ForegroundColor Red
$Ping = "Offline"
$TaskSeq = "N/A"
$Completed = "N/A"
} else {
Write-Host "Online" -ForegroundColor Green
$Ping = "Online"
}
}
$OutArray = @()
foreach ($Item in $InputCSV) {
$outarray += New-Object PSObject -Property @{
Server = $Item.Server
Platform = $Item.Platform
}
PingEM $($Item.Server)
if ($Ping -eq "Online") {
Write-Host "Checking $($Item.Server) now"
if (GCI \\$($Item.Server)\c$\log\Results_*.html -EA 0 | Where { $_.LastWriteTime -ge "$ShortDate" }) {
Write-Host "Recent log found!"
} else {
Write-Host "Recent log NOT found!"
}
}
} # PING
$OutArray
推荐答案
下面的代码应该工作。你有一个错误, $。LastWriteTime
你需要使用 $ _。LastWriteTime
。您还试图定义 $ Server
和 $ Platform
的变量,而不是在散列表中指定这些属性。最后,修改了 GCI
命令以引用 $($ Item.Server)
,而不是<$ c的未定义变量$ c> $ Server 。
The code below should work. You had a typo with $.LastWriteTime
where you needed to use $_.LastWriteTime
. You were also trying to define variables of $Server
and $Platform
, rather than specifying those as properties in the hash table. Lastly, revised your GCI
command to reference $($Item.Server)
rather than an undefined variable of $Server
.
代码:
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
$ShortDate = (Get-Date).ToString('MM/dd/yyyy')
$InputCSV = @(Import-csv $Dir\Servers.csv)
$OutArray = @()
ForEach ($Item in $InputCSV)
{
if (GCI \\$($Item.Server)\c$\*.log -EA 0 | Where { $_.LastWriteTime -ge "$ShortDate" })
{
$Completed = "Yes"
}else{
$Completed = "No"
}
$outarray += New-Object PsObject -property @{
Server = $Item.Server
Platform = $Item.Platform
Completed = $Completed
}
}
$OutArray
这篇关于导入带标题的CSV,在远程服务器上扫描日志,将数据添加到数组,将数组导出为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!