Powershell 报告脚本。要报告缺少特定文件的计算机吗

Powershell 报告脚本。要报告缺少特定文件的计算机吗,powershell,Powershell,目前有这个,但它只报告的机器与铬和版本。我希望它也报告脱机的机器,或者更重要的是缺少文件 有什么建议吗 助教 只需首先检查机器是否可以访问,如果不能访问,则在文件中写入一个特殊条目。类似地,如果找不到文件,只需编写不同的条目: $Computers = Get-Adcomputer -Filter * foreach ($Computer in $Computers) { $PC = $computer.dnshostname $hostname = $PC.split('.'

目前有这个,但它只报告的机器与铬和版本。我希望它也报告脱机的机器,或者更重要的是缺少文件

有什么建议吗

助教


只需首先检查机器是否可以访问,如果不能访问,则在文件中写入一个特殊条目。类似地,如果找不到文件,只需编写不同的条目:

$Computers = Get-Adcomputer -Filter * 
foreach ($Computer in $Computers)
{
    $PC = $computer.dnshostname
    $hostname = $PC.split('.')[0]
    if (Test-Connection $hostname -Count 1) {
        Write-Host "\\$PC\c`$\Program Files\Google\Chrome\Application\chrome.exe"
        $exe = "\\$pc\c`$\Program Files\Google\Chrome\Application\chrome.exe"
        if  ( Test-Path  $exe){
            $ver = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($exe).FileVersion
            Add-Content -path .\results.csv "$exe,$ver"
        }
        else {
            Add-Content -path .\results.csv "File not found"
        }
    }
    else {
        Add-Content -path .\results.csv "Not reachable"
    }
}

显然,您可能需要更改文本。此外,machinename的列难道没有意义吗?

首先检查主机是否可访问,然后仅在主机实际联机时检查文件是否存在。为每种情况创建自定义对象,并使用管道将其导出到CSV

$Computers | ForEach-Object {
    $PC  = $_.dnshostname
    $exe = "\\$PC\C`$\Program Files\Google\Chrome\Application\chrome.exe"

    if (Test-Connection $PC -Count 3 -Quiet) {
        if (Test-Path -LiteralPath $exe){
            [PSCustomObject]@{
                Hostname    = $PC
                HostOnline  = $true
                FileExists  = $true
                FileVersion = [Diagnostics.FileVersionInfo]::GetVersionInfo($exe).FileVersion
            }
        } else {
            [PSCustomObject]@{
                Hostname    = $PC
                HostOnline  = $true
                FileExists  = $false
                FileVersion = $null
            }
        }
    } else {
        [PSCustomObject]@{
            Hostname    = $PC
            HostOnline  = $false
            FileExists  = $null
            FileVersion = $null
        }
    }
} | Export-Csv '.\results.csv' -NoType
$Computers | ForEach-Object {
    $PC  = $_.dnshostname
    $exe = "\\$PC\C`$\Program Files\Google\Chrome\Application\chrome.exe"

    if (Test-Connection $PC -Count 3 -Quiet) {
        if (Test-Path -LiteralPath $exe){
            [PSCustomObject]@{
                Hostname    = $PC
                HostOnline  = $true
                FileExists  = $true
                FileVersion = [Diagnostics.FileVersionInfo]::GetVersionInfo($exe).FileVersion
            }
        } else {
            [PSCustomObject]@{
                Hostname    = $PC
                HostOnline  = $true
                FileExists  = $false
                FileVersion = $null
            }
        }
    } else {
        [PSCustomObject]@{
            Hostname    = $PC
            HostOnline  = $false
            FileExists  = $null
            FileVersion = $null
        }
    }
} | Export-Csv '.\results.csv' -NoType