Archive

Archive for the ‘one-liner’ Category

Powershell One-liners – Folder cleaning

Some application just log to much, and the log files tend to get huge.

Or maybe you have a situation where you just don’t need the log’s from the last 2 years, but would like to retain a few days back, in case of error:

Windows Scheduled task and Powershell to the rescue

 

powershell.exe -command "& {get-childitem -Path e:\IIS_Logs -Recurse| %{if ($_.CreationTime -lt ([DateTime]::Now.AddDays(-5))){remove-item $_.FullName}}}"

This will delete files older than 5 days in the targeted folder (IIS_logs) and all subfolders

I have tried to wrap this in a schtasks command, so far been unsuccessful.

In the Windows Server 2008 R2 GUI:

  • Task Scheduler
  • Create a basic task
  • Give it a name and a description
  • Choose the Task trigger and the properties for the selected trigger
  • Choose the action (start a program)
  • Add the path for powershell.exe in the program/script box and everything following to the Add arguments (optional) box
  • image
  • Review and Finish.
    /theadminguy

Powershell One-liners – IP scanner

So why use powershell? I even heard someone say “this powershell is so over-rated, what does it offer, that cannot be done with cmd.exe and a another tool?”

Well, to me, that is exactly it, what does powershell offer, which cannot be done with cmd.exe?

What if you wanted just to check a defined range of IP’s in you managed subnet, to find which ones have live hosts on them?

[PS]# 1..100 | %{ping -n 1 -w 15 11.2.7.$_ | select-string "reply from"}

Reply from 11.2.7.83: bytes=32 time=5ms TTL=122
Reply from 11.2.7.84: bytes=32 time=7ms TTL=122
Reply from 11.2.7.85: bytes=32 time=6ms TTL=122
Reply from 11.2.7.86: bytes=32 time=6ms TTL=122
Reply from 11.2.7.87: bytes=32 time=6ms TTL=122
Reply from 11.2.7.89: bytes=32 time=6ms TTL=122
Reply from 11.2.7.91: bytes=32 time=6ms TTL=122
Reply from 11.2.7.95: bytes=32 time=6ms TTL=122
Reply from 11.2.7.99: bytes=32 time=6ms TTL=122

This one-liner ping’s the range from 1 to 100 and returns the machines which replied.

The point here is not, that only Powershell can do this on Windows, and I know that ping.exe is not a native powershell cmd-let and what if the host does not respond to ICMP…bla bla.. Smile

But this method is simple, fast and intuitive. No need for starting up a dedicated application, no fiddling with scripts, just get the job done, which in turn leaves more room for other stuff.

/theadminguy

%d bloggers like this: