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. Guy Recommends:
The Free IP Address Tracker (IPAT)
|
|||||
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |