[玩转系统] 获得 PowerShell 用户组更有趣
作者:精品下载站 日期:2024-12-14 07:45:43 浏览:16 分类:玩电脑
获得 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 获取组详细信息。
如果您有这两个命令,您甚至可以将它们组合起来。
get-psusergroup | where {$_.name -match 'charlotte'} | Get-PSUserGroupDetail
这还不错。
您可以使用 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 分钟才能完成。但我想如果你不介意等待的话,这就是你可以期待的。
我想说的是,拥有所有这些信息是很有趣的。
或者你可以做这样的事情。
$us = $all | where {$_.country -eq 'United States'} | Group Region -AsHashTable -AsString
我关于该主题的最后一个函数称为 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
单击“确定”将在浏览器中打开每个链接。
如果您已经收集了我的所有功能,我建议您创建一个模块文件。我将它们全部放在名为 PSUsergroups.psm1 的模块文件中。最后您所需要的只是导出命令。
Export-ModuleMember -Function *
将文件保存在必要的模块位置,您的命令即可准备就绪。
注意:如果您运行 PowerShell 用户组并且未在此站点上注册,我强烈建议您这样做。否则你会让人们很难找到你。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag