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

[玩转系统] 周五乐趣:获取 PowerShell 用户组

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

周五乐趣:获取 PowerShell 用户组


有一天,Don Jones 在推特上发布了有关寻找 PowerShell 用户组的信息。如果您不知道,几乎所有与 PowerShell 相关的用户组都可以在 http://powershellgroup.org 上在线找到。这是寻找您附近的用户组的绝佳资源。当然,Twitter 就是这样,有人开玩笑说缺少 Get-PSUserGroup cmdlet。所以,我把这个笑话当作一个挑战。唐也打算建造一些东西,但我还没有看到他想出了什么。我怀疑它会和我的相似。

因为我们有一个网站,所以我们可以使用 Invoke-WebRequest 来抓取它。

$URL = http://powershellgroup.org/og
$r = Invoke-WebRequest $url

生成的对象没有尝试解析文档对象模型 (DOM),而是具有一个名为 AllElements 的属性,正如其名称所暗示的那样。我必须查看页面上的源 HTML 来识别我需要引用的元素。由于我似乎可以通过 HTML 类获得我想要的内容,因此我按类对元素进行分组并将其转换为哈希表。

$c = $r.AllElements | group class -AsHashTable -AsString

[玩转系统] 周五乐趣:获取 PowerShell 用户组

我选择了哈希表,以便更轻松地获取给定类的所有元素。例如,我知道“视图字段视图字段标题”类会给我每个组的名称。

$c.'views-field views-field-title'

[玩转系统] 周五乐趣:获取 PowerShell 用户组

同样,我知道“视图字段视图字段描述”类将提供描述。

$c.'views-field views-field-description'

[玩转系统] 周五乐趣:获取 PowerShell 用户组

了解每个对象的外观后,您还可以通过管道连接到 Get-Member 来发现属性名称,从而轻松提取相关信息。

#get total group count less one for the header
$count = ($c.'views-field views-field-description'.count)
for ($i = 1; $i -lt $count; $i++) {
  $groupname = $c.'views-field views-field-title'[$i].innertext
  $description = $c.'views-field views-field-description'[$i].InnerText
  [pscustomobject]@{
    Name = $groupname
    Description = $description
  }
}

[玩转系统] 周五乐趣:获取 PowerShell 用户组

一旦掌握了核心概念,我就围绕它们构建了一个名为 Get-PSUserGroup 的高级函数。

Function Get-PSUserGroup {
<#
.Synopsis
Get PowerShell User Groups
.Description
This command uses Invoke-Webrequest to retrieve user group information from PowerShellGroup.org. This command has no parameters.
.Example
PS C:\> get-psusergroup

Name        : Central Texas Powershell Users Group
Description : Powershell Users Group in Central Texas
Contact     : crshnbrn
MemberCount : 3
Status      : Join
Link        : http://powershellgroup.org/CentralTexas%20-%20Austin

Name        : Strasbourg PowerShell User Group
Description : Strasbourg PowerShell User Group
Contact     : stephanevg
MemberCount : 27
Status      : Request membership
Link        : http://powershellgroup.org/strasbourg

Name        : Basel PowerShell User Group
Description : Basel PowerShell User Group
Contact     : stephanevg
MemberCount : 26
Status      : Request membership
Link        : http://powershellgroup.org/basel

...
.Link
Get-PSUserGroupDetail
Invoke-Webrequest
#>
[cmdletbinding()]
Param()

#define base URL
[string]$URL = "http://powershellgroup.org/og"

write-Verbose "Retrieving group data from $url"

Try {
    $r = Invoke-WebRequest $url -ErrorAction Stop
}
Catch {
    Throw $_.Exception.message
}

If ($r) {
    Write-Verbose "Processing results"
    $c = $r.AllElements | group class -AsHashTable -AsString
    #get total group count less one for the header
    $count = ($c.'views-field views-field-description'.count)

    Write-Verbose "Found $($count -1) groups"

    #regular expression for parsing out the HREF link
    [regex]$rx='<A href="(?<href>/\S+)"'

    #enumerate entries, skipping the first one which is a header
    for ($i = 1; $i -lt $count; $i++) {
        $groupname = $c.'views-field views-field-title'[$i].innertext
        #get link to the group page
        $href = $rx.match($c.'views-field views-field-title'[$i].innerhtml).Groups["href"].Value
        $link = "http://powershellgroup.org{0}" -f $href

        #write a custom object to the pipeline for each group
        [pscustomobject]@{
        Name = $groupname
        Description = $c.'views-field views-field-description'[$i].InnerText
        Contact = $c.'views-field views-field-name'[$i].innertext
        MemberCount = $c.'views-field views-field-member-count'[$i].InnerText
        Status = $c.'views-field views-field-subscribe'[$i].InnerText
        Link = $Link
        }
    } #for

} #if $r

} #end function

该命令不带任何参数,仅返回有关每个组的高级信息。

[玩转系统] 周五乐趣:获取 PowerShell 用户组

管道对象包含指向组页面的链接,这意味着您可以尝试如下操作:

get-psusergroup | out-gridview -title "Select a group" -PassThru | foreach { start $_.link}

所有组都发送到 Out-GridView。

[玩转系统] 周五乐趣:获取 PowerShell 用户组

选择一个或多个组,每个链接都应在您的浏览器中打开。现在您没有理由找不到 PowerShell 用户组。如果您附近没有这样的人,那就开始吧!

关于这个主题我还有一些其他有趣的事情,但我会留到另一天再讲。享受!!

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

取消回复欢迎 发表评论:

关灯