使用工作流程并行的Powershell远程安装软件

时间:2017-11-06 11:19:05

标签: powershell installed-applications

我正在尝试转换我的库存脚本,以便能够使用工作流程在远程服务器上获取已安装软件的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

1 个答案:

答案 0 :(得分: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