[玩转系统] 更多 PowerShell 工具制作乐趣
作者:精品下载站 日期:2024-12-14 07:43:50 浏览:14 分类:玩电脑
更多 PowerShell 工具制作乐趣
因此,常见的任务是查找与特定状态(例如正在运行或已停止)相匹配的服务。您很可能见过这样的表达方式:
get-service -computername server01 | where {$_.status -eq 'running'}
这不是最繁琐的命令输入,但即使是我也输错了并且必须更正。如果这是一项常见任务,为什么不创建一个易于使用的工具呢?
#requires -version 3.0
Function Get-MyService {
<#
.Synopsis
Get services by status.
.Description
This is a proxy version of Get-Service. The only real change is that you can specify services by their status. The default is to get all running services.
.Notes
Created: 9/11/2014
Learn more:
PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones6/)
PowerShell Deep Dives (http://manning.com/hicks/)
Learn PowerShell 3 in a Month of Lunches (http://manning.com/jones3/)
Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
.Example
PS C:\> Get-MyService b* -status stopped -comp chi-dc04
Status Name DisplayName
------ ---- -----------
Stopped BITS Background Intelligent Transfer Ser...
.Link
Get-Service
#>
[CmdletBinding(DefaultParameterSetName='Default', RemotingCapability='SupportedByCommand')]
param(
[Parameter(ParameterSetName='Default', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('ServiceName')]
[string[]]$Name,
[ValidateNotNullorEmpty()]
[System.ServiceProcess.ServiceControllerStatus]$Status="Running",
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Alias('Cn')]
[ValidateNotNullOrEmpty()]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Alias('DS')]
[switch]$DependentServices,
[Alias('SDO','ServicesDependedOn')]
[switch]$RequiredServices,
[Parameter(ParameterSetName='DisplayName', Mandatory=$true)]
[string[]]$DisplayName,
[ValidateNotNullOrEmpty()]
[string[]]$Include,
[ValidateNotNullOrEmpty()]
[string[]]$Exclude,
[Parameter(ParameterSetName='InputObject', ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[System.ServiceProcess.ServiceController[]]$InputObject
)
begin {
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
Write-Verbose "Getting services with a status of $status on $($Computername.toUpper())"
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
#remove Status parameter from bound parameters since Get-Service won't recognize it
$PSBoundParameters.Remove("Status") | Out-Null
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Get-Service', [System.Management.Automation.CommandTypes]::Cmdlet)
#$scriptCmd = {& $wrappedCmd @PSBoundParameters }
#my modified command
$scriptCmd = {& $wrappedCmd @PSBoundParameters | Where-Object {$_.status -eq $Status } }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
} #begin
process {
try {
$steppablePipeline.Process($_)
} catch {
throw
}
} #process
end {
try {
$steppablePipeline.End()
} catch {
throw
}
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end
} #end function Get-MyService
set-alias -Name gsv2 -Value Get-MyService
是的,我可以编写自己的函数来调用 Get-Service,但随后我必须编写所有代码来容纳 Get-Service 的所有参数。使用代理命令,我所需要做的就是插入我自己的代码。
#my modified command
$scriptCmd = {& $wrappedCmd @PSBoundParameters | Where-Object {$_.status -eq $Status } }
换句话说,我让 Get-Service 做它的事情,然后过滤结果。现在我可以轻松地做到这一点:
PS C:\Scripts> get-myservice w* -Status stopped -ComputerName chi-dc01
Status Name DisplayName
------ ---- -----------
Stopped wbengine Block Level Backup Engine Service
Stopped WbioSrvc Windows Biometric Service
Stopped WcsPlugInService Windows Color System
Stopped WdiServiceHost Diagnostic Service Host
Stopped WdiSystemHost Diagnostic System Host
Stopped Wecsvc Windows Event Collector
Stopped WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...
Stopped wmiApSrv WMI Performance Adapter
Stopped wudfsvc Windows Driver Foundation - User-mo...
或者,因为我正在输入此内容,所以我可以使用自己的别名。
我想指出的一件事是,对于新的 Status 属性,您会注意到我没有将其转换为字符串。
[System.ServiceProcess.ServiceControllerStatus]$Status="Running"
我本可以将其设为字符串,但通过使用实际枚举,PowerShell 将自动完成可能的值。
我怎么知道该用什么?我使用了获取会员。
PS C:\Scripts> get-service bits | get-member status
TypeName: System.ServiceProcess.ServiceController
Name MemberType Definition
---- ---------- ----------
Status Property System.ServiceProcess.ServiceControllerStatus Status {get;}
这实际上并不那么困难或耗时。我写这篇博文的时间比创建该函数的时间还多。
我希望这会激励您创建自己的 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 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[软件合集] 25年6月10日 精选软件30个
[软件合集] 25年6月9日 精选软件25个
[短剧] 2025年06月08日 精选+付费短剧推荐50部
[短剧] 2025年06月07日 精选+付费短剧推荐54部
[软件合集] 25年6月8日 精选软件33个
[软件合集] 25年6月7日 精选软件26个
[短剧] 2025年06月06日 精选+付费短剧推荐51部
[电影] 死神来了 1-6合集 4K HDR 杜比视界 外挂双语字幕
[软件合集] 25年6月6日 精选软件64个
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[短剧] 2025年06月07日 精选+付费短剧推荐54部
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[软件合集] 25年6月6日 精选软件64个
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[短剧] 2025年06月07日 精选+付费短剧推荐54部
[软件合集] 25年6月6日 精选软件64个
[剧集] [央视][笑傲江湖][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个
- 最新评论
-
- 热门tag