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}

Loading ...
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

Loading ...
admin on April 26th 2012 in IT, Linux
net stop msexchangeadtopology /y
net stop msftesql-exchange /y
net stop msexchangeis /y
net stop msexchangesa /y
net stop iisadmin /y

Loading ...
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

Loading ...
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.

Loading ...
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

Loading ...
admin on February 10th 2012 in Linux
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

Loading ...
admin on February 8th 2012 in IT, Linux
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.
 Loading ...
admin on February 4th 2012 in MSSQL
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:
-
#/bin/sh
-
du -k –max-depth=1 | sort -nr | awk '
-
BEGIN {
-
split("KB,MB,GB,TB", Units, ",");
-
}
-
{
-
u = 1;
-
while ($1 >= 1024) {
-
$1 = $1 / 1024;
-
u += 1
-
}
-
$1 = sprintf("%.1f %s", $1, Units[u]);
-
print $0;
-
}
-
'
Visit his page on:
Andrew's BLog
 Loading ...
admin on January 31st 2012 in Linux
 Loading ...
admin on December 31st 2011 in IT, Linux
|