søndag 2. september 2012

Pludge.com

Visit my norwegian blog at http://www.pludge.com

onsdag 9. desember 2009

Powershell vs anything else?

If you have read my first post, you know that I've just testet Microsoft Powershell. I am an "old" DOS-veteran and DOS batching has for some time now started to get too limited for me, so I thought I would check out Powershell.

I have just testet it for one day or so, but I can't find myself comfortable with it. Earlier this day, I was gonna use telnet to test a SMTP-server. The only problem is that usual commands like for example telnet does not exist in powershell. If I was going to use Powershell it should be a little more like DOS, but the scripting should be a little like Visual Basic so you won't get into trouble if you want do replace special characters like = or \.

I know you can do this in DOS, but it is just too much fuzz just to find out how to do it. Also if you find a tool that can do it, it may not work without a bunch of wildcards that I don't have the time to spend a lot of research on.

Some of you may suggest Cygwin, vbscript or maybe a DOS clone. I don't like vbscript. I want to do the scripting command-line like, so maybe Cygwin is the right choice? Just this moment I tested Win-bash. It didn't work so well most likely because of special Norwegian characters (most likely it is fixable too).

I like bash (Linux), but don't have so much scripting experience in it.. But with the little I have done, it seems easy and logical.

Does anybody have any recommendations for me on this topic?
I will be looking in to an alternate scripting environment for Windows the next days. I don't think I will be sticking to powershell or plain DOS batching, but I'm not sure of anything right now..

Edit: It seems like I'm going for Telnet afterall. I've testet VBscript the last days and it is simple enough, but Powershell seems to be more simple and I like better to work in a "terminal" rather than scripting in Windows. I feel that a "terminal" gives me more control over what I am doing.


The transition will be real simple for me, in scripting and everything since I can use DOS-commands in Powershell like this: "cmd /c dir c:\temp". Or for scripting: "$testvariable=cmd /c dir c:\temp".

About this blog


This is my blog in English. It is not my main blog. My main blog is at http://www.pludge.com (Google translate link since it is in Norwegian).

I'm an IT professional. My knowledge and experience is mainly in Microsoft's products, but I also have a little experience in Linux (Ubuntu), PHP/MySQL/HTML (I think I'm pretty outdated here, especially in HTML) and probably in some more since I like testing things.

This blog will be used for sharing some of the problems I solve in my work. Some issues, I find my selves spending up to an hour with Google just to find something that works. This is rare, but it happends now and then. Most often it is a problem that either has a general error message that most programs or services in Windows may generate or it is no error message at all, and I have to play detective to find out why the system is having this issue. Anyway, by posting some solutions here, I will help others like me and maybe get som info back at these issues.

About the picture: Probably you might know this allready, but I'm not that old ;-). My sister made this for me as a surprise for my birthday party a few weeks ago. I'll be 30 soon.

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.