1

In powershell I am trying to do the following:

$name = "computername"
#get installed programs
Write-Host "****APPLICATIONS"
gwmi win32_Product -ComputerName $name | select name

#gets services
write-host "****SERVICES"
Get-Service -ComputerName $name | ft

the expected output would be

****APPLICATIONS
name
of
app

****SERVICES
running services here
more services here

the actual result is

****APPLICATIONS
****SERVICES
name
of
app
running services here
more services here

I have attempted to do start-job then wait-job , but running gwmi as a job seems to output nothing to the console and sending the output to a separate file defeats the purpose of other parts of the script

I also attempted to use start-sleep and it still finishes both write-host commands before proceeding

Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16
  • While Win32_Product may work it is a generally regarded as a bad way to retrieve information about installed software. Even Microsoft's Hey Scripting Guys have written articles about how bad it is. I would suggest looking at [this answer](http://stackoverflow.com/a/25268564) for a faster alternative. – StephenP Jan 13 '17 at 00:13

2 Answers2

1

Try this:

$name = "computername"
Write-Host "`n****APPLICATIONS`n"
gwmi win32_Product -ComputerName $name | % {$_.name}
write-host "`n****SERVICES"
Get-Service -ComputerName $name | ft

If you want the results alphabetical:

$name = "computername"
Write-Host "`n****APPLICATIONS`n"
$apps = gwmi win32_Product -ComputerName $name | % {$_.name}
$apps | sort
write-host "`n****SERVICES"
Get-Service -ComputerName $name | ft
Ash Housewares
  • 155
  • 1
  • 8
  • this works perfectly, could you explain a bit what the `n does here? I've never seen this notation in powershell – Cpt.Whale Jan 13 '17 at 13:19
  • 1
    @user19702 - That is for newline. It just makes the output easier to read by putting a blank line between the data sets. Without those characters, there would be no space between the `Write-Host` lines and the applications and services lists. If you want to see the difference, you can remove them and run it. – Ash Housewares Jan 13 '17 at 17:53
0
Param(
    $ComputerName = 'AT805061'
)

# Get installed programs
Write-Host "`n****APPLICATIONS`n"
Get-WmiObject win32_Product -ComputerName $ComputerName | Select-Object -ExpandProperty Name | Sort-Object

# Get services
Write-Host "`n****SERVICES`n"
Get-Service -ComputerName $ComputerName | Where-Object -Property Status -eq -Value Running | Select-Object -ExpandProperty Name | Sort-Object
Shawn Esterman
  • 2,292
  • 1
  • 10
  • 15
  • You should try a parameter block instead of a variable. That way you could be like "C:\Scripts\ComputerInfo.ps1 -ComputerName MyComputer1" and it would run the script with that parameter. – Shawn Esterman Jan 12 '17 at 21:27
  • Also I believe you just need to modify your script to expand the Name property to strings. They would currently be the type: Selected.System.Management.ManagementObject – Shawn Esterman Jan 12 '17 at 21:29
  • Also added sorting and only selecting services that were running. – Shawn Esterman Jan 12 '17 at 21:39