0

I am trying to convert my inventory script to be able to get a csv list of installed softwares on remote servers using workflows but I am not able to get it.

    $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
OwenS
  • 233
  • 2
  • 6
  • 20
  • What error do you have? When using variables in `InlineScript`, your variables must be prefixed by `$using:myVariable` if they come from outside of it. – Manu Nov 06 '17 at 11:27
  • Thanks for your feedback, in fact I have no error and no result ... It works fine when I just run it without workflow. – OwenS Nov 06 '17 at 11:41
  • There are multiple problems : use `$ItemList.Srv` instead of `$ItemList`. Where are declared `$local` and `$tod`? In `InlineScript` you have to use `$using:Serv` if you want the variable to be pass from outside. Use `@{n=` instead of `@{l=` for the calculated properties. Before using Workflows, always test your code without a workflow to validate the process. Workflows are not simple, they must be used with a good knowledge of how they work. – Manu Nov 06 '17 at 11:53
  • I have corrected my code but still no results, thanks. – OwenS Nov 06 '17 at 12:17
  • You first wrote `_HostList.txt` and then `_HostList.CFG`. If it's a csv file ,why not call it `_HostList.csv`? – Manu Nov 06 '17 at 12:29
  • It is a csv but renamed as CFG like all my other files in this folder, not related anyway, it is working fine without the workflow. Does the Inline script have to contain the Get-Wmi command line ? Thanks – OwenS Nov 06 '17 at 12:33

1 Answers1

1

I cannot test but try this and see if it works. Don't try with the full hostname list, just reduce it to 5 computers to test if it works.

EDIT 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
Manu
  • 1,685
  • 11
  • 27
  • Thanks for you suggestion, unfortunately, the script runs and I have no output and no error message with only 2 servers in the file. – OwenS Nov 06 '17 at 13:28
  • You can't have output in a workflow, for example `write-host` does not work in it. Workflow is started in a separated thread, you can't ouput values in the console until it finishes the process. The only result you'll have is the csv created in your specific folder. And don't use `.cfg` instead of a csv file as a list of hostnames is not a configuration file. – Manu Nov 06 '17 at 13:30
  • I know, I understood that but the script finishes and there is no output. – OwenS Nov 06 '17 at 13:33
  • I edited my answer, try with the full script I provided in the edit. I removed the `-Header Srv` in `Import-Csv` because it did not work with this parameter and also removed the `InlineScript` as it was useless. – Manu Nov 06 '17 at 14:20
  • Sorry I need to make another change, just wait my second edit – Manu Nov 06 '17 at 14:25
  • Thanks a lot, it is working now but it is stuck on an unreachable server and I had to add the -Header Srv or I receive an error message. – OwenS Nov 06 '17 at 14:35
  • With this new edit is it ok? You can also mark the answer as accepted as it now returns the result you expected ;-) – Manu Nov 06 '17 at 14:54