Cpt.Whale
Cpt.Whale

Reputation: 5321

powershell wait for command to finish before proceeding

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

Upvotes: 1

Views: 2659

Answers (2)

Ash Housewares
Ash Housewares

Reputation: 155

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

Upvotes: 1

Shawn Esterman
Shawn Esterman

Reputation: 2342

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

Upvotes: 0

Related Questions