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

[玩转系统] 获得 PowerShell 用户组更有趣

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

获得 PowerShell 用户组更有趣


几天前,我发布了一个 PowerShell 函数来检索有关 PowerShell 用户组的信息。该函数返回这样的基本组信息。

[玩转系统] 获得 PowerShell 用户组更有趣

网站上的每个组都有自己的页面,这就是链接属性的用途。因此,使用与我原来的帖子相同的技术从该页面抓取信息并不需要太多工作。同样,我需要分析源代码以确定要使用哪些类和属性。但最终的功能与第一个功能没有太大不同。

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

现在我可以直接从 PowerShell 获取组详细信息。

[玩转系统] 获得 PowerShell 用户组更有趣

如果您有这两个命令,您甚至可以将它们组合起来。

get-psusergroup | where {$_.name -match 'charlotte'} | Get-PSUserGroupDetail

这还不错。

[玩转系统] 获得 PowerShell 用户组更有趣

您可以使用 PowerShell 获取每个组的详细信息,但这可能非常耗时,因为处理是按顺序完成的。提高性能的一种方法是利用 PowerShell 工作流程中的并行 foreach 功能。我编写了另一个函数,实际上更多的是作为定义嵌套工作流程的概念证明。在此工作流程中,它以 8 个为一组并行处理链接集合。


Foreach -parallel -throttlelimit 8 ($url in $links) {
Sequence {
Try {
$detail = Invoke-WebRequest $url -ErrorAction Stop
}
Catch {
Throw
}
…

由于工作流旨在独立运行,因此我必须合并 Get-PSUserGroupDetail 中的代码,而不是尝试直接调用它。这是完整的功能。

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

但即使采用并行处理,这仍然不是一个快速的过程。在我的具有 8GB RAM 和非常快的 FiOS 连接的 Windows 8.1 机器上运行该命令仍然需要大约 2 分钟才能完成。但我想如果你不介意等待的话,这就是你可以期待的。

[玩转系统] 获得 PowerShell 用户组更有趣

我想说的是,拥有所有这些信息是很有趣的。

[玩转系统] 获得 PowerShell 用户组更有趣

或者你可以做这样的事情。

$us = $all | where {$_.country -eq 'United States'} | Group Region -AsHashTable -AsString

[玩转系统] 获得 PowerShell 用户组更有趣

我关于该主题的最后一个函数称为 Show-PSUserGroup。中央命令运行我原来的 Get-PSUserGroup 函数,该函数将结果通过管道传输到 Out-Gridview。从那里您可以选择一个或多个组,每个组的链接将在您的浏览器中打开。

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

[玩转系统] 获得 PowerShell 用户组更有趣

单击“确定”将在浏览器中打开每个链接。

[玩转系统] 获得 PowerShell 用户组更有趣

如果您已经收集了我的所有功能,我建议您创建一个模块文件。我将它们全部放在名为 PSUsergroups.psm1 的模块文件中。最后您所需要的只是导出命令。

Export-ModuleMember -Function *

将文件保存在必要的模块位置,您的命令即可准备就绪。

注意:如果您运行 PowerShell 用户组并且未在此站点上注册,我强烈建议您这样做。否则你会让人们很难找到你。

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

取消回复欢迎 发表评论:

关灯