[玩转系统] PowerShell 控制器脚本
作者:精品下载站 日期:2024-12-14 07:56:07 浏览:15 分类:玩电脑
PowerShell 控制器脚本
当谈到 PowerShell 脚本时,我们往往非常关注函数和模块。我们强调构建可重复使用的工具。我们的想法是,我们可以在 PowerShell 提示符下使用这些工具来完成给定的任务。这些任务很可能是重复的。在这些情况下,将通常在 PowerShell 提示符下以交互方式键入的命令放入 PowerShell 控制器脚本中是有意义的。该脚本依次使用您构建的函数和模块。我一直在构建一套用于备份重要文件夹的 PowerShell 工具。我还创建了一些管理控制器脚本,想与您分享。
设计PowerShell脚本
PowerShell 脚本的基本思想是它是一个“固定的”PowerShell 会话。您应该能够获取 ps1 文件的内容,将其粘贴到 PowerShell 会话中,然后它应该运行。该脚本不是以交互方式键入命令,而是为您执行此操作。但是,PowerShell 脚本还可以采用参数、使用 cmdlet 绑定并利用 If/Else 语句等脚本结构。 PowerShell 脚本不必遵循我们在构建函数时强调的相同规则。该脚本可以轻松运行一系列控制功能的命令或 cmdlet。这是一个例子。
#requires -version 5.1
[cmdletbinding()]
Param(
[parameter(position = 0)]
[ValidateNotNullOrEmpty()]
[string]$Backup = "*",
[Parameter(HelpMessage = "Show raw objects, not a formatted report.")]
[switch]$Raw
)
$f = Get-ChildItem -path "D:\backup$backup-log.csv" | ForEach-Object {
$p = $_.fullname
Import-Csv $_.fullname | Add-Member -MemberType NoteProperty -Name Log -value $p -PassThru
} | Where-Object { ([int32]$_.Size -gt 0) -AND ($_.IsFolder -eq 'False') }
$files = ($f.name | Select-Object -Unique).Foreach({ $n = $_; $f.where({ $_.name -eq $n }) |
Sort-Object -Property { $_.date -as [datetime] } | Select-Object -last 1 })
if ($raw) {
$files
}
else {
$grouped = $files | Group-Object Log
#display formatted results
$files | Sort-Object -Property Log, Directory, Name | Format-Table -GroupBy log -Property Date, Name, Size, Directory
$summary = foreach ($item in $grouped) {
[PSCustomObject]@{
Backup = (Get-Item $item.Name).basename.split("-")[0]
Files = $item.Count
Size = ($item.group | Measure-Object -Property size -sum).sum
}
} #foreach item
$total = [PSCustomObject]@{
TotalFiles = ($grouped | Measure-Object -property count -sum).sum
TotalSizeMB = [math]::round(($summary.size | Measure-Object -sum).sum/1MB,4)
}
Write-Host "Incremental Pending Backup Summary" -ForegroundColor cyan
($summary | Format-Table | Out-String).TrimEnd() | Write-Host -ForegroundColor cyan
$total | Format-Table | Out-String | Write-Host -ForegroundColor cyan
}
这个脚本 MyBackupPending.ps1 旨在向我展示哪些文件将作为我的日常增量备份作业的一部分进行备份。该脚本文件会遍历我的备份 csv 文件列表,并默认显示格式化报告。
通常,您不会在 PowerShell 函数中使用 Format-Table 或 Write-Host。但这个控制脚本并不是为了将对象写入管道而设计的。它旨在为我提供格式化报告。然而,这并不意味着我不能两全其美。
脚本可以有参数。我的包括指示备份集的参数或显示原始、未格式化数据的选项。
您可以在脚本中使用与在函数中使用的脚本技术相同的脚本技术。这是我的控制脚本,它为我提供了有关备份存档的报告。
# requires -version 5.1
[cmdletbinding()]
Param(
[Parameter(Position = 0, HelpMessage = "Enter the path with extension of the backup files.")]
[ValidateNotNullOrEmpty()]
[ValidateScript( { Test-Path $_ })]
[string]$Path = "\ds416\backup",
[Parameter(HelpMessage = "Get backup files only with no formatted output.")]
[Switch]$Raw
)
<#
A regular expression pattern to match on backup file name with named captures
to be used in adding some custom properties. My backup names are like:
20191101_Scripts-FULL.rar
20191107_Scripts-INCREMENTAL.rar
#>
[regex]$rx = "^20\d{6}_(?<set>\w+)-(?<type>\w+)\.rar$"
<#
I am doing so 'pre-filtering' on the file extension and then using the regular
expression filter to fine tune the results
#>
$files = Get-ChildItem -path $Path -filter *.rar | Where-Object { $rx.IsMatch($_.name) }
#add some custom properties to be used with formatted results based on named captures
foreach ($item in $files) {
$setpath = $rx.matches($item.name).groups[1].value
$settype = $rx.matches($item.name).groups[2].value
$item | Add-Member -MemberType NoteProperty -Name SetPath -Value $setpath
$item | Add-Member -MemberType NoteProperty -Name SetType -Value $setType
}
if ($raw) {
$Files
}
else {
$files | Sort-Object SetPath, SetType, LastWriteTime | Format-Table -GroupBy SetPath -Property LastWriteTime, Length, Name
$grouped = $files | Group-Object SetPath
$summary = foreach ($item in $grouped) {
[pscustomobject]@{
BackupSet = $item.name
Files = $item.Count
Size = ($item.group | Measure-Object -Property size -sum).sum
}
}
$total = [PSCustomObject]@{
TotalFiles = ($grouped | Measure-Object -property count -sum).sum
TotalSizeMB = [math]::round(($summary.size | Measure-Object -sum).sum/1MB,4)
}
Write-Host "Backup Summary" -ForegroundColor cyan
($summary | sort-object Size -Descending| Format-Table | Out-String).TrimEnd() | Write-Host -ForegroundColor cyan
$total | Format-Table | Out-String | Write-Host -ForegroundColor cyan
}
您会注意到参数具有属性和验证。但现在我有一个非常简单的命令可以运行,它显示我的备份文件的格式化和排序,包括摘要。
从任何地方运行 PowerShell 脚本
PowerShell 控制脚本的一个缺点是您必须指定 ps1 文件的完整路径。如果您的脚本位于 C:\Scripts 这样的简单路径中,那么也许不需要输入太多内容。但我一直在寻找“创造性懒惰”的方法。这是您可能喜欢的一种方法。
PowerShellGet 可以轻松地从 PowerShell 库中查找和安装模块,它还具有一组用于查找和安装 PowerShell 脚本的相关命令。您可以从 PowerShell 库安装 PowerShell 脚本文件并从任何位置运行 .ps1 文件。您不需要指定路径。我要那个!
我将采取简单的方法并使用 PowerShell 已经知道的位置:$env:ProgramFiles\WindowsPowerShell\Scripts。如果您将 ps1 文件复制到此文件夹,则只需输入脚本名称即可运行它。太棒了。几乎。
我喜欢将所有脚本工作保存在 C:\Scripts 中,并且我知道我需要调整我的备份控制脚本一段时间。我不想编辑文件并保持已安装脚本中的版本同步。所以我把它们联系起来。我利用符号链接在“已安装脚本”中创建链接副本。现在,我可以仅按名称运行控制器脚本,所需要做的就是编辑 C:\Scripts 下的文件。
为了简化过程,我自然创建了一个控制器脚本。
#requires -version 5.1
#create linked script copies in Installed Scripts
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(Position = 0, Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateScript({Test-Path $_})]
[String[]]$Path
)
Begin {
$installPath = "$env:ProgramFiles\WindowsPowerShell\Scripts"
Write-Verbose "Creating links to $installpath"
}
Process {
if (Test-Path -Path $installPath) {
foreach ($file in $Path) {
Try {
$cfile = Convert-Path $File -ErrorAction Stop
}
Catch {
Throw "Failed to find or convert $file"
}
if ($cfile) {
$name = Split-Path -Path $cfile -Leaf
$target = Join-Path -path $installPath -ChildPath $name
Write-Verbose "Creating a link from $cfile to $target. A 0 byte file is normal."
#overwrite the target if it already exists
New-Item -Path $target -value $cfile -ItemType SymbolicLink -Force
}
} #foreach file
}
else {
Write-Warning "Can't find $installPath. This script might require a Windows platform."
}
}
End {
Write-Verbose "Finished linking script files."
}
# Get-command -CommandType ExternalScript
我运行一个脚本文件来显示运行 Hyper-V 虚拟机的自定义视图,C:\Scripts\HyperVStatus.ps1。我将创建一个指向已安装脚本的链接。
正如您所看到的,这会创建一个返回目标脚本的 0 字节链接。现在我可以从任何地方运行这个脚本文件。
如果我更新脚本,我无需对链接执行任何操作。事实上,我可以更进一步定义一个别名。
我创建了备份控制脚本的链接。使用 Get-Command 发现外部脚本。
不要害怕创建利用 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