Archive for the ‘PowerShell’ category

Simple tip for PowerShell

February 25, 2009

Been doing more in PowerShell every week and while I’m far from being an expert I definitely enjoy what it offers. I’m always looking for tips and tricks to get the most out of PoSh and one came in my inbox today.

A short article by Jeffery Hicks pointed out something simple that makes perfect sense. Color coding the output to distinguish between your function info and the actual output.

Function Do-Something {
#run some code here
Write-host "Starting update process" -foregroundcolor Green
$procs=Get-wmiobject win32_process
Write $procs
}

When his above sample is run, the function message shows in green and the sorted and filtered output is sent to the pipeline.

Have to remember this for re-doing some of the early scripts I setup to see if I can clean them up.

Send Email using PowerShell from CSV data file

December 21, 2008

Just finished phase 1 of a project which required sending out an automated survey email to a dynamic list of people. The email message is the same in all of these except for an ID number embedded in a link within the message. Currently the list of emails along with the ID is automatically dumped out of the database nightly into a CSV file so I didn’t need to do anything there just yet.

Here’s the PowerShell script that I pieced together from various sites to get this to work

$EmailFileLocation = “d:\scripts\email\dailyemaillist.csv”
function SendSurvey($SendTo,$EmailID)
{
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = “mailserver.domain.com”
$FromAddress = new-object System.Net.Mail.MailAddress(“email@domain.com”, “Your Name”)
$mailmessage.sender = $FromAddress
$mailmessage.from = $FromAddress
$mailmessage.To.add($SendTo)
$mailmessage.Subject = “Your Message Subject Here”
$mailmessage.IsBodyHtml = 1

# The line below will pull the message body from the specified file
# This is currently commented out to have the message body created within this script but can be changed easily.
# $mailmessage.Body = Get-Content .\emailbody.htm
$mailmessage.Body = “<html><body>
<p>This is an example message.</p>
<p>If this had been an actual message there would be something worth reading here.</p>
<p>bla bla bla…here’s the link example:
$EmailID </p>
<p><br>End of message</p>
</body></html>”
$Credentials = new-object System.Net.networkCredential
$Credentials.UserName = email@domain.com
$Credentials.Password = “Password”
$SMTPClient.Credentials = $Credentials
$SMTPClient.Port = 25
$smtpclient.Send($mailmessage)
}

$Guests = import-csv $EmailFileLocation -erroraction stop
foreach ($guest in $guests) {
SendSurvey $guest.email $guest.number
}

Hope this helps. One other note, if you’re setting this script to run automatically, you will either need to sign the script or change the permissions in PowerShell to allow it to run. You can use the following command to make the change:

set-executionpolicy RemoteSigned

Last but not least, here’s a few of the sites I found info on to get this working:

PowerShell Notes

December 12, 2008

Been doing some projects in PowerShell lately and have found tons of great info available online to learn from. I was reading some old blogs and came across this link that talks about how to include help info in your PS functions:

This sample is from Media and MicroCode:

#.Synopsis
#      Turns a Hashtable into a PowerShell object
#.Description
#      Creates a new object from a hashtable.
#.Example
#      # Creates a new object with a property foo and the value bar
#      Get-HashtableAsObject @{"Foo"="Bar"}
#.Example
#      # Creates a new object with a property Random and a value
#      # that is generated each time the property is retreived
#      Get-HashtableAsObject @{"Random" = { Get-Random }}
#.Example
#      # Creates a new object from a hashtable with nested hashtables
#      Get-HashtableAsObject @{"Foo" = @{"Random" = {Get-Random}}}  

Another cool short PS script I read was doing a lookup on an IP address to find the location. This was posted on the Get-PowerShell blog and shows a really short script to find out the public IP address of the system being run and then do a lookup to find the georgraphical area it is located in.

Running PowerShell from your Mobile Phone

May 22, 2008

While I realistically don’t see myself doing this anytime soon I thought this was a really cool idea…Dmitry has a detailed write up on how to Execute PowerShell Scripts from your smartphone to have the ability to send a PowerShell script from your mobile phone and have it run on your servers back at the office.

I’m a little paranoid of someone sending something that would run against my systems, which is mentioned, but I could see some “somewhat” safe ideas. Perhaps setting up some monitoring or verification scripts that could run to check on the health of systems or report on the status, and have the info hard coded as to where to go. Could be nice to be able to run a quick diagnostic from your phone and have the results sent to you.

Remote system stats

April 20, 2008

At our last Palm Beach IT meeting we talked about PowerShell and some of the great things it allows you to accomplish. Here’s a site that actually shows what we talked about, monitoring systems remotely using PowerShell & MRTG. The WSMAN Instrumentation Examples page uses MRTG to graph the PowerShell stats but we had also talked about using PowerGadgets for a nicer looking dashboard.

Pause in PowerShell script

April 9, 2008

While putting together a demo for PowerShell the other day, I wanted to setup a script that would run each section of my demo without having to run a new script for each one (Lazy Admin in training!) Here’s a simple little function that replicates the old DOS based Pause command from the PowerShell Team Blog.

function Pause ($Message=”Press any key to continue…”)
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey(”NoEcho,IncludeKeyDown”)
Write-Host “”
}