Two Days PowerShell:2


 

  1. Controlling Execution of PowerShell Cmdlets
    1. Using -whatif to prototype a command
      1. The three arguments we can use to control execution are -whatif, -confirm, and suspend. Suspend is not really an argument that is supplied to a cmdlet, but rather is an action you can take at a confirmation prompt, and is therefore another method of controlling execution.
      2. Most of the Windows PowerShell cmdlets support a “prototype” mode that can be entered using the -whatif parameter. The implementation of -whatif can be decided on by the person developing the cmdlet; however, it is the recommendation of the Windows PowerShell team that developers implement -whatif.

    2. Confirming Commands
      1. As we saw in the previous section, we can use -whatif to prototype a cmdlet in Windows PowerShell. This is useful for seeing what a command would do; however, if we want to be prompted before the execution of the command, we can use the -confirm argument. The commands used in the Confirming the execution of cmdlets procedure are listed in the ConfirmingExecutionOfCmdlets.txt file.

    3. Suspending Confirmation of Cmdlets
      1. The ability to prompt for confirmation of the execution of a cmdlet is extremely useful and at times may be vital to assisting in maintaining a high level of system uptime. There are times when you have typed in a long command and then remember that you need to do something else first. For such eventualities, you can tell the confirmation you would like to suspend execution of the command. The commands used for suspending execution of a cmdlet are in the SuspendConfirmationOfCmdlets.txt file.


  2.  Working with Windows PowerShell
    1. Configuring Windows PowerShell
      1. cmdlet> Export-Console myconsole ; Many items can be configured for Windows PowerShell. These items can be stored in a Psconsole file. To export the Console configuration file, use the Export-Console cmdlet;
    2. Controlling PowerShell launch options
      1. cmdlet> PowerShell -nologo ;Launch Windows PowerShell without the banner by using the -nologo argument.
      2. cmdlet> PowerShell -version 1 ;Launch a specific version of Windows PowerShell by using the -version argument.
      3. cmdlet> PowerShell -psconsolefile myconsole.psc1;Launch Windows PowerShell using a specific configuration file by specifying the -psconsolefile argument.
      4. cmdlet> powershell -command "& {get-process}";Launch Windows PowerShell, execute a specific command, and then exit by using the -command argument. The command itself must be prefixed by the ampersand sign (&) and enclosed in curly brackets.
  3. Supplying Options for Cmdlets
    1. Keep in mind that all cmdlets will not implement these parameters. However, if these parameters are used, they will be interpreted in the same manner for all cmdlets because it is the Windows PowerShell engine itself that interprets the parameter.

    2. Parameter

      Meaning

      -whatif

      Tells the cmdlet to not execute but to tell you what would happen if the cmdlet were to run

      -confirm

      Tells the cmdlet to prompt before executing the command

      -verbose

      Instructs the cmdlet to provide a higher level of detail than a cmdlet not using the verbose parameter

      -debug

      Instructs the cmdlet to provide debugging information

      -ErrorAction

      Instructs the cmdlet to perform a certain action when an error occurs. Allowed actions are: continue, stop, silentlyContinue, and inquire.

      -ErrorVariable

      Instructs the cmdlet to use a specific variable to hold error information. This is in addition to the standard $error variable.

      -Outvariable

      Instructs the cmdlet to use a specific variable to hold the output information

      -OutBuffer

      Instructs the cmdlet to hold a certain number of objects before calling the next cmdlet in the pipeline


  4. Working with the Help Options
    1. cmdlet> get-help get-help
    2. cmdlet> get-help get-help ;This command prints out help about the Get-Help cmdlet.
    3. cmdlet> get-help about* ;To retrieve a listing of all the conceptual help articles
    4. cmdlet> get-help get*
    5. cmdlet> get-help get-p*
    6. cmdlet> get-help get-psdrive -examples
    7. cmdlet> get-help Set-Alias ;To create an alias for a cmdlet, confirm there is not already an alias to the cmdlet by using Get-Alias. Use Set-Alias to assign the cmdlet to a unique key stroke combination.
    8. cmdlet> set-alias gh get-help
  5. Exploring Commands: Step-by-Step Exercises
    1. cmdlet> Set-location $pshome ;Change the working directory to the PowerShell working directory.
    2. cmdlet> typeperf "\memory\available bytes" ;Obtain a listing of memory counters related to the available bytes by using the typeperf command. 
    3. cmdlet> ctrl-c ;After a few counters have been displayed in the PowerShell window, use the ctrl-c command to break the listing.
    4. cmdlet> Bootcfg ;Display the current boot configuration by using the bootcfg command: 
    5. cmdlet> set-location c:\mytest ;Change the working directory back to the C:\Mytest directory you created earlier: 
    6. cmdlet> fsutil file createnew mytestfile.txt 1000 ;Create a file named Mytestfile.txt in the C:\Mytest directory. Use the fsutil utility, and make the file 1,000 bytes in size.
    7. cmdlet> get-childitem ;Obtain a “directory listing” of all the files in the C:\Mytest directory by using the Get-ChildItem cmdlet. 
    8. cmdlet> get-date ;Print out the current date by using the Get-Date cmdlet. 
    9. cmdlet> cls ;Clear the screen by using the cls command.
    10. cmdlet> get-command ;Print out a listing of all the cmdlets built into Windows PowerShell. 
    11. cmdlet> get-command -name get-alias ;Use the Get-Command cmdlet to get the Get-Alias cmdlet. To do this, use the -name argument while supplying Get-Alias as the value for the argument. 
    12. cmdlet>  exit  ;Exit the Windows PowerShell by typing exit and pressing Enter
  6. One Step Further: Obtaining Help
    1. cmdlet> get-help get-help -detailed
    2. cmdlet> get-help get-help -full
    3. cmdlet> get-help get-help -examples
    4. cmdlet> get-help about*
    5. cmdlet> get-help get*
    6. cmdlet> get-help set*
    7. cmdlet> exit
  7. Quick Reference

 

 

table format:border="1" bordercolor="CCCCCC" 

posted @ 2010-03-31 00:00  Freedom  阅读(499)  评论(0编辑  收藏  举报