{"id":18,"date":"2006-12-18T09:00:00","date_gmt":"2006-12-18T17:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/12\/18\/make-an-old-friend-do-your-work\/"},"modified":"2006-12-18T09:00:00","modified_gmt":"2006-12-18T17:00:00","slug":"make-an-old-friend-do-your-work","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/12\/18\/make-an-old-friend-do-your-work\/","title":{"rendered":"Make an Old Friend Do Your Work"},"content":{"rendered":"<p>I&#8217;ve been revisiting an old friend from the days of DOS and ancient versions of Windows. I had forgotten how much work this friend did for me. The more I thought about it, and the more I caught up with this friend, the more I realized there is a entirely new generation of system administrators that have never met him.<\/p>\n<p>My old friend is DosKey. DosKey was one of the earliest automation tools and I think it&#8217;s still got some legs. Now before you jump to any age-ist conclusions, you need to see what he can still do.&nbsp; If you are like me, you spend a lot of time at a command prompt.&nbsp; I really hate typing and am always in a hurry. I also dislike having to reach for a mouse and navigating my increasingly complex Start Menu. With DosKey, if I want to start Microsoft Excel, all I need to do is type xl. It doesn&#8217;t matter what directory I&#8217;m in. Nor does the application I&#8217;m starting have to be in the path.&nbsp; All I need to do is to define a doskey macro at the command line by typing:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">doskey xl=&#8221;%programfiles%\\Microsoft Office\\Office11\\Excel.exe&#8221; $1 $2 $3 $4 $5<\/span><\/p>\n<p>You can use variables like $1 and $2 (all the way up to $9) much like you would use %1 and %2 in a batch file. In this situation, from a command prompt I can type xl file1.xls file2.xls file3.xls and have Excel open all three files. Sure, with file association if I type file1.xls, Excel will open.&nbsp; My macro lets me open multiple files with a single,easy command. And what if I want to open a file with a different application?<\/p>\n<p>CSV files (at least for me) are associate with Excel.&nbsp; If I type testusers.csv at a command prompt, Excel will launch the file. But what if I want Notepad? Well I can save a few keystrokes (they do add up) with a macro like this:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">doskey np=Notepad.exe $1 $2 $3 $4 $5<\/span><\/p>\n<p>With this macro loaded, I can run <span style=\"font-family: Courier New,Courier,mono;\">np testusers.csv<\/span> at a command prompt and the file will open in Notepad.<\/p>\n<p>To see what macros are loaded into memory type<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">doskey \/macros<\/span><\/p>\n<p>I can create a doskey macro to execute any command or series of commands.&nbsp; Suppose I always use \/all when I run ipconfig. That&#8217;s too much typing.&nbsp; I&#8217;d rather have a macro called ip that does the work for me.<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">doskey <\/span><span style=\"font-family: Courier New,Courier,mono;\">ip=ipconfig \/all<\/span><\/p>\n<p>Now at a command prompt I can simply type <span style=\"font-family: Courier New,Courier,mono;\">ip<\/span>. <\/p>\n<p>I did a MCPMag.com TipSheet <a href=\"http:\/\/www.mcpmag.com\/columns\/article.asp?EditorialsID=1535\">column <\/a>on some of my favorite one-line expressions. They can be defined as DosKey macros. I also took some one-liners from a helpful reader&#8217;s <a href=\"http:\/\/jdhitsolutions.blogspot.com\/2006\/11\/more-one-liners.html\">followup <\/a>and added macros like these:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">doskey <\/span><span style=\"font-family: Courier New,Courier,mono;\">mac=nbtstat -A $1 |find \/i &#8220;MAC Address&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">doskey <\/span><span style=\"font-family: Courier New,Courier,mono;\"> disabled=dsquery user domainroot -disabled -limit 0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">doskey <\/span><span style=\"font-family: Courier New,Courier,mono;\">stale=dsquery computer domainroot -stalepwd 180 -limit 0<\/span><\/p>\n<p>I can call batch files with a Doskey macro. I created a <a href=\"http:\/\/sapien.eponym.com\/blog\/Scripting\/BatchCmd\/_archives\/2006\/12\/6\/2548503.html\">short batch file<\/a> to launch PowerShell directly from the command line. I can call that batch file with a macro.<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">doskey posh=s:\\posh.bat<\/span><\/p>\n<p>Or I can call a PowerShell command directly via a macro. Here are several:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">doskey <\/span><span style=\"font-family: Courier New,Courier,mono;\">ps=powershell -nologo -command &#8220;&amp; {get-process}&#8221;<br \/><\/span><span style=\"font-family: Courier New,Courier,mono;\">doskey <\/span><span style=\"font-family: Courier New,Courier,mono;\">svc=powerShell -nologo -command &#8220;&amp;{get-service|where {$_.status -eq &#8216;Running&#8217;}}&#8221;<br \/>doskey showproc=powershell -nologo -command &#8220;&amp; {get-process -name $1}&#8221;<br \/><\/span><br \/>By now, I hope you are beginning to see the possibilities.<\/p>\n<p>One downside to Doskey macros is that they only exist for the life of your command window. And they have to be loaded everytime. Fortunately, you can save all your doskey macros to a text file and load that file into Doskey.&nbsp; I&#8217;ve attached a copy of my doskey macros. To load them into memory run:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">doskey \/Macrofile=<span style=\"font-style: italic;\">savedtopath<\/span>\\MyMacros.txt<\/span><\/p>\n<p>If you want the macros to be available immediately and automatically, you&#8217;ll need to modify the&nbsp; shortcut that starts CMD.Exe. In the Target field you&#8217;ll see something like<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">C:\\WINDOWS\\system32\\cmd.exe \/K<\/span><\/p>\n<p>Simply add <\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">doskey \/macrofile=<span style=\"font-style: italic;\">path<\/span>\\mymacros.txt<\/span><\/p>\n<p>to the end of the command. This will load the macro file every time you start CMD from this shortcut.<\/p>\n<p>With a little help from an old friend, you can accomplish a lot of work with very little effort. Your biggest challenge will be trying to remember all the macros you&#8217;ve created (so you aren&#8217;t constantly running <span style=\"font-family: Courier New,Courier,mono;\">doskey \/macros<\/span>), which if you are my age, gets a little more challenging all the time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been revisiting an old friend from the days of DOS and ancient versions of Windows. I had forgotten how much work this friend did for me. The more I thought about it, and the more I caught up with this friend, the more I realized there is a entirely new generation of system administrators that have never met him.<\/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":[18,2,25],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-batchcmd","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/18","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=18"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}