powershell - 更改多个目录中 .ini 文件的值
问题描述
我需要更改多个用户“主”目录中 DefaultPrint.ini 文件中的值。
我设法制作了一个 powershell 脚本来更改单个用户的值,但我正在努力让脚本接受多个用户。
$User = "Max"
$IniPath = "\\Network1\Home\$User\"
foreach ($User in $User)
{
$IniFile = "$IniPath\DefaultPrint.ini"
$IniText = Get-Content -Path $IniFile
$NewText = $IniText.Replace("Old_Priter","New_Printer")
Set-Content -Path $IniFile -Value $NewText -Force
}
我需要输入以查找文件的目录是基于用户名的,所以我需要更改文件: \Network1\Home\Max
\Network1\Home\John
\Network1\Home\Sophia
等
上面的脚本适用于单个用户,我正在尝试通过 .csv 文件使其适应多个用户
我尝试了以下
$User = "Max","John","Sophia"
但它不分离用户目录而是将它们收集在一起?(\Network1\Home\Max John Sophia)
我还尝试通过 csv 导入,因为我需要对 200 多个用户执行此操作
$User = (Import-Csv -Path .\Users.csv).User
但它最终做同样的事情,我做错了什么?
解决方案
您需要移动此语句:
$IniPath = "\\Network1\Home\$User\"
在循环内部,以便$user
每次都用正确的路径更新路径,然后重命名$User
循环外使用的变量($Users
这里似乎是一个合适的名称):
$Users = -split "Max John Sophia"
foreach ($User in $Users)
{
$IniPath = "\\Network1\Home\$User\"
$IniFile = "$IniPath\DefaultPrint.ini"
$IniText = Get-Content -Path $IniFile
$NewText = $IniText.Replace("Old_Priter","New_Printer")
Set-Content -Path $IniFile -Value $NewText -Force
}
推荐阅读
- node.js - “MongoError:拓扑已关闭,请连接”错误
- php - PHP + curl:FTP TLS 如何在我的服务器和 FTP 之间匹配 TLS 版本
- javascript - Javascript单选按钮检查条件
- ruby-on-rails - Sidekiq 和 puma 共享同一个数据库连接池吗
- powershell - GUI中的Powershell Write-Progress命令在完成之前停止
- python - RuntimeError:当 `enforce_sorted` 为 True 时,`lengths` 数组必须按降序排序。- 火炬
- reactjs - React Apollo 客户端多个突变
- mongodb - 并发请求的MongoDB内存管理
- webpack - 当extractCss为真时,如何在head标签中获取特定css文件的内联样式?
- javascript - 加载所有图像后更新组件的状态