首页 > 解决方案 > 更改多个目录中 .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

但它最终做同样的事情,我做错了什么?

标签: powershell

解决方案


您需要移动此语句:

$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
}

推荐阅读