{"id":71433,"date":"2004-09-15T18:28:00","date_gmt":"2004-09-15T18:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/09\/15\/how-can-i-use-both-command-line-arguments-and-a-default-value\/"},"modified":"2004-09-15T18:28:00","modified_gmt":"2004-09-15T18:28:00","slug":"how-can-i-use-both-command-line-arguments-and-a-default-value","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-both-command-line-arguments-and-a-default-value\/","title":{"rendered":"How Can I Use Both Command-line Arguments and a Default Value?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><\/p>\n<p>Hey, Scripting Guy! I\u2019d like to have a script that accepts computer names as command-line arguments and then runs against each of those computers. However, if you don\u2019t enter any command-line arguments, I\u2019d like it to default to running against the local computer. Can you help me out here?<\/p>\n<p>&#8212; TS<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/a><\/p>\n<p>Hey, TS. Sure, we can help you out here. Let\u2019s say you started your hypothetical script by typing the following command at the command prompt:<\/p>\n<pre class=\"codeSample\">cscript my_script.vbs atl-ws-01 atl-ws-02 atl-ws-03\n<\/pre>\n<p>In that case, you\u2019d like the script to run against the three specified computers: atl-ws-01; atl-ws-02; and atl-ws-03. But what if you typed in a command with no command-line arguments, like this:<\/p>\n<pre class=\"codeSample\">cscript my_script.vbs\n<\/pre>\n<p>In that case, you\u2019d like the script to say, \u201cOh, no command-line arguments, huh? Ok, then I\u2019ll just run against the local computer instead.\u201d<\/p>\n<p>So how do you do that? Well, you do that by using code similar to this:<\/p>\n<pre class=\"codeSample\">If Wscript.Arguments.Count = 0 Then\n    arrComputers = Array(\".\")\nElse\n    Dim arrComputers()\n    For i = 0 to Wscript.Arguments.Count - 1\n        Redim Preserve arrComputers(i)\n        arrComputers(i) = Wscript.Arguments(i)\n    Next\nEnd If\nFor Each strComputer in arrComputers\n    Set objWMIService = GetObject _\n        (\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\n    Set colItems = objWMIService.ExecQuery _\n        (\"Select * from Win32_OperatingSystem\")\n    For Each objItem in colItems\n        Wscript.Echo objItem.Caption\n    Next\nNext\n<\/pre>\n<p>Let\u2019s take a moment to explain how this works. We start by seeing whether or not any command-line arguments were entered; we do that by checking the value of Wscript.Arguments.Count (as you might expect, this tells us the number of arguments that were supplied when the script started). Suppose Count is equal to 0. Well, that means you didn\u2019t supply <i>any<\/i> command-line arguments. Therefore, we set the value of the array arrComputers to a dot (\u201c.\u201d). Why a dot? Well, in the wild and wonderful world of WMI, the dot represents the local computer. <\/p>\n<p>And why an array? Well, we want to write as few lines of code as possible. By using an array, we don\u2019t have to write one set of code that gets used when we don\u2019t specify a command-line argument and a separate set of code that gets used when we <i>do<\/i> specify a command-line argument (or two). Instead, we use a simple If-Then statement to check the value of Wscript.Arguments.Count. As we noted, if the value is 0 (that is, if no command-line arguments were entered), we set the value of arrComputers to a single dot. We thus have an array consisting of a single item.<\/p>\n<p>But what if Count does <i>not<\/i> equal 0? In that case, we want to take all the command-line arguments and stuff each of those into arrComputers. Here\u2019s how we do that:<\/p>\n<p>First, we create a dynamic array using this code: <b>Dim arrComputers()<\/b>. A dynamic array is an array in which we don\u2019t specify the size in advance; instead, we dynamically grow the array as needed. We do that so that we don\u2019t have to worry about how many command-line arguments were entered; we\u2019ll just resize the array as needed to accommodate all of those arguments. <\/p>\n<p>Next, we create a For Next loop that runs from 0 to Wscript.Arguments.Count &#8211; 1. This seems crazy, but we do this because the first item in a VBScript array is actually item 0; the last item, therefore, is the number of items minus 1. For example; suppose we have three items. The first item is item 0; the second is item 1; and the third is item 2 (3 &#8211; 1) <\/p>\n<p>For each item, we resize the array using this code: <b>Redim Preserve arrComputers(i)<\/b>. This sets the size of the array to the value of i (i is the loop variable, so it tells us which arguments in Wscript.Arguments we are working with). The first time through the loop, i is equal to 0, so we are &#8211; in effect &#8211; using this code: <b>Redim Preserve arrComputers(0)<\/b>, which gives us an array with one element. <\/p>\n<p>Incidentally, the Preserve part of this code ensures that we don\u2019t lose all the existing each time we re-dimension the array. If we just said <b>Redim arrComputers()<\/b>, the array would get resized, but any data already in the array would be deleted.<\/p>\n<p>We then add the argument in question to the array. If we supply atl-ws-01 and atl-ws-02 as command-line arguments, then at the end of this loop arrComputers will be an array with two elements: atl-ws-01 and atl-ws-02.<\/p>\n<p>The rest is easy. We create a For-Each loop that loops through all the elements in the array arrComputers; as we know, arrComputers contains either a dot (representing the local machine) or a list of computer names pulled from the command-line arguments. Each time the loop runs in connects to the appropriate computer and retrieves the Caption property from the Win32_OperatingSystem class. And that\u2019s all there is to it!<\/p>\n<p><br><\/p>\n<div>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<tbody>\n<tr>\n<td class=\"\"><a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0915.mspx#top\"><img decoding=\"async\" height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/a><a class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0915.mspx#top\">Top of page<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019d like to have a script that accepts computer names as command-line arguments and then runs against each of those computers. However, if you don\u2019t enter any command-line arguments, I\u2019d like it to default to running against the local computer. Can you help me out here? &#8212; TS Hey, TS. Sure, we [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[22,3,4,5],"class_list":["post-71433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-retrieving-input","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I\u2019d like to have a script that accepts computer names as command-line arguments and then runs against each of those computers. However, if you don\u2019t enter any command-line arguments, I\u2019d like it to default to running against the local computer. Can you help me out here? &#8212; TS Hey, TS. Sure, we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=71433"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71433\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=71433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}