当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 周五乐趣:修改后的每日报价

作者:精品下载站 日期:2024-12-14 07:39:20 浏览:13 分类:玩电脑

周五乐趣:修改后的每日报价


[玩转系统] 周五乐趣:修改后的每日报价

一段时间以来,我一直在使用一个函数从 Brainyquotes 获取当天的最新报价。当我第一次编写该函数时,我不得不求助于 .NET 和 webclient 类。但现在有了 PowerShell 3.0,我们有了新的 Web cmdlet,它们更容易使用,所以我决定重写我的函数。

#requires -version 3.0

# -----------------------------------------------------------------------------
# Script: Get-QOTD.ps1
# Author: Jeffery Hicks
#    https://jdhitsolutions.com/blog
#    follow on Twitter: http://twitter.com/JeffHicks
# Date: 6/28/2013
# Version: 2.0
# Keywords: RSS, XML, REST
# Comments:
#
# "Those who neglect to script are doomed to repeat their work."
#
#  ****************************************************************
#  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
#  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
#  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
#  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
#  ****************************************************************

# -----------------------------------------------------------------------------

Function Get-QOTD {
<#
.Synopsis
Download quote of the day.
.Description
Using Invoke-RestMethod download the quote of the day from the BrainyQuote RSS
feed. The URL parameter has the necessary default value.
.Example
PS C:\> get-qotd
"We choose our joys and sorrows long before we experience them." - Khalil Gibran
.Link
Invoke-RestMethod
#>
    [cmdletBinding()]

    Param(
    [Parameter(Position=0)]
    [ValidateNotNullorEmpty()]
    [string]$Url="http://feeds.feedburner.com/brainyquote/QUOTEBR"
    )

    Write-Verbose "$(Get-Date) Starting Get-QOTD"  
    Write-Verbose "$(Get-Date) Connecting to $url" 

    Try
    {
        #retrieve the url using Invoke-RestMethod
        Write-Verbose "$(Get-Date) Running Invoke-Restmethod"

        #if there is an exception, store it in my own variable.
        $data = Invoke-RestMethod -Uri $url -ErrorAction Stop -ErrorVariable myErr

        #The first quote will be the most recent
        Write-Verbose "$(Get-Date) retrieved data"
        $quote = $data[0]
    }
    Catch
    {
        $msg = "There was an error connecting to $url. "
        $msg += "$($myErr.Message)."

        Write-Warning $msg
    }

    #only process if we got a valid quote response
    if ($quote.description)
    {
        Write-Verbose "$(Get-Date) Processing $($quote.OrigLink)"
        #write a quote string to the pipeline
        "{0} - {1}" -f $quote.Description,$quote.Title
    }
    else
    {
        Write-Warning "Failed to get expected QOTD data from $url."
    }

    Write-Verbose "$(Get-Date) Ending Get-QOTD"

} #end Get-QOTD

#OPTIONAL: create an alias
#Set-Alias -name "qotd" -Value Get-QOTD

该功能正在从 RSS 源下载 XML 内容。我发现使用 Invoke-RestMethod 是完成此任务的一个方便的 cmdlet,因为它将数据格式化为易于使用的对象。我的函数所做的就是编写一个由最新条目的不同属性组成的字符串。我本周在 TrainSignal 博客上发表的第一篇文章使用了一些相同的代码。

好的。也许您不需要日常灵感,但现在您已经看到了 Invoke-Restmethod 的另一个实际示例,也许这是您所需要的。在那里享受并享受乐趣。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯