Here's a handy script template that can be used for processing a list of computer hostnames. The template does not actually do anything with the hostnames but is a good starting point from which additional functionality can be implemented. Also, the "hostnames" could actually be any list of items to be processed.
Template Features:
- Uses -? or -H to invoke usage help
- Invalid argument triggers usage help and aborts
- Documentation and built-in Help are one in the same using a "Here" string
- Auto-detection of filename argument (assumed to contain a list)
- Skip any commented lines within file
- Handles any number of arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | $psScriptName = "MyScript.ps1" $psScriptAuth = "Kahuna at PoshTips.com" $psScriptDate = "11/18/2009" ############################################################################## ## MyScript.ps1 - powershell script template ## ## Original Script by Kahuna at PoshTips.com - 11/18/2009 ## ## Maintenance (most recent updates at top): ## Date By Comments ## ---------- --- ---------------------------------------------------------- ## 11/18/2009 KPS new script ## ############################################################################## $scriptUsage = @" ============================================================================== SCRIPT : $psScriptName AUTHOR : $psScriptAuth VERSION : $psScriptDate ============================================================================== PURPOSE: powershell script to <decribe functionality here> USAGE: MyScript [hostname(s) | filename(s)][-OPTION1][-OPTION2][-?|-H] - From the PowerShell command-line, invoke the script, passing any combination of hostnames and filenames (text file(s) containing a hostname listing - Any combinations and number of arguments can be used - if an argument is a filename: - each line in the file will processed as a hostname - any lines starting with `# will be treated as comments and ignored - if an argument is not a filename: - the argument will processed as a hostname FLAGS: -OPTION1 = Describe option here or remove -OPTION2 = Describe option here or remove -? or -H = Display this help EXAMPLES: ./MyScript server1 hostlist.txt server2 server3 -OPTION1 -OPTION2 ./MyScript host1 host2 host2 ============================================================================== "@ Set-PSDebug -Trace 0 $script:recCount = 0 function ShowUsage([string]$message=""){ $message $scriptUsage exit } function DoSomething([string]$hostName) { #Here is where you will do some work with $hostname parameter ++$script:recCount; "$script:recCount,$hostName" } ###################################################### ## MAIN ###################################################### if ($args.count) { # Process command-line flags foreach ($item in $args) { if ($item -like "-*") { switch ($item) { "-OPTION1" { #setup this option here or in a new function } "-OPTION2" { #setup this option here or in a new function } "-H" {ShowUsage;} "-?" {ShowUsage;} default {ShowUsage "ERROR: $item is invalid!";} } } } # Write the CSV header "LN,HostName" # Process non-flag arguments foreach ($item in $args) { if ($item -notlike "-*") { # if argument is filename: process file contents if (test-path($item)) { foreach ($hostname in get-content $item) { $hostname = $hostname.trim() if ($hostname -notlike "#*") { DoSomething $hostname } } } # else treat as hostname else { DoSomething($item) } } } } else { ShowUsage "ERROR: This script requires arguments" } |
Posted by xb90 at 1:43 pm
Recent Comments