Author Posts

January 1, 2012 at 12:00 am

by willbs at 2013-02-19 15:02:48

when i run this command:
invoke-command -scriptblock {if ( -not (get-process winword -errorAction SilentlyContinue) ) { "Process winword NOT running!" }} -computername 192.168.0.xxx -credential ' $cred
i get the comment "Process winword NOT running!" displayed on the sceen if winword is not running

when i run this command:
invoke-command -scriptblock {if ( -not (get-process winword -errorAction SilentlyContinue) ) { $a = "Process winword NOT running!" }} -computername 192.168.0.xxx -credential ' $cred
nothing appears in the variable $a
how do i get the comment to appear in $a so i can place into report like this
$a | out-file -filepath C:\TestReport.txt -append

also what is the way to load multiply values into the name where winword is, for example
$Proc = "winword", "svchost", "certsrv", "goopy", "OUTLOOK"
invoke-command -scriptblock {if ( -not (get-process $Proc -errorAction SilentlyContinue) ) { "Process $Proc NOT running!" }} -computername 192.168.0.102 -credential ' $cred

thanks in advance

by vstarmanv at 2013-02-19 23:30:16

$Proc = "winword", "svchost", "certsrv", "goopy", "OUTLOOK"
foreach($EXV in $Proc)
{
invoke-command -scriptblock { param($Procs) if ( (get-process $Procs -errorAction SilentlyContinue) ) { "Process $Procs NOT running!" }} -computername 192.168.0.102 -credential ' $cred -ArgumentList $EXV
}

by willbs at 2013-02-20 08:06:44

thanks vstarmanv, your reply worked on the second part of my question

can anyone help with the first part of my problem, placing the message "Process $Procs NOT running!" into a variable?

by ArtB0514 at 2013-02-20 08:39:13

Create an empty array outside of the foreach loop and append to it inside.
$Proc = "winword", "svchost", "certsrv", "goopy", "OUTLOOK"
$Data = @()
$Script = {param($Procs) if (-not (get-process $Procs -errorAction SilentlyContinue) ) { "Process $Procs NOT running!" }}
foreach ($EXV in $Proc) {
$Data += invoke-command -scriptblock $Script -computername 192.168.0.102 -credential $cred -ArgumentList $EXV
}

(I moved the script block outside the invoke-command to shorten the line and [hopefully] make it a bit more readable.)

by willbs at 2013-02-20 09:15:31

here is the final code that does everything i was asking for, i ended up doing an -outvariable and piped it to the report file, here is my example

# Test the list for processes that ARE NOT running
$Proc = "winword", "svchost", "certsrv", "goopy", "OUTLOOK"
foreach($EXV in $Proc)
{
invoke-command -scriptblock { param($Procs) if ( -not (get-process $Procs -errorAction SilentlyContinue) ) { "Process $Procs IS NOT Running!" }}
-computername 192.168.0.102 -credential $cred -ArgumentList $EXV -OutVariable out | out-file -filepath C:\TestReport.txt -append # this is all one line
}

i'll probably apply ArtB0514's approach to shorten the lines

thanks everyone