Code Review and Guidance


I’m very new to powershell. I got a book to help me learn the basics but have also have googled a lot. I was able to write a script that would run a search from in in AD and spit back if the PC was online, if someone was logged in or if the PC was unreachable.

 Import-Module ActiveDirectory
$job2 = 0
(Get-ADComputer -Filter 'name -like "PCNAME/NAMINGCONVENTION"').name | ForEach-Object { 
    If (test-connection -computername $_ -Quiet) {
        $job = (gwmi win32_computersystem -comp $_).USername
        If ($job){
        Write-Host "$_ : $job is currently logged on" -ForegroundColor Yellow}
            Write-Host "$_ : is available to remote in" -ForegroundColor Green
        Write-Host "$_ : is offline/unreachable" -ForegroundColor Red
Write-Host "There are "$job2 "PC's available." -ForegroundColor White 

I was hoping to get some feedback. I understand the basics of what I’ve done here but some of this was just copy/paste/edit/test until I was able to produce the results I wanted. Any better techniques I could have used?

I have another idea I want to start but am having trouble wrapping my head around how to start. I would like to create a script that will look up a user in AD and spit out if the user is logged on the network and if so which PC they are using. I know that it’s my lack of knowledge but I’m getting dead ends when trying to research something. I am hoping for some guidance as to what to lookup/research to get me on track.