Powershell

My latest hobby has been learning Powershell.

Powershell is the long-awaited Windows equivalent of the Unix shell.  It doesn’t have things like grep, awk, sed, ps, that would be way too sensible. It can however do everything a UNIX shell can and a whole lot more, as it plugs into all of Microsoft’s application APIs.

However, like the UNIX shell it comes with a steep learning curve, but once climbed, it’s an inseparable tool, leaving Cygwin struggling in the dust.

Some example queries:

Show me all xml files containing “con” in the title, and “connect” in the contents:

gci -include *con*.xml -recurse |select-string “connect”

Or even better: Show me all .xml and .ini files modified in the last 15 days, larger than 5 MB which contain “connect” or “open” in the contents

gci -recurse -include *.xml, *.ini | Where-object {$_.Length -gt 5000000} | Where-Object {$_.LastWriteTime -gt (get-date).AddDays(-15)}| select-String “connect|open”

Mark all .ini files as read-only:

get-childitem -force -recurse -include *.ini -erroraction continue $directoryLocation | foreach { $_.Set_IsReadOnly($true)}

Find all .xml files and replace all occurrences of Value=”.*” with Value=”YYY”

gci -include *.xml -recurse | foreach {gc $_ | foreach { [regex]::Replace($_, “(VALUE=\s*`”)(.*?)(`”.*$)”,’$1YYYY$3′) } set-content $_;}

List all duplicated files in a given directory:

$dupList=get-childitem -force -recurse -filter $fileType -erroraction continue $directory | Group-Object -Property name | ? { $_.count -gt 1 }

Start a service on a remote computer:

Get-WmiObject -computer akl-tfs01 Win32_Service -Filter “Name=’MichellesService'” | Start-Service

Restart a remote computer:

(gwmi win32_operatingsystem -ComputerName MyServer -cred(get-credential)).Win32Shutdown(6)

Connect to a database and execute a query:

$SqlConnString = “Server = $SqlServer; Database = $Database; user = $SqlUser; password = $SqlPass”

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection

$SqlConnection.ConnectionString = $SqlConnString

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand

$SqlCmd.CommandText = “Select * from mytable”

$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter

$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet

$a = $SqlAdapter.Fill($DataSet)

$SqlConnection.Close()

$DataSet.Tables | Select-Object -ExpandProperty Rows

Send an email (with attachment):

$SmtpClient = new-object system.net.mail.smtpClient

$SmtpClient.host = “localhost”

$msg = new-object System.Net.Mail.MailMessage $from, $to, $Title, $Body

$msg.IsBodyHTML=$true

$attachment = new-object System.Net.Mail.Attachment $file

$msg.Attachments.Add($attachment)

$SmtpClient.Send($msg)

$attachment.Dispose()

Powershell is available for download here.

(Alas learning Powershell comes at the cost of bash atrophy, so unable to provide the Linux equivalents.)

This entry was posted in IT. Bookmark the permalink.

5 Responses to Powershell

  1. david says:

    You’d think I’d have something better to do on a Friday night… unfortunately not.

    Show me all xml files containing “con” in the title, and “connect” in the contents:

    locate ‘*con*.xml’ | xargs -d\\n grep -n1 -l connect

    or,

    find / -name ‘*con*.xml’ -exec grep -l connect “{}”

    Show me all .xml and .ini files modified in the last 15 days, larger than 5 MB which contain “connect” or “open” in the contents

    find / \( -name ‘*.xml’ -o -name ‘*.ini’ \) -mtime -15 -size +5M -exec grep -l connect “{}” \;

    Mark all .ini files as read-only: (Read only, from Ms chmod 777?)

    find / -name ‘*.ini’ -exec chmod a-r “{}” \;

    Find all .xml files and replace all occurrences of Value=”.*” with Value=”YYY”

    find / -name ‘*.xml’ -exec perl -p -i -e ‘s/Value=”[^”]*”/Value=”YYY”/’ “{}” \;

    List all duplicated files under a given directory:

    find $directory -exec basename “{}” \; | sort | uniq -d

    Start a service on a remote computer:

    ssh michelle@akl-tfs01 service start MichellesService

    Restart a remote computer:

    ssh michelle@akl-tfs01 reboot

    Connect to a database and execute a query:

    echo “Select * from mytable” |
    mysql -h $SqlServer -u $SqlUser -p $SqlPass $Database

    Send an email (with attachment):

    echo $Body |
    mail -r $from -s $Title -a $file $to

  2. david says:

    Show me all .xml and .ini files modified in the last 15 days, larger than 5 MB which contain “connect” or “open” in the contents

    find / \( -name ‘*.xml’ -o -name ‘*.ini’ \) -mtime -15 -size +5M -exec egrep -l “connect|open” “{}” \;

  3. david says:

    I should have proof read my answers

    Mark all .ini files as read-only:

    find / -name ‘*.ini’ -exec chmod 440 “{}” \;

    or, ug=r,o=

  4. michelle says:

    Oh cool. I’ve always wanted to know how to use find : – )

  5. david says:

    I’d like to know fully how to use ps, on Solaris and Linux.

Leave a Reply

Your email address will not be published.