One Step Further: Working with New-Object


In this exercise, we create a couple of objects.

  1. Start Windows PowerShell by using Start | Run | Windows PowerShell. The PowerShell prompt will open by default at the root of your Documents And Settings.

  2. Create an instance of the wshNetwork object by using the New-Object cmdlet. Use the comobject argument, and give it the program ID for the wshNetwork object, which is “wscript.network”. Store the results in a variable called $wshnetwork. The code looks like the following:

     $wshnetwork = new-object -comobject "wscript.network"

  3. Use the EnumPrinterConnections method from the wshNetwork object to print out a list of printer connections that are defined on your local computer. To do this, use the wshNetwork object that is contained in the $wshnetwork variable. The command for this is as follows:

     $wshnetwork.EnumPrinterConnections()

  4. Use the EnumNetworkDrives method from the wshNetwork object to print out a list of network connections that are defined on your local computer. To do this, use the wshNetwork object that is contained in the $wshnetwork variable. The command for this is as follows:

     $wshnetwork.EnumNetworkDrives()

  5. Use the up arrow twice and retrieve the $wshnetwork.EnumPrinterConnections() command. Use the $colPrinters variable to hold the collection of printers that is returned by the command. The code looks as follows:

     $colPrinters = $wshnetwork.EnumPrinterConnections()

  6. Use the up arrow and retrieve the $wshnetwork.EnumNetworkDrives() command. Use the Home key to move the insertion point to the beginning of the line. Modify the command so that it holds the collection of drives returned by the command into a variable called $colDrives. This is shown here:

     $colDrives = $wshnetwork.EnumNetworkDrives()

  7. Use the $userName variable to hold the name that is returned by querying the username property from the wshNetwork object. This is shown here:

     $userName = $wshnetwork.UserName

  8. Use the $userDomain variable to hold the name that is returned by querying the UserDomain property from the wshNetwork object. This is shown here:

     $userDomain = $wshnetwork.UserDomain

  9. Use the $computerName variable to hold the name that is returned by querying the UserDomain property from the wshNetwork object. This is shown here:

     $computerName = $wshnetwork.ComputerName

  10. Create an instance of the wshShell object by using the New-Object cmdlet. Use the comobject argument and give it the program ID for the wshShell object, which is “wscript.shell”. Store the results in a variable called $wshShell. The code for this follows:

     $wshShell = new-object -comobject "wscript.shell"

  11. Use the Popup method from the wshShell object to produce a popup box that displays the domain name, user name, and computer name. The code for this follows:

     $wshShell.Popup($userDomain+"\$userName $computerName")

  12. Use the Popup method from the wshShell object to produce a popup box that displays the collection of printers held in the $colPrinters variable. The code looks as follows:

     $wshShell.Popup($colPrinters)

  13. Use the Popup method from the wshShell object to produce a popup box that displays the collection of drives held in the $colDrives variable. The code is as follows:

     $wshShell.Popup($colDrives)

  14. This concludes this one step further exercise. Completed commands for this exercise are in the image from book OneStepFurther.txt file.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net