ค้นหาเว็บไซต์

ความสนุกวันศุกร์: รับกลุ่มผู้ใช้ PowerShell


เมื่อวันก่อน Don Jones ทวีตเกี่ยวกับการค้นหากลุ่มผู้ใช้ PowerShell ในกรณีที่คุณไม่ทราบ คุณสามารถค้นหากลุ่มผู้ใช้ทุกกลุ่มที่เกี่ยวข้องกับ PowerShell ทางออนไลน์ได้ที่ http://powershellgroup.org นี่เป็นแหล่งข้อมูลที่ยอดเยี่ยมสำหรับการค้นหากลุ่มผู้ใช้ที่อยู่ใกล้คุณ แน่นอนว่า Twitter เป็นสิ่งที่มีคนล้อเล่นเกี่ยวกับการขาด Get-PSUserGroup cmdlet ดังนั้นการนำเรื่องตลกมาเป็นความท้าทายที่ฉันสร้างขึ้น ดอนกำลังจะสร้างอะไรบางอย่างเช่นกัน แต่ฉันไม่เห็นว่าเขาคิดขึ้นมาได้อย่างไร สงสัยคงจะเหมือนของผมล่ะมั้ง

เพราะเรามีเว็บไซต์ จึงสามารถขูดมันได้ด้วย Involve-WebRequest

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

แทนที่จะพยายามแยกวิเคราะห์ document object model (DOM) ออบเจ็กต์ผลลัพธ์จะมีคุณสมบัติที่เรียกว่า AllElements ซึ่งเป็นชื่อที่สื่อถึงทุกประการ ฉันต้องดูซอร์ส HTML บนหน้าเพื่อระบุองค์ประกอบที่ฉันต้องการอ้างอิง เนื่องจากดูเหมือนว่าฉันจะได้สิ่งที่ต้องการผ่านคลาส HTML ฉันจึงจัดกลุ่มองค์ประกอบตามคลาสและเปลี่ยนให้เป็นแฮชเทเบิล

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

ฉันเลือกใช้แฮชเทเบิลเพื่อให้ง่ายต่อการรับองค์ประกอบทั้งหมดสำหรับคลาสที่กำหนด ตัวอย่างเช่น ฉันรู้ว่าคลาส 'views-field views-field-title' จะให้ชื่อของแต่ละกลุ่มแก่ฉัน

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

ในทำนองเดียวกัน ฉันรู้ว่าคลาส 'views-field views-field-description' จะให้คำอธิบาย

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

เมื่อทราบว่าแต่ละออบเจ็กต์มีลักษณะอย่างไร คุณสามารถไปป์ที่ 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
  }
}

เมื่อฉันลดแนวคิดหลักลงแล้ว ฉันได้สร้างฟังก์ชันขั้นสูงรอบๆ แนวคิดเหล่านั้นที่เรียกว่า 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

คำสั่งไม่ใช้พารามิเตอร์ใดๆ และเพียงแค่ส่งคืนข้อมูลระดับสูงเกี่ยวกับแต่ละกลุ่ม

ออบเจ็กต์ไปป์ไลน์มีลิงก์ไปยังหน้าของกลุ่มซึ่งหมายความว่าคุณสามารถลองทำสิ่งนี้:

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

กลุ่มทั้งหมดจะถูกส่งไปยัง Out-GridView

เลือกหนึ่งกลุ่มขึ้นไป และแต่ละลิงก์ควรเปิดในเบราว์เซอร์ของคุณ ตอนนี้คุณไม่มีข้อแก้ตัวในการไม่พบกลุ่มผู้ใช้ PowerShell และถ้าที่นี่ไม่อยู่ใกล้คุณ เริ่มเลย!

ฉันมีสิ่งที่น่าสนใจอื่นๆ ในหัวข้อนี้ แต่ฉันจะบันทึกไว้วันอื่น สนุก!!

บทความที่เกี่ยวข้อง