2

I'm looking for a method to kill a .exe of disconnected terminal session. It's about Navision; We have limited licences and want to keep killing the ones that aren't in use.

I know there are methods to completely logoff the disconnected session, but I prefer to only close this specific progress.

I did find a blogpost with a code that is discribed to do exactly what i'm looking for. A powershell script that's sheduled to scan for a certain .exe on disconnected sessions, and kill it off. http://www.interworks.com/blogs/jpoehls/2012/12/12/killing-processes-disconnected-terminal-service-sessions the code is posted on this webpage.

Unfortunately it doesn't work, I get the following error:

PS C:\scripts> .\navscript.ps1
An empty pipe element is not allowed.
At C:\scripts\navscript.ps1:91 char:10
+         | <<<<  % { $id = $_;
+ CategoryInfo          : ParserError: (:) [
+ FullyQualifiedErrorId : EmptyPipeElement

Besides alot of time spent on google to find different solutions, I also tried to remove the empty line at line 90. this resulted in another error:

PS C:\scripts> .\navscript.ps1
Exception calling "Substring" with "2" argument(s): "Lengte kan niet minder dan nul zijn.
Parameternaam: length"
At C:\scripts\navscript.ps1:52 char:44
+                     ;"ID" = $line.Substring <<<< ($line.IndexOf(" ",     $starters.Username), $starters.ID - $line.IndexO
f(" ", $starters.Username) + 2).trim()
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

I hope someone has an idea how to get this working.

4

1 回答 1

1

query session命令输出是本地化的,因此如果Get-Sessions第一行与预期的行不匹配,则无法触发其硬编码到函数中的子字符串:

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE

请注意,该行的第一个符号有一个空格。

因此,要修复脚本,您必须query session在目标 PC 上运行,获取第一行并手动更新第 40 到 45 行,使其在本地化输出的第一行中显示正确的字符序列。

于 2015-06-17T08:24:07.303 回答