{"id":50,"date":"2007-01-17T04:00:00","date_gmt":"2007-01-17T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/01\/17\/i-can-define-that-function-in-1-step\/"},"modified":"2007-01-17T04:00:00","modified_gmt":"2007-01-17T12:00:00","slug":"i-can-define-that-function-in-1-step","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/01\/17\/i-can-define-that-function-in-1-step\/","title":{"rendered":"I can define that function in 1 step"},"content":{"rendered":"<p>If you are like me, you&#8217;re starting to develop a lot of aliases and custom functions.  The thing is, I can&#8217;t always remember what every function or alias does.  Fortunately, that is pretty easy to discover.For example, I have an alias called du<\/p>\n<p><font face=\"'Courier New',Courier,monospace\" size=\"2\"><span style=\"font-family: Courier New,Courier,mono;\">CommandType     Name                            Definition<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&#8212;&#8212;&#8212;&#8211;     &#8212;-                            &#8212;&#8212;&#8212;-<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Alias&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; du                              Get-DiskUsage<\/span><\/p>\n<p><\/font>But what did I create for Get-DiskUsage?  Piping the Get-Alias command through Get-Member gives me this:<br \/><span style=\"font-family: Courier New,Courier,mono;\">PS S:\\ &gt; get-alias du|gm<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp; TypeName: System.Management.Automation.AliasInfo<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType&nbsp;&nbsp;&nbsp;&nbsp; Definition<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Equals&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Boolean Equals(Object obj)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">GetHashCode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Int32 GetHashCode()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">GetType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Type GetType()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">get_CommandType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Management.Automation.CommandTyp..<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">get_Definition&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.String get_Definition()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">get_Description&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.String get_Description()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">get_Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.String get_Name()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">get_Options&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Management.Automation.ScopedItem..<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">get_ReferencedCommand Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Management.Automation.CommandInf..<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">get_ResolvedCommand&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Management.Automation.CommandInf..<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">set_Description&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Void set_Description(String value)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">set_Options&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Void set_Options(ScopedItemOptio..<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">ToString&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.String ToString()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">CommandType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Management.Automation.CommandTyp..<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Definition&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.String Definition {get;}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Description&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.String Description {get;set;}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.String Name {get;}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Options&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Management.Automation.ScopedItem..<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">ReferencedCommand&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Management.Automation.CommandInf..<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">ResolvedCommand&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Management.Automation.CommandInf..<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">ResolvedCommandName&nbsp;&nbsp; ScriptProperty System.Object ResolvedCommandName {get=..<\/span><\/p>\n<p>&nbsp;Ahhhh. I think Resolvedcommand might get me what I need.  So I try this:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">PS S:\\ &gt; (get-alias du).ResolvedCommand<br \/>CommandType     Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                            Definition<br \/>&#8212;&#8212;&#8212;&#8211;     &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                            &#8212;&#8212;&#8212;-<br \/>Function&nbsp;&nbsp;&nbsp;        Get-DiskUsage                   param( [string]$computer = &#8220;&#8230;<\/span><\/p>\n<p>Yes.  I can see the definition but it is trunctated. Easily fixed with a little formatting. (code formatting in the blog leaves a little to be desired but you get the idea.)<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">PS S:\\ &gt; (get-alias du).ResolvedCommand |format-list<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Name        : Get-DiskUsage<br \/>CommandType : Function<br \/>Definition  : param( [string]$computer = &#8220;.&#8221; , [switch]$all) $size = @{ l = &#8220;Size (MB)&#8221;; e = { $_.size\/1mb};      f = &#8220;{0:N}&#8221;}<br \/>&nbsp;$free = @{ l = &#8220;free (MB)&#8221;; e = { $_.freespace\/1mb}; f = &#8220;{0:N}&#8221;}$perc = @{ l = &#8220;percent&#8221;; e = { 100.0 * ([double]$_.freespace\/[double]$_.size)}; f=&#8221;{0:f}&#8221; }$name = @{ e = &#8220;name&#8221;; f = &#8220;{0,-20}&#8221; }<br \/>$fields = $name,$size,$free,$perc<\/p>\n<p># in case the user wants to see more than just local drives$filter = &#8220;DriveType = &#8216;3&#8217;&#8221;if ( $all ) { $filter = &#8220;&#8221; }<\/p>\n<p># go do the work by getting the information from the appropriate<br \/># computer, and send it to format-table with the appropriate# fields and formatting infoget-wmiobject -class win32_logicaldisk -filter $filter -comp $computer |format-table $fields -auto<\/span><\/p>\n<p>If I only want to see the definition I can use an expression like this:<br \/><span style=\"font-family: Courier New,Courier,mono;\">(get-alias du).ResolvedCommand.Definition<\/span><\/p>\n<p>Now I can see the full function definition. Now that I know the shortcut, I can try it out on another alias:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">PS S:\\ &gt; (get-alias fs).resolvedcommand.definitionparam([string]$Computer) $results=Get-WmiObject -class win32_logicaldisk -computername $Computer| `where {$_.drivetype -eq 3} | select DeviceID,VolumeName,Size,FreeSpace<\/p>\n<p>foreach ($drive in $results) {$obj = New-Object System.Object$sz=&#8221;{0:N2}&#8221; -f (($drive.Size)\/1MB)$fs=&#8221;{0:N2}&#8221; -f (($drive.Freespace)\/1MB)<\/p>\n<p>Add-Member -inputobject $obj -membertype NoteProperty -Name DeviceID -value$drive.DeviceID<br \/>Add-Member -inputobject $obj -membertype NoteProperty -Name VolumeName -value $drive.VolumeName<br \/>Add-Member -inputobject $obj -membertype NoteProperty -Name SizeMB -value $sz<br \/>Add-Member -inputobject $obj -membertype NoteProperty -Name FreespaceMB -value $fsWrite-Output $obj}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br \/>Terrific. Now with a single line I can get complete alias definition.<\/p>\n<p><a href=\"http:\/\/www.technorati.com\/tags\/powershell\" rel=\"tag\">PowerShell<\/a> <a href=\"http:\/\/www.technorati.com\/tags\/scripting\" rel=\"tag\">Scripting<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are like me, you&#8217;re starting to develop a lot of aliases and custom functions. The thing is, I can&#8217;t always remember what every function or alias does. Fortunately, that is pretty easy to discover.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,25],"tags":[],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}