Page 1 of 1

Powershell to logoff and clean profile on another server

Posted: Thu Apr 26, 2018 11:30 am
by jimhung777
Hey guys,



Here is the code I execute successfully.

Code: Select all

#TI
sCommand = 'Powershell D:\TEST\file.ps1';

ExecuteCommand(sCommand, 0);

#PS
New-Item c:\TEST\new_file.txt -type file


However, when I tried to use TI to call PowerShell to log off a user or clean user's profile on another server, it did not work!
I'm sure this code can be executed by Powershell.exe merely.

Code: Select all

#servername: XENPW55CV.woo.unatu
#username: 177684
#serverOA: 10.35.36.30

#clean user profile
(Get-WmiObject Win32_UserProfile -ComputerName XENPW55CV.woo.unatu -Filter "localpath='C:\\Users\\177684'").Delete()

#log off user
logoff ((quser /server:10.35.36.30 | Where-Object { $_ -match 177684 }) -split ' +')[2] /server:10.35.36.30


I don't know what's wrong... :o :o :o Is there anyone could help us or give us some direction?

Thanks so much! :)

Re: Powershell to logoff and clean profile on another server

Posted: Thu Apr 26, 2018 12:51 pm
by PavoGa
Might try changing to this:

Code: Select all

sCommand = 'Powershell D:\TEST\file.ps1 2>ErrorFile.txt';
and try to capture any errors the Powershell script is returning. The TI code looks fine.

Re: Powershell to logoff and clean profile on another server

Posted: Thu Apr 26, 2018 1:15 pm
by Wim Gielis
Can you run any PS script?

Re: Powershell to logoff and clean profile on another server

Posted: Thu Apr 26, 2018 2:08 pm
by jimhung777
PavoGa wrote: Thu Apr 26, 2018 12:51 pm Might try changing to this:

Code: Select all

sCommand = 'Powershell D:\TEST\file.ps1 2>ErrorFile.txt';
and try to capture any errors the Powershell script is returning. The TI code looks fine.
Hi PavoGa,
Thank you for your idea!
Even though I can't run the code you provide, I still look for an alternative to reach the same result..
Here it is.

Code: Select all

#PS
#log off user
logoff ((quser /server:10.35.36.30 | Where-Object { $_ -match 177684 }) -split ' +')[2] /server:10.35.36.30

set-content D:\TEST\error.log $error

Wim Gielis wrote: Thu Apr 26, 2018 1:15 pm Can you run any PS script?
Hi Wim, definitely.
I can run PowerShell with the code above, but when it comes to using TI to call .ps1, it failed to log off the user.
And I also put PS script in TI, still not work now.
Hopefully, with the help of PavoGa, I got some clue.

Code: Select all

Error [5]:Access is denied.

Could not logoff session ID 1, Error code 5

Re: Powershell to logoff and clean profile on another server

Posted: Thu Apr 26, 2018 9:16 pm
by macsir
Check your service account you are using for TM1. It might be the service account don't have proper privilege compared with the one you run the PS script.

Re: Powershell to logoff and clean profile on another server

Posted: Fri Apr 27, 2018 2:40 am
by jimhung777
macsir wrote: Thu Apr 26, 2018 9:16 pm Check your service account you are using for TM1. It might be the service account don't have proper privilege compared with the one you run the PS script.
Hi macsir,

You are GOAT.

In DEV environment, it works according to your instruction.
But it still has some problem in PROD, I will upgrade from Powershell 2.0 to 4.0 to see if it is about the version. (now DEV 4.0, PROD 2.0)

Thanks!