Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending multiple commands with psexec

Tags:

powershell

I'd like to condense

psexec \\server taskill /f /t /fi "USERNAME eq $username" /im soffice*
psexec \\server taskill /f /t /fi "USERNAME eq $username" /im swriter*
psexec \\server taskill /f /t /fi "USERNAME eq $username" /im scalc*
psexec \\server taskill /f /t /fi "USERNAME eq $username" /im simpress*

Into one psexec command. Normally I'd try the & operator to do so & so but since I'm doing this all in PS, it doesn't seem to like that. I tried an array of () and "" but it doesn't seem to like those either.

EDIT [answer]

Ended up just copying a .cmd (BAT) file and making a shortcut in my $PROFILE locally.

function flushlibra
{
param([string]$user = "")
if ($user -eq "")
{
    $user = Read-Host "User to nuke LibraOffice proccesses: "
}

psexec -c "\\unc\path\to\flushlibra.cmd" $user
}

.cmd file

taskkill /f /t /fi "USERNAME eq %1" /im soffice*
taskkill /f /t /fi "USERNAME eq %1" /im swriter*
taskkill /f /t /fi "USERNAME eq %1" /im scalc*
taskkill /f /t /fi "USERNAME eq %1" /im simpress*
like image 791
Jaigene Kang Avatar asked Dec 19 '25 09:12

Jaigene Kang


1 Answers

psexec allows you to invoke a batch script remotely, too, not just single commands. Use the -c switch to indicate that the script should be copied to the remote system.

So if you locally have a script KillProcs.cmd:

taskill /f /t /fi "USERNAME eq $username" /im soffice*
taskill /f /t /fi "USERNAME eq $username" /im swriter*
taskill /f /t /fi "USERNAME eq $username" /im scalc*
taskill /f /t /fi "USERNAME eq $username" /im simpress*

Then you can run that remotely like this

psexec \\server -c c:\localpath\KillProcs.cmd
like image 147
latkin Avatar answered Dec 21 '25 01:12

latkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!