onsdag 9. desember 2009

Powershell script for checking workstations that are online

I recently made my first powershell script. What the script does is get all the computers from the domain controller, trim the results and check if they are alive. If the computers are alive, it will fire up the Event Viewer (or whatever you like) against the remote computer.

Since I have a lot of customers and I do a monthly check of some of the customers servers and workstations, I needed this script so I can be more efficient.

#Open Workstations by Onan den 08.12.2009
#Retrieves all the computers from Active Directory
#and opens the event viewer or whatever you want against the remote computer.
#Version 0.1
# You may have to type the following command in powershell to make this script work:
#set-executionpolicy unrestricted
#
#You have to adapt the script so it fits your organization.
#You may need psexec (Google Microsoft Sysinternals)
#
#Changelog:
#09.12.2009 - Translated to English. Included comments of the code.


#Retrieving the computer names from Active Directory.
$computers=dsquery computer -name *
#You might want to activate the following line so you can do some copy'n replace in this script.
#write-output $computers >computernames.txt
#Trim. Replacing unnecessary text with nothing. You will have to change these lines so it fits to your organization.
$computers=$computers -replace('"CN=DC02,OU=Domain Controllers,DC=domain,DC=local"')
$computers=$computers -replace('"CN=')
$computers=$computers -replace(',CN=Computers,DC=domain,DC=local"')
$computers=$computers -replace(',OU=SBSComputers,OU=Computers,OU=MyBusiness,DC=domain,DC=local"')
#removing empty line breaks.
$computers=$computers -ne ""
#cycling through the computernames in the $computers variable
ForEach($_ in $computers)
{
#Creating a ping object
$ping = new-object System.Net.NetworkInformation.Ping
#Doing a ping
$Reply = $ping.send($_)
#Doing stuff if computer is online
if ($Reply.status –eq "Success")
    {
    #Opening .....
    Write-Output "Åpner $_"
    #Adding some firewall rules to the computer so you may manage it.
    .\psexec \\$_ netsh firewall set service remoteadmin enable
    .\psexec \\$_ netsh firewall set service FILEANDPRINT ENABLE
    #Opening Explorer against the computers C-drive
    explorer.exe \\$_\c$
    #Opening Event viewer against the remote computer
    eventvwr.exe \\$_
    }
}
#Setting the execution of scripts to default.
#set-executionpolicy Restricted

I know that there can be improvements in this script, so please tell me if you have made some.

Ingen kommentarer:

Legg inn en kommentar