2

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*

3 Answers 3

8

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
Sign up to request clarification or add additional context in comments.

1 Comment

I over thought this. This ended up being the best. I ended up just passing a parameter to a .cmd script
3

I always use like this way :) and works properly

psexec \\COMPUTER -e cmd /c (COMMAND1 ^& COMMAND2 ^& COMMAND3)

Comments

1

If you just want to concatencate commands on a single line then use the statement separator ; e.g.:

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*

From looking at the PSEXEC usage, it doesn't appear to allow you to specify multiple programs in a single invocation of PSEXEC.

BTW you could use PowerShell's remoting capability - assuming server has WMF 2.0 or higher installed and has enabled WSMan remoting e.g.:

Invoke-Command -ComputerName server {Stop-Process -Name soffice*,swriter*,scalc*,simpress* -Force}

If you can't go the remoting route, another approach would be to create a PowerShell function:

function Stop-RemoteProcess([string]$ComputerName, [string[]]$Programs, [string]$UserName)
{
    foreach ($program in $Programs)
    {
        psexec "\\$ComputerName" taskill /f /t /fi "USERNAME eq $UserName" /im $program
    }
}

Stop-RemoteProcess server soffice*,swriter*,scalc*,simpress* JohnDoe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.