Open Source Cloud Authors: Zakia Bouachraoui, Liz McMillan, Elizabeth White, Pat Romanski, Yeshim Deniz

Blog Feed Post

Unix To PowerShell - Which

PowerShell_unix PowerShell is definitely gaining momentum in the windows scripting world but I still hear folks wanting to rely on unix based tools to get their job done.  In this series of posts I’m going to look at converting some of the more popular Unix based tools to PowerShell.


For this post, I’m going to tackle the Unix “which” command.  With the which command, you specify a file specification (ie notepad.exe, cs.*, etc) and it will scan your defined lookup paths in your environment for a file matching that specification.  Years and years ago I wrote a windows command line version of this that I still use almost daily.  So, it’s fitting that this is the first app I’m tackling in this series.

The logic behind this app is fairly simple: iterate through the values in your PATH.  For each directory in there, look for matches, and then print them out.  The following code performs the iteration logic in the Do-Which function and then writes the output of each match with the Write-FileInfo function.

   1: #----------------------------------------------------------------
   2: # Which.ps1
   3: #----------------------------------------------------------------
   4: param(
   5:   [string]$filespec = $null
   6: );
   8: function Write-FileInfo()
   9: {
  10:   param([System.IO.FileInfo]$fi);
  11:   if ( $null -ne $fi )
  12:   {
  13:     $t = $fi.LastWriteTime;
  14:     $ampm = "a";
  15:     $hour = $t.Hour;
  16:     if ( $hour -gt 12 ) { $hour -= 12; $ampm = "p"; }
  17:     if ( $hour -eq 0 ) { $hour = 12; }
  19:     $fmt = "{0,10:00}/{1:00}/{2:00} {3,3}:{4:00}:{5:00}{6} {7,12:0,0} {8}" -f 
  20:       $t.Month, $t.Day, $t.Year, $hour, $t.Minute, $t.Second, $ampm, $fi.Length, $fi.FullName;
  21:     Write-Host $fmt;
  22:   }
  23: }
  25: function Do-Which()
  26: {
  27:   param([string]$filespec = $null);
  28:   if ( ($null -ne $filespec) -and ("" -ne $filespec) ) 
  29:   {
  30:     foreach ($dir in ($env:path).Split(';'))
  31:     {
  32:       [System.IO.FileInfo[]]$cis = Get-ChildItem ${dir}\* -include $filespec;
  33:       foreach ($file in $cis)
  34:       {
  35:         Write-FileInfo $file;
  36:       }
  37:     }
  38:   }
  39:   else
  40:   {
  41:     Write-Host "Usage: which.ps1 filespec"
  42:   }
  43: }
  45: Do-Which $filespec;


Read the original blog entry...

More Stories By Joe Pruitt

Joe Pruitt is a Principal Strategic Architect at F5 Networks working with Network and Software Architects to allow them to build network intelligence into their applications.

IoT & Smart Cities Stories
While the focus and objectives of IoT initiatives are many and diverse, they all share a few common attributes, and one of those is the network. Commonly, that network includes the Internet, over which there isn't any real control for performance and availability. Or is there? The current state of the art for Big Data analytics, as applied to network telemetry, offers new opportunities for improving and assuring operational integrity. In his session at @ThingsExpo, Jim Frey, Vice President of S...
In his keynote at 18th Cloud Expo, Andrew Keys, Co-Founder of ConsenSys Enterprise, provided an overview of the evolution of the Internet and the Database and the future of their combination – the Blockchain. Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settl...
@CloudEXPO and @ExpoDX, two of the most influential technology events in the world, have hosted hundreds of sponsors and exhibitors since our launch 10 years ago. @CloudEXPO and @ExpoDX New York and Silicon Valley provide a full year of face-to-face marketing opportunities for your company. Each sponsorship and exhibit package comes with pre and post-show marketing programs. By sponsoring and exhibiting in New York and Silicon Valley, you reach a full complement of decision makers and buyers in ...
Two weeks ago (November 3-5), I attended the Cloud Expo Silicon Valley as a speaker, where I presented on the security and privacy due diligence requirements for cloud solutions. Cloud security is a topical issue for every CIO, CISO, and technology buyer. Decision-makers are always looking for insights on how to mitigate the security risks of implementing and using cloud solutions. Based on the presentation topics covered at the conference, as well as the general discussions heard between sessio...
The Internet of Things is clearly many things: data collection and analytics, wearables, Smart Grids and Smart Cities, the Industrial Internet, and more. Cool platforms like Arduino, Raspberry Pi, Intel's Galileo and Edison, and a diverse world of sensors are making the IoT a great toy box for developers in all these areas. In this Power Panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, panelists discussed what things are the most important, which will have the most profound e...
The Jevons Paradox suggests that when technological advances increase efficiency of a resource, it results in an overall increase in consumption. Writing on the increased use of coal as a result of technological improvements, 19th-century economist William Stanley Jevons found that these improvements led to the development of new ways to utilize coal. In his session at 19th Cloud Expo, Mark Thiele, Chief Strategy Officer for Apcera, compared the Jevons Paradox to modern-day enterprise IT, examin...
Rodrigo Coutinho is part of OutSystems' founders' team and currently the Head of Product Design. He provides a cross-functional role where he supports Product Management in defining the positioning and direction of the Agile Platform, while at the same time promoting model-based development and new techniques to deliver applications in the cloud.
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
LogRocket helps product teams develop better experiences for users by recording videos of user sessions with logs and network data. It identifies UX problems and reveals the root cause of every bug. LogRocket presents impactful errors on a website, and how to reproduce it. With LogRocket, users can replay problems.
Data Theorem is a leading provider of modern application security. Its core mission is to analyze and secure any modern application anytime, anywhere. The Data Theorem Analyzer Engine continuously scans APIs and mobile applications in search of security flaws and data privacy gaps. Data Theorem products help organizations build safer applications that maximize data security and brand protection. The company has detected more than 300 million application eavesdropping incidents and currently secu...