Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell Scriptblock with Elevated Credentials

$
0
0

We have an application that only allows one instance per station, regardless of the user.

With Windows 7 pro and fast user switching we are running into issue where the second user is unable to access the application.

We have created a multi-part powershell script that will run during the login on these stations; it will logoff all disconnected sessions.  And before anyone points it out - yes it has an embedded username/password in the script - right now that's the least of my worries.

Script One calls Script Two passing it elevated credentials that permit it to force out the other logged in user(s).

$username = '.\mylocaladmin'
$password = 'mypassword'
$credentials = New-Object System.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.

function RemoveSpace([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] 
}

It works as is - calling one script from the other script and I can force out the second user without issue.  However, I am looking to try and merge these two items into one single script easier for distribution, etc.

Can anyone offer some suggestions?



Viewing all articles
Browse latest Browse all 15028

Trending Articles



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