Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Regarding this script....Powershell Scriptblock with Elevated Credentials

$
0
0

Powershell Scriptblock with Elevated Credentials is being tested under a GPO on my domain to force the logoff any user, who locks and walks away from a PC, by the next user who logs in using 'Switch User. This is the script in question.

$username ='.\mylocaladmin'
$password ='mypassword'
$credentials =New-ObjectSystem.Management.Automation.PSCredential-ArgumentList@($username,(ConvertTo-SecureString-String $password -AsPlainText-Force))

$pshell ="powershell.exe"
$uname =[Environment]::UserName
$scriptname ="c:\forcelogoff.ps1 $uname"

start-process $pshell $scriptname -Credential($credentials)

Script Two parses the device, finds the disconnected accounts and logs them off.

functionRemoveSpace([string]$text){
    $private:array = $text.Split(" ",`
    [StringSplitOptions]::RemoveEmptyEntries)
    [string]::Join(" ", $array) }

$quser = quser
$uname = $($args[0]) #[Environment]::UserName
foreach ($sessionString in $quser) {
    $sessionString = RemoveSpace($sessionString)
    $session = $sessionString.split()

    if ($session[0].Equals(">$uname")) {
    continue }
    if ($session[0].Equals("USERNAME")) {
    continue }
    $result = logoff $session[1]
}

The script would indicate that disconnected users don't have an ID number, the script checks this and logs off those with no ID. Unfortunately the disconnected users on my W7 Enterprise clients appear to keep their ID numbers and the script logseveryone off including the latest user.

Is this a quirk of Enterprise?

Is there a way that the script could run but looking at the status of the users - ie 'disconnected' rather than ID number. I am a complete newbie to Powershell so editing this myself is a problem. However if all I get is hints and clues I can give it a go with a little help from my colleagues.

Many thanks


Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>