PoshCode Archive  Hex Artifact Content

Artifact 3b0b510e993705b0570cc65a07d60f3e7587c122097869719c98ffea034352c5:

  • File Terminate-process-user.ps1 — part of check-in [be72d2dda1] at 2018-06-10 13:29:30 on branch trunk — #Ty Lopes – Calgary – Oct 2012 (user: Ty Lopes size: 1450)

0000: 23 20 65 6e 63 6f 64 69 6e 67 3a 20 61 73 63 69  # encoding: asci
0010: 69 0d 0a 23 20 61 70 69 3a 20 70 6f 77 65 72 73  i..# api: powers
0020: 68 65 6c 6c 0d 0a 23 20 74 69 74 6c 65 3a 20 54  hell..# title: T
0030: 65 72 6d 69 6e 61 74 65 20 70 72 6f 63 65 73 73  erminate process
0040: 20 2f 20 75 73 65 72 0d 0a 23 20 64 65 73 63 72   / user..# descr
0050: 69 70 74 69 6f 6e 3a 20 23 54 79 20 4c 6f 70 65  iption: #Ty Lope
0060: 73 20 e2 80 93 20 43 61 6c 67 61 72 79 20 e2 80  s ... Calgary ..
0070: 93 20 4f 63 74 20 32 30 31 32 0d 0a 23 20 76 65  . Oct 2012..# ve
0080: 72 73 69 6f 6e 3a 20 30 2e 31 0d 0a 23 20 74 79  rsion: 0.1..# ty
0090: 70 65 3a 20 73 63 72 69 70 74 0d 0a 23 20 61 75  pe: script..# au
00a0: 74 68 6f 72 3a 20 54 79 20 4c 6f 70 65 73 0d 0a  thor: Ty Lopes..
00b0: 23 20 6c 69 63 65 6e 73 65 3a 20 43 43 30 0d 0a  # license: CC0..
00c0: 23 20 78 2d 70 6f 73 68 63 6f 64 65 2d 69 64 3a  # x-poshcode-id:
00d0: 20 33 36 38 39 0d 0a 23 20 78 2d 61 72 63 68 69   3689..# x-archi
00e0: 76 65 64 3a 20 32 30 31 32 2d 31 30 2d 31 34 54  ved: 2012-10-14T
00f0: 32 33 3a 33 37 3a 32 39 0d 0a 23 20 78 2d 70 75  23:37:29..# x-pu
0100: 62 6c 69 73 68 65 64 3a 20 32 30 31 32 2d 31 30  blished: 2012-10
0110: 2d 31 32 54 30 38 3a 31 31 3a 30 30 0d 0a 23 0d  -12T08:11:00..#.
0120: 0a 23 20 23 28 54 72 6f 79 20 69 73 20 61 20 68  .# #(Troy is a h
0130: 75 67 65 20 6e 65 72 64 29 0d 0a 23 20 23 48 6f  uge nerd)..# #Ho
0140: 77 20 74 6f 20 6b 69 6c 6c 20 61 20 70 72 6f 63  w to kill a proc
0150: 65 73 73 20 62 79 20 75 73 65 72 6e 61 6d 65 0d  ess by username.
0160: 0a 23 20 23 4f 72 69 67 69 6e 61 6c 6c 79 20 63  .# #Originally c
0170: 72 65 61 74 65 64 20 66 6f 72 20 61 20 73 63 72  reated for a scr
0180: 69 70 74 20 28 72 75 6e 6e 69 6e 67 20 75 6e 64  ipt (running und
0190: 65 72 20 61 20 73 63 68 65 64 20 74 61 73 6b 29  er a sched task)
01a0: 20 74 68 61 74 20 77 61 73 20 6e 6f 74 20 63 6c   that was not cl
01b0: 6f 73 69 6e 67 20 65 78 63 65 6c 20 61 70 70 6c  osing excel appl
01c0: 69 63 61 74 69 6f 6e 20 61 66 74 65 72 20 65 6e  ication after en
01d0: 75 6d 65 72 61 74 69 6e 67 20 74 68 72 6f 75 67  umerating throug
01e0: 68 74 20 74 68 65 20 65 78 63 65 6c 20 66 69 6c  ht the excel fil
01f0: 65 0d 0a 23 20 23 50 6f 77 65 72 73 68 65 6c 6c  e..# #Powershell
0200: 20 64 6f 65 73 20 6e 6f 74 20 73 65 65 6d 20 74   does not seem t
0210: 6f 20 63 6c 6f 73 65 20 65 78 63 65 6c 20 70 72  o close excel pr
0220: 6f 70 65 72 6c 79 20 75 73 69 6e 67 20 74 68 65  operly using the
0230: 20 77 6f 72 6b 62 6f 6f 6b 2e 63 6c 6f 73 65 20   workbook.close 
0240: 66 75 6e 63 74 69 6f 6e 0d 0a 23 20 23 4e 6f 74  function..# #Not
0250: 65 3a 20 63 6f 75 6c 64 20 6e 6f 74 20 6b 69 6c  e: could not kil
0260: 6c 20 74 68 65 20 70 72 6f 63 65 73 73 20 77 68  l the process wh
0270: 65 6e 20 49 20 69 6e 63 6c 75 64 65 64 20 74 68  en I included th
0280: 65 20 65 78 74 65 6e 73 69 6f 6e e2 80 a6 20 65  e extension... e
0290: 78 61 6d 70 6c 65 20 6e 6f 74 65 70 61 64 2e 65  xample notepad.e
02a0: 78 65 20 68 61 73 20 74 6f 20 62 65 20 6e 6f 74  xe has to be not
02b0: 65 70 61 64 20 66 6f 72 20 74 68 65 20 24 70 72  epad for the $pr
02c0: 6f 63 65 73 73 20 76 61 72 69 61 62 6c 65 0d 0a  ocess variable..
02d0: 23 0d 0a 23 54 79 20 4c 6f 70 65 73 20 2d 20 43  #..#Ty Lopes - C
02e0: 61 6c 67 61 72 79 20 2d 20 4f 63 74 20 32 30 31  algary - Oct 201
02f0: 32 0d 0a 23 28 54 72 6f 79 20 69 73 20 61 20 68  2..#(Troy is a h
0300: 75 67 65 20 6e 65 72 64 29 0d 0a 0d 0a 23 48 6f  uge nerd)....#Ho
0310: 77 20 74 6f 20 6b 69 6c 6c 20 61 20 70 72 6f 63  w to kill a proc
0320: 65 73 73 20 62 79 20 75 73 65 72 6e 61 6d 65 0d  ess by username.
0330: 0a 23 4f 72 69 67 69 6e 61 6c 6c 79 20 63 72 65  .#Originally cre
0340: 61 74 65 64 20 66 6f 72 20 61 20 73 63 72 69 70  ated for a scrip
0350: 74 20 28 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72  t (running under
0360: 20 61 20 73 63 68 65 64 20 74 61 73 6b 29 20 74   a sched task) t
0370: 68 61 74 20 77 61 73 20 6e 6f 74 20 63 6c 6f 73  hat was not clos
0380: 69 6e 67 20 65 78 63 65 6c 20 61 70 70 6c 69 63  ing excel applic
0390: 61 74 69 6f 6e 20 61 66 74 65 72 20 65 6e 75 6d  ation after enum
03a0: 65 72 61 74 69 6e 67 20 74 68 72 6f 75 67 68 74  erating throught
03b0: 20 74 68 65 20 65 78 63 65 6c 20 66 69 6c 65 0d   the excel file.
03c0: 0a 23 50 6f 77 65 72 73 68 65 6c 6c 20 64 6f 65  .#Powershell doe
03d0: 73 20 6e 6f 74 20 73 65 65 6d 20 74 6f 20 63 6c  s not seem to cl
03e0: 6f 73 65 20 65 78 63 65 6c 20 70 72 6f 70 65 72  ose excel proper
03f0: 6c 79 20 75 73 69 6e 67 20 74 68 65 20 77 6f 72  ly using the wor
0400: 6b 62 6f 6f 6b 2e 63 6c 6f 73 65 20 66 75 6e 63  kbook.close func
0410: 74 69 6f 6e 0d 0a 23 4e 6f 74 65 3a 20 63 6f 75  tion..#Note: cou
0420: 6c 64 20 6e 6f 74 20 6b 69 6c 6c 20 74 68 65 20  ld not kill the 
0430: 70 72 6f 63 65 73 73 20 77 68 65 6e 20 49 20 69  process when I i
0440: 6e 63 6c 75 64 65 64 20 74 68 65 20 65 78 74 65  ncluded the exte
0450: 6e 73 69 6f 6e 2e 2e 2e 20 65 78 61 6d 70 6c 65  nsion... example
0460: 20 6e 6f 74 65 70 61 64 2e 65 78 65 20 68 61 73   notepad.exe has
0470: 20 74 6f 20 62 65 20 6e 6f 74 65 70 61 64 20 66   to be notepad f
0480: 6f 72 20 74 68 65 20 24 70 72 6f 63 65 73 73 20  or the $process 
0490: 76 61 72 69 61 62 6c 65 0d 0a 0d 0a 24 75 73 65  variable....$use
04a0: 72 6e 61 6d 65 20 3d 20 22 75 73 65 72 6e 61 6d  rname = "usernam
04b0: 65 22 0d 0a 24 70 72 6f 63 65 73 73 3d 20 22 6e  e"..$process= "n
04c0: 6f 74 65 70 61 64 22 0d 0a 0d 0a 24 6f 77 6e 65  otepad"....$owne
04d0: 72 73 20 3d 20 40 7b 7d 0d 0a 67 77 6d 69 20 77  rs = @{}..gwmi w
04e0: 69 6e 33 32 5f 70 72 6f 63 65 73 73 20 7c 25 20  in32_process |% 
04f0: 7b 24 6f 77 6e 65 72 73 5b 24 5f 2e 68 61 6e 64  {$owners[$_.hand
0500: 6c 65 5d 20 3d 20 24 5f 2e 67 65 74 6f 77 6e 65  le] = $_.getowne
0510: 72 28 29 2e 75 73 65 72 7d 0d 0a 67 65 74 2d 70  r().user}..get-p
0520: 72 6f 63 65 73 73 20 24 70 72 6f 63 65 73 73 20  rocess $process 
0530: 7c 20 73 65 6c 65 63 74 20 70 72 6f 63 65 73 73  | select process
0540: 6e 61 6d 65 2c 49 64 2c 40 7b 6c 3d 22 4f 77 6e  name,Id,@{l="Own
0550: 65 72 22 3b 65 3d 7b 24 6f 77 6e 65 72 73 5b 24  er";e={$owners[$
0560: 5f 2e 69 64 2e 74 6f 73 74 72 69 6e 67 28 29 5d  _.id.tostring()]
0570: 7d 7d 20 7c 20 77 68 65 72 65 2d 6f 62 6a 65 63  }} | where-objec
0580: 74 20 7b 24 5f 2e 6f 77 6e 65 72 20 2d 65 71 20  t {$_.owner -eq 
0590: 24 75 73 65 72 6e 61 6d 65 7d 20 7c 20 6b 69 6c  $username} | kil
05a0: 6c 20 2d 66 6f 72 63 65 0d 0a                    l -force..