Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

powershell - Check if process is running on multiple remote computers and copy a file if process is NOT running, keep list of those which succeeded

I need to copy a file to multiple computers, but can only do so if a particular app (process) is not running.

I know I can use Invoke-Command to run a script (scriptblock) on a list of machines.

But how can I check if process is running on the machine and then only copy file if it is not running.

So that at the end of running against a load of computers I can easily see those which succeeded e.g. process was not running and file was copied

Thanks

UPDATE:

I am assuming something like this will do the first bits of what I am asking, but how to visually show or log success or failure so I know which computers have been done - doesn't need to be anything fancy, even if simply a variable that holds computername of those where process wasn't running and file was copied okay

Invoke-Command -ComputerName PC1, PC2, PC3 -ScriptBlock {

If ((Get-process -Name notepad -ea SilentlyContinue) -eq $Null){

   Copy-Item -Path "\server01c$estfile.txt" -Destination "C:estfile.txt" -Force

}

}
question from:https://stackoverflow.com/questions/65833581/check-if-process-is-running-on-multiple-remote-computers-and-copy-a-file-if-proc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$Procs = invoke-command -ComputerName PC1 { get-process | Select Name }
    If($Procs -notmatch "Notepad"){  Copy-Item -Path "\server01c$estfile.txt" -Destination "\$PC1c$est" -Force}

edited:

$computers = @("PC1","PC2","PC3")
    Foreach($computer in $computers){
$Procs = invoke-command -ComputerName $computer { Get-Process Notepad -ErrorAction SilentlyContinue}
    If(!$Procs){"$Computer - not running Notepad"; Copy-Item -Path "\server01c$estfile.txt" -Destination "\$computerc$est" -Force}
    elseif($Procs){"$Computer - is running Notepad"}
    }

Edit2(for clean output):

$computers = @("PC1","PC2","PC3")
$RNote = @()
$NNote = @()
$off   = @()

    Foreach($computer in $computers){
$TestC = Test-Connection -ComputerName $computer -Count 1
    If(!($TestC)){$off += $computer} Else{

$Procs = invoke-command -ComputerName $computer { Get-Process Notepad -ErrorAction SilentlyContinue}
    If(!$Procs){$NNote +=$computer; Copy-Item -Path "\server01c$estfile.txt" -Destination "\$computerc$est" -Force}
    elseif($Procs){$RNote +=$computer}
    }
}

$leng =[array]$RNote.count,$NNote.Count,$off.count
[int]$max = ($leng | measure -Maximum).Maximum
    for($i=0; $i -lt $max;$i++){
[pscustomobject]@{
    "Notepad On"  = $(if ($RNote[$i]){$RNote[$i]})
    "Notepad Off" = $(if ($NNote[$i]){$NNote[$i]})
    "Offline "    = $(if ($off[$i]){$off[$i]})
        }
    }               

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...