[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 初涉Powershell 不懂,求注释

本帖最后由 terse 于 2013-11-1 16:15 编辑

下载了一段Powershell代码  对其中功能的实现不了解 谁能注释下吗 感谢
  1. function Get-WebSite {
  2. <#  
  3. .SYNOPSIS  
  4.     Retrieves information about a website.
  5. .DESCRIPTION
  6.     Retrieves information about a website.
  7. .PARAMETER Url
  8.     URL of the website to test access to.
  9. .PARAMETER UseDefaultCredentials
  10.     Use the currently authenticated user's credentials  
  11. .PARAMETER Proxy
  12.     Used to connect via a proxy
  13. .PARAMETER TimeOut
  14.     Timeout to connect to site, in milliseconds
  15. .PARAMETER Credential
  16.     Provide alternate credentials              
  17. .NOTES  
  18.     Name: Get-WebSite
  19.     Author: Boe Prox
  20.     DateCreated: 08Feb2011        
  21. .EXAMPLE  
  22.     Get-WebSite -url "http://www.bing.com"
  23.      
  24. Description
  25. ------------
  26. Returns information about Bing.Com to include StatusCode and type of web server being used to host the site.
  27. #>
  28. [cmdletbinding(
  29.     DefaultParameterSetName = 'url',
  30.     ConfirmImpact = 'low'
  31. )]
  32.     Param(
  33.         [Parameter(
  34.             Mandatory = $True,
  35.             Position = 0,
  36.             ParameterSetName = '',
  37.             ValueFromPipeline = $True)]
  38.             [string][ValidatePattern("^(http|https)\://*")]$Url,
  39.         [Parameter(
  40.             Position = 1,
  41.             Mandatory = $False,
  42.             ParameterSetName = 'defaultcred')]
  43.             [switch]$UseDefaultCredentials,
  44.         [Parameter(
  45.             Mandatory = $False,
  46.             ParameterSetName = '')]
  47.             [string]$Proxy,
  48.         [Parameter(
  49.             Mandatory = $False,
  50.             ParameterSetName = '')]
  51.             [Int]$Timeout,
  52.         [Parameter(
  53.             Mandatory = $False,
  54.             ParameterSetName = 'altcred')]
  55.             [switch]$Credential           
  56.                         
  57.         )
  58. Begin {     
  59.     $psBoundParameters.GetEnumerator() | % {
  60.         Write-Verbose "Parameter: $_"
  61.         }
  62.    
  63.     #Create the initial WebRequest object using the given url
  64.     Write-Verbose "Creating the web request object"      
  65.     $webRequest = [net.WebRequest]::Create($url)
  66.      
  67.     #Use Proxy address if specified
  68.     If ($PSBoundParameters.ContainsKey('Proxy')) {
  69.         #Create Proxy Address for Web Request
  70.         Write-Verbose "Creating proxy address and adding into Web Request"
  71.         $webRequest.Proxy = New-Object -TypeName Net.WebProxy($proxy,$True)
  72.         }
  73.          
  74.     #Set timeout
  75.     If ($PSBoundParameters.ContainsKey('TimeOut')) {
  76.         #Setting the timeout on web request
  77.         Write-Verbose "Setting the timeout on web request"
  78.         $webRequest.Timeout = $timeout
  79.         }        
  80.      
  81.     #Determine if using Default Credentials
  82.     If ($PSBoundParameters.ContainsKey('UseDefaultCredentials')) {
  83.         #Set to True, otherwise remains False
  84.         Write-Verbose "Using Default Credentials"
  85.         $webrequest.UseDefaultCredentials = $True
  86.         }
  87.     #Determine if using Alternate Credentials
  88.     If ($PSBoundParameters.ContainsKey('Credentials')) {
  89.         #Prompt for alternate credentals
  90.         Write-Verbose "Prompt for alternate credentials"
  91.         $wc.Credential = (Get-Credential).GetNetworkCredential()
  92.         }            
  93.          
  94.     #Set TimeStamp prior to attempting connection   
  95.     $then = get-date
  96.     }
  97. Process {   
  98.     Try {
  99.         #Make connection to gather response from site
  100.         $response = $webRequest.GetResponse()
  101.         #If successful, get the date for comparison
  102.         $now = get-date
  103.          
  104.         #Generate report
  105.         Write-Verbose "Generating report from website connection and response"
  106.         $report = @{
  107.             URL = $url
  108.             StatusCode = $response.Statuscode -as [int]
  109.             StatusDescription = $response.StatusDescription
  110.             ResponseTime = "$(($now - $then).totalseconds)"
  111.             WebServer = $response.Server
  112.             Size = $response.contentlength
  113.             }
  114.         }
  115.     Catch {
  116.         #Get timestamp of failed attempt
  117.         $now = get-date
  118.         #Put the current error into a variable for later use
  119.         $errorstring = "$($error[0])"
  120.          
  121.         #Generate report
  122.         $report = @{
  123.             URL = $url
  124.             StatusCode = ([regex]::Match($errorstring,"\b\d{3}\b")).value
  125.             StatusDescription = (($errorstring.split('\)')[2]).split('.\')[0]).Trim()
  126.             ResponseTime = "$(($now - $then).totalseconds)"
  127.             WebServer = $response.Server
  128.             Size = $response.contentlength
  129.             }   
  130.         }
  131.     }
  132. End {        
  133.     #Display Report   
  134.     New-Object PSObject -property $report
  135.     }   
  136. }  
  137. Get-WebSite -url "http://www.bing.com"
复制代码
  1. function Get-WebPage {
  2. <#  
  3. .SYNOPSIS  
  4.    Downloads web page from site.
  5. .DESCRIPTION
  6.    Downloads web page from site and displays source code or displays total bytes of webpage downloaded
  7. .PARAMETER Url
  8.     URL of the website to test access to.
  9. .PARAMETER UseDefaultCredentials
  10.     Use the currently authenticated user's credentials  
  11. .PARAMETER Proxy
  12.     Used to connect via a proxy
  13. .PARAMETER Credential
  14.     Provide alternate credentials
  15. .PARAMETER ShowSize
  16.     Displays the size of the downloaded page in bytes                 
  17. .NOTES  
  18.     Name: Get-WebPage
  19.     Author: Boe Prox
  20.     DateCreated: 08Feb2011        
  21. .EXAMPLE  
  22.     Get-WebPage -url "http://www.bing.com"
  23.      
  24. Description
  25. ------------
  26. Returns information about Bing.Com to include StatusCode and type of web server being used to host the site.
  27. #>
  28. [cmdletbinding(
  29.     DefaultParameterSetName = 'url',
  30.     ConfirmImpact = 'low'
  31. )]
  32.     Param(
  33.         [Parameter(
  34.             Mandatory = $True,
  35.             Position = 0,
  36.             ParameterSetName = '',
  37.             ValueFromPipeline = $True)]
  38.             [string][ValidatePattern("^(http|https)\://*")]$Url,
  39.         [Parameter(
  40.             Position = 1,
  41.             Mandatory = $False,
  42.             ParameterSetName = 'defaultcred')]
  43.             [switch]$UseDefaultCredentials,
  44.         [Parameter(
  45.             Mandatory = $False,
  46.             ParameterSetName = '')]
  47.             [string]$Proxy,
  48.         [Parameter(
  49.             Mandatory = $False,
  50.             ParameterSetName = 'altcred')]
  51.             [switch]$Credential,
  52.         [Parameter(
  53.             Mandatory = $False,
  54.             ParameterSetName = '')]
  55.             [switch]$ShowSize                       
  56.                         
  57.         )
  58. Begin {     
  59.     $psBoundParameters.GetEnumerator() | % {
  60.         Write-Verbose "Parameter: $_"
  61.         }
  62.    
  63.     #Create the initial WebClient object
  64.     Write-Verbose "Creating web client object"
  65.     $wc = New-Object Net.WebClient
  66.     $wc.Encoding = [System.Text.Encoding]::UTF8
  67.     #Use Proxy address if specified
  68.     If ($PSBoundParameters.ContainsKey('Proxy')) {
  69.         #Create Proxy Address for Web Request
  70.         Write-Verbose "Creating proxy address and adding into Web Request"
  71.         $wc.Proxy = New-Object -TypeName Net.WebProxy($proxy,$True)
  72.         }      
  73.      
  74.     #Determine if using Default Credentials
  75.     If ($PSBoundParameters.ContainsKey('UseDefaultCredentials')) {
  76.         #Set to True, otherwise remains False
  77.         Write-Verbose "Using Default Credentials"
  78.         $wc.UseDefaultCredentials = $True
  79.         }
  80.     #Determine if using Alternate Credentials
  81.     If ($PSBoundParameters.ContainsKey('Credentials')) {
  82.         #Prompt for alternate credentals
  83.         Write-Verbose "Prompt for alternate credentials"
  84.         $wc.Credential = (Get-Credential).GetNetworkCredential()
  85.         }         
  86.          
  87.     }
  88. Process {   
  89.     Try {
  90.         If ($ShowSize) {
  91.             #Get the size of the webpage
  92.             Write-Verbose "Downloading web page and determining size"
  93.             "{0:N0}" -f ($wr.DownloadString($url) | Out-String).length -as [INT]
  94.             }
  95.         Else {
  96.             #Get the contents of the webpage
  97.             Write-Verbose "Downloading web page and displaying source code"
  98.             $wc.DownloadString($url)      
  99.             }
  100.          
  101.         }
  102.     Catch {
  103.         Write-Warning "$($Error[0])"
  104.         }
  105.     }   
  106. }  
  107. Get-WebPage -url "http://www.bing.com"
复制代码
只用一些简单的
  1. $url = 'http://www.bing.com'
  2. $wc = new-object system.net.webclient
  3. $wc.downloadstring($url)
复制代码

本帖最后由 terse 于 2013-11-2 18:33 编辑

回复 5# PowerShell
无论怎样先谢你的解答
唉~只怪老夫乃天生愚笨,虽得版主指点,也不解心中迷困,
其实代码本身并不复杂 主要powershell很多的类方法都不熟悉啊
这里参数怎么传进去  传进去处理流程怎样
发现下面参数里的 -URL 有和没有一样 是否里面定义HTTP和HTTPS开头就定义给 $URL 呢?
如果要传多个参数怎么调用呢 最多支持几个参数进去  
如每次调用 要变换PROXY的话怎么办 等等
最好有范例出来就好了  或许你们都忙 那就忽略吧
最后还是谢了哦

TOP

回复 7# PowerShell
再次谢你耐心的解答
先看会 看懂和看不懂 都要看的 必须的 谢谢

TOP

返回列表