运行此脚本时无法获取要报告的每个驱动器和要报告的每个证书

问题描述 投票:0回答:1

所以,这个脚本输出了我需要的大部分信息。

但是,

  1. 它不会报告或发布每台服务器上的所有驱动器或列出的所有证书。
  2. 构建日期为 20210218123012.000000-300,而不是标准的 mm/dd/yyyy
  3. 驱动器空间显示为 39.8967208862305 而不是 %

这些都应该与每个服务器名称分组在一起。

Import-Module ActiveDirectory

# Select the servers you want to hit $server = (Get-ADComputer -SearchBase "OU=OU" -filter *).name

$scriptblock = {
    # Set the server name
    $serverName = $env:computername
    # Get the up/down status of the server
    $pingResult = Test-Connection -ComputerName $serverName -Count 1
    $status = if ($pingResult.StatusCode -eq 0) {
        'Up'
    }
    else {
        'Down'
    }
    # Get the server build date
    $os = Get-WmiObject -Class Win32_OperatingSystem
    $buildDate = $os.InstallDate
    # Get the server certificate validity
    $cert = Get-ChildItem -Path Cert:\LocalMachine\My |
        Where-Object { $_.PSIsContainer -eq $false } |
        Sort-Object -Property NotBefore |
        Select-Object -First 1
    $validFrom = $cert.NotBefore
    $validTo = $cert.NotAfter
    #Check Trellix update
    $TrellixDate = [datetime](get-itemproperty "C:\Program Files\Common Files\McAfee\Engine\AMCoreUpdater\amupdate.dat").lastwritetime.DateTime

    foreach ($disk in (Get-WmiObject -ComputerName $env:computername -Class Win32_LogicalDisk -Filter 'DriveType = 3')) {
        $diskLabel = $disk.DeviceID
        $freeSpace = $disk.FreeSpace / 1GB
        $diskSize = $disk.Size / 1GB
        $percent = $freeSpace / $diskSize * 100
    }
    # Create a custom object with the collected information
    $serverInfo = [PSCustomObject]@{
        ServerName           = $serverName
        Status               = $status
        BuildDate            = $buildDate.datetime
        CertificateValidFrom = $validFrom
        CertificateValidTo   = $validTo
        TrellixLastUpdate    = $TrellixDate
        DiskLabel            = $diskLabel
        Freespace            = $freespace
        DiskSize             = $disksize
        Percent              = $percent
    }
    # Export the information to a CSV file
    $serverInfo | Export-Csv -Path '\nec\ServerInfo.csv' -NoTypeInformation
}
Write-Host 'Server information exported to ServerInfo.csv'
#command to run the script on the servers
foreach ($server in $servers) {
    Invoke-Command -ComputerName $server -ScriptBlock $scriptblock
}

foreach ($server in $servers) {
    $CSV = Get-ChildItem -Filter ServerInfo.csv | Select-Object -ExpandProperty FullName | Import-Csv
}

Export-Csv c:\NEC\CombinedFile.csv -NoTypeInformation -Append
powershell csv scripting
1个回答
0
投票
  1. 它不会报告或发布每台服务器上的所有驱动器或列出的所有证书。

发生这种情况是因为您正在创建一个

pscustomobject
,无论有多少个证书或多少个磁盘,您需要做的就是枚举两者(证书和磁盘)并为每个对象创建 1 个对象。这通过以下代码中的
for
循环进行了演示。

  1. .BuildDate
    显示为
    20210218123012.000000-300
    ,而不是标准
    mm/dd/yyyy

当使用

Get-WmiObject
定位
OperatingSystem
类时,这是此属性的默认格式。在这种情况下,建议不要使用此 cmdlet,主要是因为它不再存在于较新版本的 PowerShell 中,它本质上已过时。使用
Get-CimInstance
代替,语法大部分相同,并且此属性的类型为
DateTime
,可以轻松地将其转换为您所需格式的字符串,几个示例:

# using the `u` format specifier (Outputs: 2021-02-04 12:19:19Z)
'{0:u}' -f (Get-CimInstance -Class Win32_OperatingSystem).InstallDate
# using the `s` format specifier (Outputs: 2021-02-04T12:19:19)
'{0:s}' -f (Get-CimInstance -Class Win32_OperatingSystem).InstallDate
# using a custom `MM/dd/yyyy` format specifier (Outputs: 02/04/2021)
'{0:MM/dd/yyyy}' -f (Get-CimInstance -Class Win32_OperatingSystem).InstallDate

另请参阅格式说明符表以供参考。

  1. 驱动器空间显示为 39.8967208862305 而不是 %

为此,您可以将

$freeSpace / $diskSize * 100
更改为
$freeSpace / $diskSize
并使用
P
(Percent) 格式说明符,例如:

# lets assume you have 20Gb free space out of 1Tb
'{0:P}' -f (20Gb / 1Tb) # Outputs: 1.95%

另请参阅标准格式说明符以供参考。

最后,代码最终应该是什么样子,除非有特定需要在每个服务器中存储 CSV,然后从它们中提取该信息我不明白为什么你不能只捕获

 的输出Invoke-Command
直接,无需合并每个服务器的输出,这使事情变得更加简单。因此,我从
Export-Csv
中删除了
$scriptblock
语句。

还值得注意的是,

Invoke-Command
可以执行并行调用,无需
foreach
循环,只需将
$servers
数组直接传递给cmdlet即可。

Import-Module ActiveDirectory

# Select the servers you want to hit $server = (Get-ADComputer -SearchBase "OU=OU" -filter *).name

$scriptblock = {
    # Set the server name
    $serverName = $env:computername

    # Get the up/down status of the server
    $pingResult = Test-Connection -ComputerName $serverName -Count 1
    $status = if ($pingResult.StatusCode -eq 0) {
        'Up'
    }
    else {
        'Down'
    }

    # Get the server build date
    $buildDate = (Get-CimInstance -Class Win32_OperatingSystem).InstallDate

    # Get the server certificate validity
    [array] $certs = Get-ChildItem -Path Cert:\LocalMachine\My |
        Where-Object -Not PSIsContainer |
        Sort-Object -Property NotBefore

    #Check Trellix update
    $TrellixDate = [datetime](Get-ItemProperty 'C:\Program Files\Common Files\McAfee\Engine\AMCoreUpdater\amupdate.dat').LastWriteTime.DateTime

    [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
        }

        [PSCustomObject]@{
            ServerName           = $serverName
            Status               = $status
            BuildDate            = '{0:u}' -f $buildDate
            CertificateValidFrom = $validFrom
            CertificateValidTo   = $validTo
            TrellixLastUpdate    = $TrellixDate
            DiskLabel            = $label
            Freespace            = '{0:N2} Gb' -f $freespace
            DiskSize             = '{0:N2} Gb' -f $disksize
            Percent              = '{0:P0}' -f $percent
        }
    }
}
#command to run the script on the servers
Invoke-Command -ComputerName $servers -ScriptBlock $scriptblock -HideComputerName |
    Select-Object * -ExcludeProperty RunspaceId |
    Export-Csv c:\NEC\CombinedFile.csv -NoTypeInformation -Append

最终输出的示例应该类似于:

© www.soinside.com 2019 - 2024. All rights reserved.