0

我正在尝试转换我的库存脚本,以便能够使用工作流获取远程服务器上已安装软件的 csv 列表,但我无法获得它。

    $tod = Get-Date;

    $local = $PSScriptRoot +"\_Output\"+ "$((Get-Date).ToString('yyyyMMdd'))" + "\InstalledSoftwares\";

    if(!(Test-Path -Path $local ))
        {
        New-Item -ItemType Directory -Path $local
        }

        $ItemList = Import-Csv $($PSScriptRoot + "\_HostList.CFG") -Header Srv -Delimiter ";" 
        Write-Host $ItemList.srv
        workflow AllInstalledSoft {
        ForEach -Parallel ($Serv in $ItemList.srv) {
             #$Serv = $_.Srv
             if (Test-Connection -computer $Serv -count(1) -quiet)
                {
                InlineScript { Write-Host $using:Serv "Is Reachable"  -ForegroundColor  Green
                $file = $using:Serv+"_InstalledSoft"+"-{0:yyyyMMdd}.csv" -f $tod
                $ExportFile = $local+$file 
                Get-WmiObject -Class Win32_Product -PSComputerName $using:Serv | select-object @{l="HostName";e={$using:Serv}},Name,InstallDate,InstallLocation,Vendor,Version,Caption,LocalPackage,IdentifyingNumber | Export-CSV -path $ExportFile -notypeinformation}
                }
            else
                {
                InlineScript { Write-Host $using:Serv "Is UnReachable"  -ForegroundColor  Red}
                }
            }
        }
        AllInstalledSoft
4

1 回答 1

1

我无法测试,但试试这个,看看它是否有效。不要尝试使用完整的主机名列表,只需将其减少到 5 台计算机以测试它是否有效。

编辑 3:

$tod =  (Get-Date).ToString('yyyyMMdd')

$local = $PSScriptRoot + "\_Output\" + $tod + "\InstalledSoftwares"

if(!(Test-Path -Path $local )){
    New-Item -ItemType Directory -Path $local
}

$ItemList = Import-Csv $($PSScriptRoot + "\_HostList.CFG") -Header Srv -Delimiter ";" | Select-Object -Skip 1

workflow AllInstalledSoft {
    param (
        [parameter(Mandatory=$true)][array]$ItemList,
        [parameter(Mandatory=$true)][string]$LocalExport,
        [parameter(Mandatory=$true)][string]$Tod
    )
    ForEach -Parallel ($Serv in $ItemList) {
        if(Test-Connection -ComputerName $Serv -Count 1 -Quiet){
            $file = "$($Serv)_InstalledSoft-$Tod.csv"
            $ExportFile = "$LocalExport\$file"
            try {
                Get-WmiObject -Class Win32_Product -PSComputerName $Serv -ErrorAction Stop | Select-Object PSComputerName,Name,InstallDate,InstallLocation,Vendor,Version,Caption,LocalPackage,IdentifyingNumber | Export-CSV -Path $ExportFile -NoTypeInformation
            }
            catch {}
        }
    }
}
AllInstalledSoft -LocalExport $local -ItemList $ItemList.Srv -Tod $tod
于 2017-11-06T12:36:57.960 回答