Powershell

Powershell is awesome.

Here a collection of some snippets:

Check who is accessing a file on a server:
net file | where {$_ –match “$filename”}

More:
Get-Service | Sort-Object -Descending Status | ForEach{if($_.Status
-eq “Running”){Write-Host $_.Name -ForegroundColor green} else {Write-Host $_.Na
me -ForegroundColor red}}

Get-Service | ConvertTo-Html -Property Name, Status | ForEach {if($_
-like “*<td>Running</td>*”) {$_ -replace “<tr>”, “<tr bgcolor=green>”} else {$_
-replace “<tr>”, “<tr bgcolor=red>”}} > .\get-service.html

Get-PSDrive | Sort-Object Used -Descendind

New-PSDrive -name FK -psprovider FileSystem -root C:\Temp\Powershell\Files

Set-Location FK:
Get-ChildItem * -Exclude *.tmp, *.temp | Select-Object name, length | Sort-Object Length, Name -Descending

Get-Alias | Sort-Object

Get-Service | Group-Object Status
Get-Service | Get-Member
Get-Service | where{$_.status -eq “stopped”}

Get-ChildItem | Group-Object Extension | Sort-Object Count -Descending
Get-ChildItem | Measure-Object length -average -sum -maximum -minimum
Get-ChildItem | where-object{$_.length -gt 2MB}
Get-ChildItem | where {$_.PSIsContainer}

$a=Get-ChildItem | Select-Object Extension | Sort-Object Extension -unique
$a | ForEach {“.\Neu”+$_.Extension} | ForEach {NewItem $_ -type directory}

$a=Get-ChildItem | where {$_.PsIsContainer -eq -0}
$a | foreach{Copy-Item $_.FullName (“.\Neu” + $_.Extension + “\” + $_.Name)}

Get-ChildItem | foreach{$_.IsReadOnly = -1}

Get-ADUser -Filter *
Get-Command *user*

admin on May 16th 2012 in PowerShell

No Comments »

Upgrade Problem with nagios from lenny to Squeeze in debian

Because lenny is no longer supported I had to upgrade our nagios monitor server from lenny to squeeze. After the upgrade the check_snmp commands timed out. After some research I found out because of a licensing issues snmpd with the mibs are no longer installed by default.

1. Edit Source List
First I had to change the /etc/apt/source.list and add “contrib non-free”.

It should look similar to this:
deb http://ftp.ch.debian.org/debian/ squeeze main contrib non-free
deb-src http://ftp.ch.debian.org/debian/ squeeze main contrib non-free

2. Install SNMPD with the MIB List
#apt-get install snmpd
#apt-get install snmp-mibs-downloader

3. Download Mibs
#/usr/bin/download-mibs

4. Restart SNMPD
I’m not sure if this step is necessary. I actually even restarted the whole server.
#/etc/init.d/snmpd restart

admin on April 26th 2012 in IT, Linux

No Comments »

How to Shutdown Exchange 2007 Services

net stop msexchangeadtopology /y
net stop msftesql-exchange /y
net stop msexchangeis /y
net stop msexchangesa /y
net stop iisadmin /y

admin on April 2nd 2012 in Exchange Server, IT

No Comments »

Find old Computers/users in Active Directory

Easiest way is to use following query on your Domain Controller to find all computers which were inactive for 4 weeks:
dsquery computer –inactive 4

The same you could for users:
dsquery users – inactive 4

admin on March 22nd 2012 in Windows, Windows Server

No Comments »

Batch check if a Program or Service is running

tasklist /FI "IMAGENAME eq %PROGNAME%" 2>NUL | find /I /N "%PROGNAME%">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running
if "%ERRORLEVEL%"=="1" echo Programm is not running

%PROGNAME% needs to be replaced with your program name.

admin on February 18th 2012 in IT, Windows

No Comments »

sendEmail from Bash over GMail

The syntax is following:
sendEmail -v –f $EMAIL_FROM -s smtp.gmail.com:587 -xu p$USERNAME –xp $PASSWORD –t $EMAIL_TO -o tls=yes –u $SUBJECT –m $MESSAGE

But after I tried it I got following message:
ERROR => No TLS support!  SendEmail can’t load required libraries. (try installing Net::SSLeay and IO::Socket::SSL

This fixed the problem:
apt-get install libio-socket-ssl-perl libnet-ssleay-perl

admin on February 10th 2012 in Linux

No Comments »

Color LInux Prompt

vi ~/.bashrc

To color it red change PS1 to:
export PS1=’\[\e[1;31m\][\u@\h \W]\$\[\e[0m\] ‘

After that you need to save it and reload it with:
source ~/.bashrc

admin on February 8th 2012 in IT, Linux

No Comments »

Quick and Dirty MS Database Analysis

This method queries all tables in a database and it displays the top 10 rows.

Get all tables with the following query:
SELECT name FROM ssysobjects WHERE type = ‘u’;

Copy&Paste it into Excel A Column and put following Formula into B Column for each row:
="SELECT TOP 10 * FROM " & A1 &";"

Result will be something like this:
SELECT TOP 10 * FROM COMPANY; SELECT TOP 10 * FROM PEOPLE; SELECT TOP 10 * FROM BILLS;

Run this query in Microsoft Management Studio an analyse the result.

admin on February 4th 2012 in MSSQL

No Comments »

Treesize in Linux Command Console

I love the program treesize on windows and I was looking for a script to do the same on linux.

Thanks to Andrew who made this neat script:

admin on January 31st 2012 in Linux

No Comments »

Using nmap to check if multiple hosts are up

nmap -sP 192.168.1.0-255

admin on December 31st 2011 in IT, Linux

No Comments »




Check out new movies online website. Download and buy movies now.