Thú vị hơn khi có được các nhóm người dùng PowerShell
Vài ngày trước tôi đã đăng một chức năng PowerShell để lấy thông tin về các nhóm người dùng PowerShell. Hàm đó trả về thông tin nhóm cơ bản như thế này.
Mỗi nhóm trên trang web đều có trang riêng, đó là mục đích của thuộc tính Liên kết đó. Vì vậy, không mất nhiều công sức để sử dụng các kỹ thuật tương tự như bài đăng ban đầu của tôi để lấy thông tin từ trang đó. Một lần nữa, tôi cần phân tích mã nguồn để xác định nên sử dụng những lớp và thuộc tính nào. Nhưng chức năng cuối cùng không khác nhiều so với chức năng đầu tiên.
Function Get-PSUserGroupDetail {
<#
.Synopsis
Get PowerShell user group detail
.Description
This command will get detailed information about a PowerShell user group from Powershellgroup.org. You will need the link for the group which you can get with Get-PSUserGroup.
.Example
PS C:> $g = Get-PSUserGroup
This gets basic user group information for all groups.
PS C:\> $g | where {$_.name -match 'charlotte'} | Get-PSUserGroupDetail
Name : Charlotte PowerShell Users Group
Email : [email
Meeting : First Thursday of the month starting Jan 2012
Location : Charlotte
Region : NC
Country : United States
Mission : The Charlotte PowerShell Users Group offers learning and support
opportunities for IT administrators and developers working with
PowerShell. While some meetings include guest speakers, most
meetings are designated as "Script Club" sessions, where group
members can bring in their own PowerShell scripts and receive
advice and assistance from fellow group members. In addition, the
group actively participates in the annual Scripting Games and holds
it own mini-competitions to foster PowerShell knowledge in the
Charlotte area.
Link : http://powershellgroup.org/charlotte.nc
Filtering for a specific group and then getting detail.
.Link
Get-PSUserGroup
Invoke-Webrequest
#>
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
HelpMessage="The URL for detailed user group information")]
[ValidateNotNullorEmpty()]
[Alias("link")]
[string]$URL
)
Process {
write-Verbose "Retrieving group data from $url"
Try {
$detail = Invoke-WebRequest $url -ErrorAction Stop
}
Catch {
Throw
}
if ($detail ) {
Write-Verbose "Processing results"
$class = $detail.AllElements | group class -AsHashTable -AsString
#suppress any errors for non-defined values which will leave
#corresponding property names with a null value
$script:ErrorActionPreference = "SilentlyContinue"
$email = ($class.'field field-type-email field-field-email'.innerText).Split(":")[1].Trim()
$mission = $class.'og-mission'.innertext.Trim()
$meeting = $class.'field field-type-text field-field-meetingtime'.innertext.Split(":")[1].Trim()
$location = $class.locality[0].innertext
$region = $class.region[0].innertext
$country = $class.'country-name'[0].innerText
$groupname = $class.title.where({$_.tagname -eq "H1"}).innerText
Write-Verbose "Creating results for $groupname"
#write a custom object to the pipeline
[pscustomobject]@{
Name = $groupname
Email = $email
Meeting = $meeting
Location = $Location
Region = $region
Country = $Country
Mission = $mission
Link = $Url
}
#reset variables in case there were errors
Remove-variable -Name groupname,email,meeting,location,region,mission
} #if $r
} #process
} #end function
Bây giờ tôi có thể lấy thông tin chi tiết về nhóm trực tiếp từ PowerShell.
Nếu bạn có cả hai lệnh, bạn thậm chí có thể kết hợp chúng.
get-psusergroup | where {$_.name -match 'charlotte'} | Get-PSUserGroupDetailĐiều này không quá tệ.
Bạn có thể sử dụng PowerShell để nhận thông tin chi tiết cho từng nhóm nhưng điều đó có thể tốn thời gian vì quá trình xử lý được thực hiện tuần tự. Một cách bạn có thể cải thiện hiệu suất là tận dụng tính năng tìm kiếm song song trong quy trình làm việc PowerShell. Tôi đã viết một hàm khác, thực sự giống như một bằng chứng về khái niệm xác định một quy trình làm việc lồng nhau. Trong quy trình công việc này, nó xử lý một tập hợp các liên kết song song theo lô 8 liên kết.
Foreach -parallel -throttlelimit 8 ($url in $links) {
Sequence {
Try {
$detail = Invoke-WebRequest $url -ErrorAction Stop
}
Catch {
Throw
}
…Vì quy trình công việc dự định chạy riêng biệt nên tôi phải kết hợp mã từ Get-PSUserGroupDetail, thay vì cố gắng gọi trực tiếp. Đây là chức năng hoàn chỉnh.
Function Get-PSUserGroupDetailAll {
<#
.Synopsis
Get details for all PowerShell user groups.
.Description
This command is similar to Get-PSUserGroupDetail, except it will use a PowerShell workflow to retrieve all user group detail in parallel. While this aids in performance this command could still take a few minutes to complete. There are no parameters.
.Example
PS C:\> Get-PSUserGroupDetailAll
Name : Pac IT Pros - PowerShell - Sacramento
Email : [email
Meeting : First Tuesday of the month 6-9 pm Pacific time and simulcast online
Location : Sacramento
Region : CA
Country : United States
Mission : Pac IT Pros - PowerShell - Sacramento, CA
PowerShell users group meeting in Sacramento, CA and online.
Meetings are on
the first Tuesday of the month, 6-9 pm (Pacific) and simulcast on
the web.
Link : http://powershellgroup.org/Sacramento
...
.Link
Get-PSUserGroupDetail
#>
[cmdletbinding()]
Param()
Write-Verbose "Starting $($MyInvocation.Mycommand)"
#define a nested workflow using the core code from Get-PSUsergroupdetail
Workflow GetDetail {
Param([string[]]$Links)
Foreach -parallel -throttlelimit 8 ($url in $links) {
Sequence {
Try {
$detail = Invoke-WebRequest $url -ErrorAction Stop
}
Catch {
Throw
}
if ($detail ) {
Write-Verbose "Processing results"
#reset variables in case there were errors
$groupname = $null
$email = $null
$meeting = $null
$location = $null
$region = $null
$country = $null
$mission = $null
$classElements = $detail.AllElements | group class -AsHashTable -AsString
Try {
$email = ($classElements.'field field-type-email field-field-email'.innerText).Split(":")[1].Trim()
} Catch { $email = $null }
Try {
$mission = $classElements.'og-mission'.innertext.Trim()
} Catch { $mission = $null }
Try {
$meeting = $classElements.'field field-type-text field-field-meetingtime'.innertext.Split(":")[1].Trim()
} Catch { $meeting = $null }
Try {
$location = $classElements.locality[0].innertext
} Catch { $location = $null }
Try {
$region = $classElements.region[0].innertext
} Catch { $region = $null }
Try {
$country = $classElements.'country-name'[0].innerText
} Catch { $country = $null }
Try {
$groupname = $classElements.title.where({$_.tagname -eq "H1"}).innerText
} Catch { $groupname = $null }
Write-Verbose "Creating results for $groupname"
#write a custom object to the pipeline
[pscustomobject]@{
Name = $groupname
Email = $email
Meeting = $meeting
Location = $Location
Region = $region
Country = $Country
Mission = $mission
Link = $Url
}
} #if $detail
} #sequence
} #foreach
} #end workflow
write-Verbose "Retrieving group data"
$links = Get-PSUserGroup | Select -ExpandProperty Link
Write-Verbose "Processing $($links.count) links"
#invoke the workflow
$results = GetDetail $links
#write results to pipeline without Workflow properties
$Results | Select * -ExcludeProperty PS*
Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end function
Nhưng ngay cả với việc xử lý song song, đây vẫn không phải là một quá trình nhanh chóng. Chạy lệnh trên hộp Windows 8.1 với 8GB RAM và kết nối FiOS rất nhanh vẫn mất khoảng 2 phút để hoàn thành. Nhưng tôi cho rằng nếu bạn không ngại chờ đợi thì đây là những gì bạn có thể mong đợi.
Tôi sẽ nói rằng việc có tất cả những thông tin này thật thú vị khi chơi.
Hoặc bạn có thể làm điều gì đó như thế này.
$us = $all | where {$_.country -eq 'United States'} | Group Region -AsHashTable -AsString
Chức năng cuối cùng của tôi về chủ đề này được gọi là Show-PSUserGroup. Lệnh trung tâm chạy hàm Get-PSUserGroup ban đầu của tôi để chuyển kết quả sang Out-Gridview. Từ đó bạn có thể chọn một hoặc nhiều nhóm và liên kết của mỗi nhóm sẽ mở ra trong trình duyệt của bạn.
Function Show-PSUserGroup {
<#
.SYNOPSIS
Graphically display PowerShell user groups.
.DESCRIPTION
This command will display PowerShell user groups using Out-Gridview. You can select one or more groups which should open the corresponding page in your web browser.
.NOTES
NAME : Show-PSUserGroup
VERSION : 1.0
LAST UPDATED: 4/8/2015
.LINK
Get-PSUserGroup
.INPUTS
None
.OUTPUTS
None
#>
[cmdletbinding()]
Param()
Get-PSUserGroup |
Out-GridView -Title "Select one or more user groups" -OutputMode Multiple |
Foreach {
#write each result to the pipeline
$_
#open each link
Start $_.link
}
} #end function
Nhấp vào OK sẽ mở từng liên kết trong trình duyệt của tôi.
Nếu bạn đã thu thập tất cả các chức năng của tôi, tôi khuyên bạn nên tạo một tệp mô-đun. Tôi có tất cả chúng trong một tệp mô-đun có tên PSUsergroups.psm1. Tất cả những gì bạn cần ở cuối là một lệnh xuất.
Export-ModuleMember -Function *Lưu tệp vào vị trí mô-đun cần thiết và các lệnh của bạn sẽ sẵn sàng khi bạn cần.
LƯU Ý: Nếu bạn điều hành Nhóm người dùng PowerShell và chưa đăng ký trên trang này, tôi thực sự khuyến khích bạn làm như vậy. Nếu không, bạn đang làm cho mọi người khó tìm thấy bạn.







