[玩转系统] 周五乐趣:创建所有 PowerShell 配置文件脚本
作者:精品下载站 日期:2024-12-14 07:41:58 浏览:14 分类:玩电脑
周五乐趣:创建所有 PowerShell 配置文件脚本
配置文件路径是硬编码的。您可以通过查看内置的 $profile 变量来查看所有这些。
PS C:\> $profile | select *
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps
1
CurrentUserAllHosts : C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length : 74
您还可以仅查看 $profile,它将显示当前主机上当前用户的脚本。如果您在 ISE 中查看此内容,您会发现有些东西有所不同。
这个变量有点棘手。尽管您可以在此处看到属性,但如果将 $profile 通过管道传递给 Get-Member,您得到的只是一个字符串定义。这是您发现这些的另一种方法。
PS C:\> $profile.psobject.properties | format-table Name,Value -auto
Name Value
---- -----
AllUsersAllHosts C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length 74
虽然我不太关心长度,所以让我们过滤掉它。
PS C:\> $profile.psobject.properties | select -first 4 | format-table Name,Value -auto
Name Value
---- -----
AllUsersAllHosts C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
现在是有趣的部分。该值是一个路径,因此我可以使用 Test-Path 来查看它是否存在。如果没有,我可以创建配置文件并至少表明该配置文件未被使用。由于大多数文件默认情况下不存在,并且事实上 WindowsPowerShell 文件夹在您创建之前并不存在于 Documents 下,因此您可能必须首先创建父文件夹。所以我想出了一个名为 Create-AllProfiles.ps1 的小脚本。
#requires -version 3.0
[cmdletbinding(SupportsShouldProcess=$True)]
Param()
$profile.psobject.properties.value[0..3] | where { -Not (Test-path $_)} |
foreach {
#create the parent folder if it doesn't exist
$parent = Split-Path $_
if (-Not (Test-Path -path $parent)) {
mkdir $parent
}
#create the profile with this text
#the here string must be left justified
$txt=@"
#requires -version $($psversiontable.PSVersion)
<#
This profile is not used and intentionally left blank.
$env:userdomain$env:username
$(Get-Date)
#>
"@
#write the text to the profile script
Write-Host "Creating $_" -ForegroundColor Green
$txt | Out-File -FilePath $_
}
该脚本利用了 PowerShell 3 中自动枚举属性的功能。在 PowerShell 2.0 中我需要这样做:
PS C:\> $profile.psobject.properties | select -ExpandProperty Value -first 4
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
但现在在 v3 及更高版本中我可以这样做:
PS C:\> $profile.psobject.properties.value[0..3]
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
value 属性将显示所有值,包括长度,因此因为我正在获取一个数组,所以我可以使用一系列索引号来选择我想要的值。在我的脚本中,每个值都会被测试,那些不存在的值将被传递给 Foreach-Object。
对于每个路径,我首先将其拆分,以便测试父路径是否存在。如果没有,则创建它。然后我创建一些文本,将插入到每个新的配置文件脚本中。该脚本支持 -WhatIf,因此您可以在实际运行它之前了解它会做什么。
我最终得到一个如下所示的配置文件脚本:
理想情况下,我建议您对配置文件脚本进行数字签名(假设您使用的是 AllSigned 执行策略),以便在您不知情的情况下无法对这些脚本进行任何更改。只是不要忘记也在 PowerShell ISE 中运行它。如果您已经创建了配置文件,它们将被忽略。如果您稍后决定利用任何配置文件脚本,它们已准备好供您编辑。
学习、享受并度过一个愉快的周末。
猜你还喜欢
- 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