Flylib.com

Books Software

 
 
 

Section 7.10. Find Out Which Command Is Being Run


7.10. Find Out Which Command Is Being Run

Use a function to list all possible matches for an executable command in the order in which they are searched:

function which {
        param($search)

        # look for the search term in the possible invocation list
        get-command *  where-object { $_.Name -like $search }

        # look for the search term with possible extensions
        foreach ($ext in $($env:PATHEXT).Split(";"))
        {
            get-command *  where-object { $_.Name -like "$search$ext" }
        }
    }



7.11. Downloading Content from the Web

The System.Net.WebClient class offers a DownloadString method that can be used to pull content directly from a URL. Because these examples are pulled from dynamic content on the Internet, the exact values in the output may change over time:

MSH D:\MshScripts>

$wc = new-object System.Net.WebClient

MSH D:\MshScripts>

$content = $wc.DownloadString("http://www.oreilly.com")

MSH D:\MshScripts>

$content  measure-object -words -lines -characters

Lines               Words               Characters          Property
    -----               -----               ----------          --------
    1108                4554                54957

WebClient is not particular about the type of content it downloads. If a URL points to XML content, it is perfectly legitimate to cast the string to an XmlDocument and work with it from there:

MSH D:\MshScripts>

$xml = [Xml]$wc.DownloadString
    ("http://www.oreillynet.com/rss/render/160.rss")

MSH D:\MshScripts>

$xml.GetElementsByTagName("item")  format-table title

title
    -----
    Exchange Server Cookbook
    ASP.NET 2.0: A Developer's Notebook
    Knoppix Pocket Reference
    Firefox Secrets: A Need-to-Know Guide
    Mapping Hacks
    Mac OS X Tiger Pocket Guide
    Learning Unix for Mac OS X Tiger
    Assembling Panoramic Photos: A Designer's Notebook
    DHTML Utopia: Modern Web Design Using JavaScript and DOM
    Toad Pocket Reference for Oracle, 2nd Edition
    Mac OS X Tiger for Unix Geeks



7.12. Shorthand for Frequently Used Data

To generate a Windows-style domain and username combination:

MSH D:\MshScripts>

function whoami { "$env:USERDOMAIN$env:USERNAME" }


For quick access to the current time and other convenient instants:

MSH D:\MshScripts>

alias now get-date

MSH D:\MshScripts>

function yesterday { (get-date).AddDays(-1) }

MSH D:\MshScripts>

function tomorrow { (get-date).AddDays(1) }

MSH D:\MshScripts>

function lastweek { (get-date).AddWeeks(-1) }


For a quick report of the operating system and version from WMI:

MSH D:\MshScripts>

function ver { $os = get-wmiobject
    Win32_OperatingSystem; "$($os.Caption)'n$($os.CSDVersion)" }




7.13. Returning System Uptime

First, start with a function for converting WMI date formats into DateTime objects:

function parsewmidate {
        param($wmidate)

        $year = [int]$wmidate.substring(0, 4)
        $month = [int]$wmidate.substring(4,2)
        $day = [int]$wmidate.substring(6,2)
        $hour = [int]$wmidate.substring(8,2)
        $min = [int]$wmidate.substring(10,2)
        $sec = [int]$wmidate.substring(12,2)

        new-object DateTime $year,$month,$day,$hour,$min,$sec
    }

Use the Win32_OperatingSystem WMI class to calculate uptime and time since last rebuild:

function uptime {
        $reboot = parsewmidate $(get-wmiobject Win32_OperatingSystem).LastBootupTime
        new-timespan $reboot $(get-date)
    }

    function sinceRebuild {
    {
        $rebuild = parsewmidate $(get-wmiobject Win32_OperatingSystem).InstallDate
        new-timespan $rebuild $(get-date)
    }