The Multifunctioning DBA:

System Administration

Aug 18 2009   5:47PM GMT

Find how many files of any extension are in a folder



Posted by: Colin Smith
Powershell, One Liners, Powershell Tips, System Administration, Scripting

Recently I needed to find out if I had any files of a certain extension in a particular folder and if so how many. With Powershell this is no problem at all.

Say I was looking for a count of .ps1 files in the directory.

@(Dir Directorypath\*.ps1).Count

The @() makes the result go into an array object. This way no matter what you will get an accurate count without having to do a for loop on that directory.

Apr 30 2009   9:15PM GMT

Powershell Ping (Cont)



Posted by: Colin Smith
Database, Powershell, SQL Server, Monitoring, Ping, System Administration, Database Administration

Earlier this month I posted about SQL Ping Servers Script that I was working on to notify different groups if a server was down or just SQL Server was down or just the agent was down. Well here is the next part. This is the Ping_Interface Function of the script. This goes out and does a normal dos ping and looks at the return value. Sends only 1 packet and checks for 0% loss to be in the return value. Anything but that and it fails. If it fails it checks the ping_failure variable. If that is 0 then it knows to send the notification out and it puts the touch file on the filesystem so that on the next run it knows that it sent the notification out on the last run. Windows team did not want to be overtly notified that a server was down. Not my idea. So here is the function and if you have any questions head to http://sysadminsmith.com and click the ‘Submit a Question’ link.

##Ping_Interface

function
Ping_Interface

{

echo
“nt users are $ntusers”

$pingresult
=
ping
$machine
-n 1

if ($pingresult
-like
“*(0% loss*”)

{

## Server Interface is responsive

## Remove Ping Failure file from monitoring folder if ping is successful and file exists.

$pingfail
= 0

if (test-path
“i:\$folder\ping_Failure.txt”)

{

del
“i:\$folder\ping_Failure.txt”

}

echo
“Ping Interface is succseful for $machine” >> “i:\OUT\ping_with_service_check.txt”

Check_Services
## Check to see if the SQL Server and the SQLAgent are running via WMI

}

else

{

## Server Interface is non-responsive

$pingfail
= 1

if (!(test-path
“i:\$folder\ping_Failure.txt”))

{

New-Item
-force
-itemType
file
-path
“i:\$folder\ping_Failure.txt”

}

## Check if this is first time and if it has been more than 6 hours since notification was sent.

if (($pingfail
-eq 1) -and ($pingnoemail
-eq 0))

{

$users
=
get-content
-path
“i:\$folder\email”
|
Sort-Object
|
Get-Unique

$users
=
$users
+
$NTusers

$Subject
=
“Unable to Ping $machine”

$body
=
“$machine is not responding to Ping. $pingresult.”

echo
“Ping interface failed for $machine, Sending notification to Systems Team and DBA Team” >> “i:\OUT\ping_with_service_check.txt”

Notify

}

else

{

echo
“Ping interface failed for $machine, notification has already been sent.” >> “i:\OUT\ping_with_service_check.txt”

}


}

}


Mar 30 2009   9:57PM GMT

Scripting



Posted by: Colin Smith
Scripting, Powershell, Unix, Windows, System Administration, Windows Administration, Linux, Database Administration, Active Directory

So I have people ask me all the time how I can get so much done in a day. I have to be honest with you, I do not really do that much. This is because I write scripts to do anything and everything for me if I have to do it more than once I script it. If I think something might be useful I script it. I hate doing the same work over and over. Take something as simple as creating a new user on a domain. This is a very simple task in AD but it takes about 2 minutes per account. It is easy to find a script that will create mass accounts with some generic name. Why not take that same script and make it so it creates one account at a time or ten or whatever you need. You can make it so you type in the name of the user interactively or read names in from a list that you have. Simple things like that. Creating this account only takes 2 minutes in AD but I can do it in under 10 seconds.

When you work in the fast paced world like we do, especially in IT where everyone wants it now now now, every second counts. Saving just under 2 minutes does not seem like a lot but that is the just the beginning. If you save 5 minutes here and 10 there and 2 there then it adds up very fast so you can leave work an hour early, or you just make your boss think that you are that much better and more effective. It does not really matter if you are a Windows person or a Unix/Linux person. SCRIPT anything you can and save time.

I come from a Windows background and in this area the Unix/Linux admins are years ahead of the Windows users. I had done some automation using VBScript when I was a Windows Admin, but when I got into Unix for Database Admin, I quickly learned that scripting is the way. Now with Windows Powershell, Windows administrators can be much more effective in less time. Please learn a scripting language that is of use to you and that you understand. I prefer Powershell but VBscript is a good way to go if you like it better.

Good luck scripting and as always, if you have a question let me know by heading to http://sysadminsmith.com and click the ‘Submit a Question’ link on the right.