2

I'm writing a script to get the Username of any user logged on to any computer. It works fine with 2 Exceptions.

Here is the code:

$computername = Get-Content 'I:\NodeList\SWSL.txt'
Remove-item "I:\NodeList\UsersPC.txt"
foreach ($computer in $computername) 
{
    if(Test-Connection -Computername $Computer -BufferSize 16 -Count 1 -ea 0)
    {        
        $Uinfo = gwmi win32_computersystem -comp $computer | select Username,Caption #,Manufacturer
        $Uinfo | Out-File -Filepath "I:\NodeList\UsersPC.txt" -Append
        Write-Host $Uinfo
    }else{ 
        Write-Host $computer " Is offline, not responing to ping"
        $computer + " Is offline!" | Out-File -Filepath     "I:\NodeList\UsersPC.txt" -Append
    }
}

First the Output is:

USername       Caption 
--------       ------- 
BINYAN\Charlie SYD-WS04

But I would only like to have:

Charlie SYD-WS04

The domain is always the same, so I just need the username and the computer name, and NOT the headings or the "---"

2nd Problem is, we are a render farm and all renders are processed on a user account Called "Render". For those computers I only get the computer name, Username is Blank. This user account is a Domain user, but not in an OU like the others (Sydney, Melbourne, Brisbane). It resides in the default "Users" folder in AD.

1 Answer 1

1

You could remove the domain name using a regex that replaces everything until and including a \: '.*?\\'

You get rid of the heading using select -expand or using a foreach loop where you select the properties using $_.PROPERTY

So your statement could look like this:

$Uinfo = gwmi win32_computersystem -comp $computer | 
 foreach { '{0} {1}' -f ($_.Username -replace '.*?\\'), $_.Caption }

I have no answer for / I dont understand your second question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.