Ezine 161 - PowerShell Listing SharesEzine 161 - PowerShell Listing SharesMost servers benefit from file shares as points of contact with users or other servers. PowerShell has a rich selection of commands to list, create and connect to such shares. Topics for PowerShell and Shares
This Week's Secret - How to Uninstall PowerShellThere is one aspect of PowerShell that I have neglected to emphasise in previous ezines, that is when you install PowerShell on Windows Server 2003, it acts like a hotfix for the operating system, rather than a stand-alone program. This relationship becomes important if you ever have to uninstall PowerShell, the key is to research the PowerShell Hotfix number for your operating system, for example: KB926139. The uninstall procedure on XP is different and more traditional. Call for XP's Add or Remove Programs wizard and just look amongst the Installed Programs for: Windows PowerShell(TM) 1.0. As for Vista, the secret is to launch Appwiz.cpl then, crucially, find 'View Installed Updates'. This link should lead you to Windows PowerShell(TM) 1.0, from there it's easy to double click and uninstall. This Week's MissionPowerShell can automate a variety of tasks involving shared folders. For instance, you may be curious for a list of shares on a particular server. Alternatively, you may need to create network shares so that users can connect to the server. There again, you may need a logon script which maps a network drive to a local drive letter. While you can achieve all the above with PowerShell commands, in this issue I will concentrate just on listing the shares. For this, and similar missions, PowerShell works hand in glove with WMI (Windows Management Instrumentation) objects, consequently, any knowledge of WMI through VBScript will transfer easily to PowerShell. Pre-requisitesAs a preliminary to running these scripts, download and install PowerShell and .NetFramework. Go to Microsoft's download site and choose the flavour to suit your operating system. To get these scripts to work, either copy them into the PowerShell interface, or else save the code in notepad and create cmdlet files with a .ps1 extension. Then call your cmdlet with ./filename. Example 1 - List or Enumerate SharesI love the word enumerate. The word 'Enumerate' simply bursts with mathematical gravitas. In comparison, 'list' sounds so weedy and ineffectual. Well, this script goes and gets the share names and displays them in tabular form. # PowerShell script to enumerate shared folders get-WmiObject -class Win32_Share -computer LocalHost Note 1: Once the script works, then you can substitute the name of another machine on your network for 'LocalHost'. Alternatively, you could remove these two words -computer LocalHost, thereby proving that PowerShell substitutes a default value for this parameter. Note 2: The hash symbol # is to REMark out the line with a comment. Note 3 Intelligence: PowerShell is a particularly intelligent and forgiving scripting language. For example it can deduce from its (pole) position that Win32_Share is a class of WMI object. Thus where Win32_Share is the first parameter, it's optional to add the code: -class. What's more PowerShell has built-in aliases, this is why it can expand ft into format-Table, sort into sort-Object, and of course gwmi into get-WmiObject. This cleverness brings with it a special challenge, when we write scripts from scratch should we use the long-winded commands such as format-Table? Or should we use the alias ft, and risk not knowing what our own script means 6 months down the line? The answer depends on how comfortable you are with an alias, and if anyone less experienced will need to modify your script. ˆ Example 2 - Greater EfficiencyIn Example 2 (below) my main goal is to control the content of PowerShell's output.
However, I have a subsidiary aim, to introduce PowerShell's short-hand ways. The first part is
condensed because: # PowerShell script to enumerate shared folders gwmi Win32_Share | sort type, name | ft name, type, path -auto Challenge 1: Amend the sort command. For example, sort by path, then name. Challenge 2: Try a cosmetic change. After 'ft name',
choose different properties, for example, description or status. Footnote,
how did I know about these extra properties? Try this: If you are looking for handy network utilities, try some of the free downloads at Tools4Ever Example 3 - Learning with get-Help, get-Member and get-CommandIt is rare for experts to agree; however, all PowerShell gurus will tell you that the three cmdlets that provide the foundation for learning this language are, get-Help, get-Member and get-Command. In many ways this section should have come first. My problem is that I am always restless to show you a script that does real work. But now the time has come to reflect on how you could have worked out my previous examples for yourself. The practical point of this script is to show you how to research more WMI objects. Knowledge of WMI will enable you to automate numerous other server and network tasks. Instructions: Copy only the first line into PowerShell, delete the # hash, and only then execute the command. Repeat the procedure for the other 3 lines. # get-Command get* Note 1: Curiously, Example 3 will do nothing - unless you remove the #. Please, please remove one # at a time; for perfection, replace the # as you move down the list of commands. My point is that the above example is really 4 completely separate learning techniques; they will demonstrate the key cmdlets: get-Command, get-Help and get-Member. Summary of PowerShell Listing SharesHere are PowerShell examples to help you take an inventory of shares on a server. Along the way you can learn the role of -parameters. If you go the extra mile, you can study get-Command, get-Help and get-Member, thus learn for yourself and thus make me redundant. See more Windows PowerShell real life tasks• PowerShell Home • Real life tasks • IpConfig • Exchange • Com objects • Services • Syntax Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
*
|
||||||