powershell - 编辑注册表时使用 Push-Location/Pop-Location 有用吗?
问题描述
这是我自己的带有 Push/Pop 行的脚本。从这里得到 PushPop 的想法。他们被评论了,因为他们显然什么都不做……在功能上很糟糕。
# DisableCortana.ps1
$newKeyLocation="HKLM:\SOFTWARE\Policies\Microsoft\Windows"
$newKeyName="Windows Search"
#Push-Location # "Adds that location at the top of the stack" - for what?
# I guess if your location is changed mid-thread execution, this could make sense, but how could that be allowed in the first place?
Set-Location $newKeyLocation
if(Test-Path "$newKeyLocation\$newKeyname"){
echo "Path exists, aborting"
}else{
New-Item -Path "$newKeyLocation" -Name "$newKeyName"
$newKey="HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search"
$newValueName="AllowCortana"
$allowCortanaValue="0"
New-ItemProperty -Path $newKey -Name $newValueName -Value $allowCortanaValue `
-PropertyType DWORD -Force | Out-Null
#Pop-Location
}
解决方案
在您的情况下Push-Location
,Pop-Location
这里是为了保证您的脚本不会修改当前位置。
在一个新的 PowerShell 会话 Test 中:
Get-Location
$newKeyLocation="HKLM:\SOFTWARE\Policies\Microsoft\Windows"
$a = Set-Location $newKeyLocation
Get-Location
看看 prop 发生了变化,你不再是在默认的文件系统提供程序中,而是在注册表提供程序中。
现在进行一个新的 PowerShell 会话和测试:
Get-Location
Push-Location
$newKeyLocation="HKLM:\SOFTWARE\Policies\Microsoft\Windows"
$a = Set-Location $newKeyLocation
Pop-Location
Get-Location
提示停留在文件系统中
我认为您可以查看About Providers 文档。该位置在提供程序的顶部运行set-location
,Push-Location
并且Pop-Location
在全球范围内使用不同的 PSDrive,它们不仅是文件系统,而且可以是注册表、别名(请参阅 get-psprovider、get-psdrive)
推荐阅读
- python - 绘制 SVC 决策区域
- apache-flink - 是否可以将 apache ranger/atlas 与 apache flink 集成?
- python - Scrapy:在爬取之前检查 MongoDB 是否有重复项
- spring-boot - 使用与 Spring Boot 存储库相同的存储库方法返回不同的 Dto
- apache-flink - Apache Flink 检查点不断失败
- box - FTPS MoveIt 无法下载没有扩展名的文件 - 不是普通文件 -
- chart.js - 由于标签过于紧凑,如何修复 X 轴中的网格线数量
- python - 我需要帮助在 python 中导航 api
- python - Matplotlib:更改日期时间刻度会使绘图消失
- go - 作为 systemd 服务运行的程序拒绝连接