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

hlswsv35  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(119)

所以,这个脚本输出了我需要的大部分信息。
然而,在这方面,
1.它不会报告或发布每个服务器上的所有驱动器或列出的所有证书。
1.构建日期显示为20210218123012.000000-300,而不是标准的mm/dd/yyyy
1.驱动器空间显示为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

字符串

guz6ccqo

guz6ccqo1#

1.它不会报告或发布每个服务器上的所有驱动器或列出的所有证书。
发生这种情况是因为无论有多少证书或磁盘,您都需要创建一个pscustomobject,您需要做的是枚举两者(证书和磁盘)并为每个创建一个对象。这将在以下代码中使用for循环进行演示。

  1. .BuildDate20210218123012.000000-300,而不是标准的mm/dd/yyyy
    当使用Get-WmiObject针对OperatingSystem类时,这是此属性的默认格式。在这种情况下,建议不使用此小工具,主要是因为它不再存在于较新版本的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并使用PPercent)格式说明符,例如:

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


另请参阅标准格式说明符以供参考。
最后,代码最终应该是什么样子,除非有特定的需要将CSV存储在每个服务器中,然后从它们中提取信息我不明白为什么你不能直接从Invoke-Command捕获输出,而不需要合并每个服务器的输出,这使事情变得更简单。我已经从$scriptblock中删除了Export-Csv语句。
另外值得注意的是,Invoke-Command可以执行并行调用,而不需要foreach循环,只需将$servers数组直接传递给小程序。

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++) {
        $freeSpace = $diskSize = $percent = $label = $validTo = $validFrom = $null
        $disk = $diskInfo[$i]
        $cert = $certs[$i]

        if ($disk) {
            $label = $disk.DeviceID
            $freeSpace = $disk.FreeSpace / 1GB
            $diskSize = $disk.Size / 1GB
            $percent = '{0:P0}' -f ($freeSpace / $diskSize)
        }

        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              = $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


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


的数据

相关问题