我试图通过$servername变量对我的每日服务器报告的输出进行排序,但是,我无法得到要排序的输出。我认为它需要某种数组或选择对象字符串。有人能给我指出正确的方向吗?
另外,如果有一种方法可以用一个空行分隔每个新的$servername,那将是惊人的!
- 增加了完整的脚本,以便更好地理解
Import-Module ActiveDirectory
$server = (Get-ADComputer -SearchBase "searchbase" -filter *).name
$scriptblock = {
# Set the server name
$serverName = $env:COMPUTERNAME
# Get the up/down status of the server
$pingResult = Test-Connection -ComputerName $server.Name -Count 1 -ErrorAction SilentlyContinue
$status = if ($pingResult.StatusCode -eq 1) {
'Up'
}
else {
'Down'
}
# Get the server build date
$buildDate = (Get-CimInstance -Class Win32_OperatingSystem).InstallDate
# Certificate name information
$certname = get-childitem -path Cert:\LocalMachine\My\ |
Select friendlyname
# Get the server certificate validity
[array] $certs = Get-ChildItem -Path Cert:\LocalMachine\My |
Where-Object { -Not $_.PSIsContainer } |
Sort-Object -Property NotBefore
#Number of Certs on Machine
$certcount = (get-childitem -path Cert:\LocalMachine\my).count
# Check Trellix update
$TrellixDate = [datetime](Get-ItemProperty 'C:\Program Files\Common Files\McAfee\Engine\AMCoreUpdater\amupdate.dat').LastWriteTime.DateTime
# Operating system collection
$os = Get-CimInstance Win32_OperatingSystem | Select-Object Caption
# Disk information
[array] $diskInfo = Get-CimInstance -Class Win32_LogicalDisk -Filter 'DriveType = 3'
for ($i = 0; $i -lt [System.Math]::Max($cert.Count, $diskInfo.Count); $i++) {
$disk = $diskInfo[$i]
$freeSpace = $diskSize = $percent = $label = $null
$cert = $certs[$i]
if ($disk) {
$label = $disk.DeviceID
$freeSpace = $disk.FreeSpace / 1GB
$diskSize = $disk.Size / 1GB
$percent = '{0:P0}' -f ($freeSpace / $diskSize)
}
$validTo = $validFrom = $null
if ($cert) {
$validTo = $cert.NotAfter
$validFrom = $cert.NotBefore
}
#SQL Version
if (test-path 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server' -ErrorAction SilentlyContinue){
$sql = (get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
foreach ($i in $sql)
{
$sqlver = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').$i
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$p\Setup").Version
}
}
else {$sqlver = "n/a"}
#IIS version, if available
if (Get-Service -Name W3SVC -ErrorAction SilentlyContinue){
$IIS = (Get-ItemProperty -PATH 'HKLM:\SOFTWARE\Microsoft\InetStp\').VERSIONSTRING
}
else {$iis = "n/a"}
#Describing the Columns for Output
[PSCustomObject]@{
ServerName = $serverName
Status = $status
OperatingSystem = $os
BuildDate = '{0:u}' -f $buildDate
CertName = $certname
CertCreated = $validFrom
CertExpiration = $validTo
CertCount = $certcount
TrellixLastUpdate = $TrellixDate
SQLVersion = $sqlver
IISVersion = $IIS
DiskLabel = $label
Freespace = '{0:N2} Gb' -f $freespace
DiskSize = '{0:N2} Gb' -f $disksize
PercentFree = $percent
}
foreach ($serverName in 'PSCustomObject') { $obj | where-object {$_.Servername -eq $servername} | Sort-Object $servername }
#foreach ($serverName in 'PSCustomObject') { $obj | where-object {$_.Servername -eq $servername} | Sort-Object $servername }
}
字符串
}
#command to run the script on the servers
Invoke-Command -ComputerName $server -ScriptBlock $scriptblock -HideComputerName -ErrorAction SilentlyContinue|
Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName |
#Export-Csv e:\activetasks\server_checks\DailyServerReport.csv -NoTypeInformation -force
Export-Csv c:\Nec\DailyServerReport1.csv -NoTypeInformation -force
型
2条答案
按热度按时间x8diyxa71#
我会说一个简单的
$ArrayList | Sort-Object -Property ServerName | Format-Table
,其中$ArrayList
是一个[System.Collections.ArrayList]
(在大多数情况下比常规数组更好)变量,包含各种[PSCustomObject]
应该可以做到这一点。你甚至可能不需要行与行之间的空间。
wbrvyc0a2#
下面是重写的代码,希望能展示如何捕获结果对象,对它们进行排序并写入Csv文件
字符串