-1

How can i get: (works)

$Procname = 'notepad'

Invoke-Command -ComputerName sandbox -ScriptBlock {

    if ((Get-Process | Where-Object {$_.Name -eq $Procname}) -eq $null) {
        Write-Host 'null' -ForegroundColor Red
    } else {
        Write-Host running -ForegroundColor Green
    }
}

Too look like this? (no work)

$Procname = 'notepad'
$chk = (Get-Process | Where-Object {$_.Name -eq $Procname})

Invoke-Command -ComputerName sandbox -ScriptBlock {

    if ($chk -eq $null) {
        Write-Host 'null' -ForegroundColor Red
    } else {
        Write-Host running -ForegroundColor Green
    }
}

The trouble i am having is getting the Get-process | where-object to run from outside the scriptblock, I can pass text OK from outside the scriptblock (IE the $Procname) but i am trying to pass the command, what special things do i need to do?

I have asked uncle google, but I think I'm asking the wrong question.

I have also tried $chk as a Param and an arg but they didn't work for me

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
madman
  • 27
  • 5
  • Are you trying to see if `$ProcName` is running on the remote server? Or are you trying to reference the results of `$chk` (run on the local server) from within the scriptblock being executed on the remote server? – TheMadTechnician Dec 11 '17 at 19:57
  • Also, you aren't passing `$Procname`, so you aren't really able to access that string like you think you are. All you are doing is getting processes, looking for processes who's name is equal to `$null`, which is nothing (i.e. $null), so you are asking if `$null -eq $null`, which should always be true. – TheMadTechnician Dec 11 '17 at 20:01
  • I'm trying to see if $Procname is running on the remote server, $chk should be executed on the remote side not the local side – madman Dec 11 '17 at 20:06
  • @TheMadTechnician -- THANK YOU – madman Dec 11 '17 at 21:05

1 Answers1

0

Ok, it looks like you want to know how to reference a local variable in a remote execution via Invoke-Command. Totally something you can do with the $using: context. For example, to make your script look to see if notepad is running on your computer, and then reference those results on a remote system you could do this:

$Procname = 'notepad'
$chk = (Get-Process | Where-Object {$_.Name -eq $Procname})

Invoke-Command -ComputerName sandbox -ScriptBlock {

    if ($using:chk -eq $null) {
        Write-Host 'null' -ForegroundColor Red
    } else {
        Write-Host 'running' -ForegroundColor Green
    }
}

Similarly, if you want to see if notepad is running on the remote server you could do this:

$Procname = 'notepad'

Invoke-Command -ComputerName sandbox -ScriptBlock {

    if ((Get-Process | Where-Object {$_.Name -eq $using:Procname}) -eq $null) {
        Write-Host 'null' -ForegroundColor Red
    } else {
        Write-Host 'running' -ForegroundColor Green
    }
}

But personally I think that is hard to read, I'd reverse it and do:

$Procname = 'notepad'

Invoke-Command -ComputerName sandbox -ScriptBlock {

    if (Get-Process | Where-Object {$_.Name -eq $using:Procname}) {
        Write-Host 'running' -ForegroundColor Green
    } else {
        Write-Host 'null' -ForegroundColor Red
    }
}

Or without running anything on the remote server directly:

$Procname = 'notepad'
$chk = Get-Process -Computer sandbox | ?{$_.Name -eq $Procname}
If($chk){
    Write-Host 'Running' -ForegroundColor Green
}else{
    Write-Host 'Null' -ForegroundColor Red
}
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56