Jaigene Kang
Jaigene Kang

Reputation: 164

Sending multiple commands with psexec

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*

Upvotes: 2

Views: 20734

Answers (3)

serdar
serdar

Reputation: 147

I always use like this way :) and works properly

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

Upvotes: 3

latkin
latkin

Reputation: 16792

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

Upvotes: 8

Keith Hill
Keith Hill

Reputation: 201682

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

Upvotes: 1

Related Questions