Tuesday, March 12, 2013
Line/Word Counting Files with Powershell
Line Counting a file
(gc ./myfile.txt | measure
-line).Count
Word Counting a file
(gc ./myfile.txt | measure
-word).Count
Character Counting a file
(gc ./myfile.txt | measure
-char).Count
White Character Count for a file
(gc ./myfile.txt | % { [char[]]$_ |
? { $_ -match ‘[\s]’ } } | measure).Count
Black Character Count for a file
(gc ./myfile.txt | % { [char[]]$_ |
? { $_ -match ‘[\S]’ } } | measure).Count
Line counting many files (recurse from curdir) and getting
the Sum
(ls -recurse *.hl7 | % { (gc $_ | measure –line).Lines } | measure –sum).Sum
Line counting many files (recurse from curdir) and getting
the Average
(ls -recurse *.hl7 | % { (gc $_ | measure –line).Lines } | measure
–average).Average
Count all lines containing “error” or “Error” in all log
files from curdir down and getting the Sum
(ls -recurse *.log | % { (gc $_ | ? {$_ -match ‘[Ee]rror’ } | measure
–line).Lines } | measure –average).Sum
Restarting a service on a remote machine
If you have Windows Servers that you need to do any admin on, you should already have set them up with powershell and enable remoting.
If you need to restart a service from your own box, do not be a monkey and RDP into the machine and go to control panel, etc.
Open powershell on your own machine and type this:
icm SERVERNAME { net stop 'Service Name'; net start 'Service Name'; }
If you find yourself doing this a lot, open up your profile script at "~/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1" (If it doesn't exist, make it) and define this as a function that you can easily remember:
function restart-jira() { icm SERVERNAME { net stop 'Atlassian JIRA'; net start 'Atlassian JIRA'; }
Restart your shell (or reload profile script) and from now on you can just type e.g. "restart-jira" and it will restart the remote service.
If you need to restart a service from your own box, do not be a monkey and RDP into the machine and go to control panel, etc.
Open powershell on your own machine and type this:
icm SERVERNAME { net stop 'Service Name'; net start 'Service Name'; }
If you find yourself doing this a lot, open up your profile script at "~/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1" (If it doesn't exist, make it) and define this as a function that you can easily remember:
function restart-jira() { icm SERVERNAME { net stop 'Atlassian JIRA'; net start 'Atlassian JIRA'; }
Restart your shell (or reload profile script) and from now on you can just type e.g. "restart-jira" and it will restart the remote service.
A Better Console For Windows
Using Windows, but still crafty enough to use the shell?
Go get yourself ConsoleEmu. Support for font changes, multi-shell support, tabs, and most importantly of all: window resizing/maximizing.
Make sure you set it up to use PowerShell as it will default to boring/shitty old cmd.
Tailing Log Files in Powershell
Want to watch a log file on a windows box and have the console window constantly updated as the file grows? Open up powershell and type:
gc c:\somefile.log -wait
Want to watch a log file and only show lines that contain the word "Error?"
gc c:\somefile.log -wait | ? { $_.Contains("Error") }
Want to watch a log file and have it send you an email any time an error message occurs in the log file?
gc trace.log -wait | ? { $_.Contains("Error") } | % { Send-MailMessage -to "myinbox@wherever.com" -from "mysmtpinbox@wherever.com" -bodyAsHTML ("An error occurred: <br/><br/> + $_ } -smtpserver "smtp.myserver.com" }
Want to do the same with an SMTP server that requires authentication?
$username = "mysmtpinbox@wherever.com"
$password = ConvertTo-SecureString "mysmtppassword" -AsPlainText - Force
$creds = New-Object System.Management.Automation.PSCredential($username, $password)
gc trace.log -wait | ? { $_.Contains("Error") } | % { Send-MailMessage -to "myinbox@wherever.com" -from "mysmtpinbox@wherever.com" -bodyAsHTML ("An error occurred: <br/><br/> + $_ } -smtpserver "smtp.myserver.com" -credential $creds }
gc c:\somefile.log -wait
Want to watch a log file and only show lines that contain the word "Error?"
gc c:\somefile.log -wait | ? { $_.Contains("Error") }
Want to watch a log file and have it send you an email any time an error message occurs in the log file?
gc trace.log -wait | ? { $_.Contains("Error") } | % { Send-MailMessage -to "myinbox@wherever.com" -from "mysmtpinbox@wherever.com" -bodyAsHTML ("An error occurred: <br/><br/> + $_ } -smtpserver "smtp.myserver.com" }
Want to do the same with an SMTP server that requires authentication?
$username = "mysmtpinbox@wherever.com"
$password = ConvertTo-SecureString "mysmtppassword" -AsPlainText - Force
$creds = New-Object System.Management.Automation.PSCredential($username, $password)
gc trace.log -wait | ? { $_.Contains("Error") } | % { Send-MailMessage -to "myinbox@wherever.com" -from "mysmtpinbox@wherever.com" -bodyAsHTML ("An error occurred: <br/><br/> + $_ } -smtpserver "smtp.myserver.com" -credential $creds }
Sharing Files Over Http
If you want to share out a directory over HTTP, and you have python installed on your machine, run this at the command line to do so:
python -m SimpleHTTPServer 8080
Anyone on your network can now navigate to http://YOURHOST_OR_IP:8080/. Obviously, if you want to share this out over the internet, you can setup port forwarding on your router and have them hit your public IP.
python -m SimpleHTTPServer 8080
Anyone on your network can now navigate to http://YOURHOST_OR_IP:8080/. Obviously, if you want to share this out over the internet, you can setup port forwarding on your router and have them hit your public IP.
Subscribe to:
Posts (Atom)