Tuesday, May 26, 2015

Powershell associate users to pcs for the entire domain

Let's start saying that when I write a script I try not only to achieve the goal but I also try  to test some specific cmdlets, even if in this specific script use this cmdlet is not the best way to do a task.
So for sure the series "Complex, improvable, optimizable"  doesn't want to be the BEST SCRIPT ever written for the purpose, but something to do a task and, for sure, improvable.
This was a sort of introduction....let's go ahead with it.

We didn't have a software that make this association....I mean a software with an agent that communicates periodically all info about every single pc on the status, software or accounts.
We need to know, more or less, in real time which user is logged on a pc.
This is a time expensive operation to do with Powershell : for each single pc we need to wait for the WMI connection. Let's imagine for hundreds of them.
So since the users don't change pc every day (this could be a real hell) I decided to create an excel file that could periodically be refreshed, but till the refresh it will be available to be queried.

So the script asks you if you want to refresh the file. Obviously if the file doesn't exist you have to reply YES to create it the first time.
This is the long part of the task : the script will search in the entire directory and "scan" every single pc to retrieve computername and username.

If you don't want to scan and refresh the script asks you another question : if you want to search on the local file or a targeted search. The targeted search was something that I wrote because the Sites in our AD corresponds to a dedicated OU. So I can launch a search in a restricted environment and abbreviate the response time (and additionally make the search in "live mode" instead of query the "static" file)
.
SORRY FOR MISSING INDENDATION.

You have to customize some parts of it



For sure adjustable in every single line but usable more or less in few minutes.
Comments and critics are welcome
Hope this helps
See you soon 

No comments:

Post a Comment