Deploy Network Printers from Ini File

Hi Guys,

I am new so I don’t know if I am in the right topic:

We connect our network-printers via script created in Ivanti DSM, this script uses a .ini file located on our netlogon. The file contains the printers to be connected per user in one line for example:

Username = \Server1\printer2;\server2\printer2

I have now changed this script to Powershell with a Function Get-Inifile (which I found in the WWW). The script looks like this:

function Get-IniFile
{
param(
[parameter(Mandatory = $true)] [string] $filePath
)
$anonymous = “NoSection”

$ini = @{}  
switch -regex -file $filePath  
{  
    "^\[(.+)\]$" # Section  
    {  
        $section = $matches[1]  
        <font color="#ffff00">-==- proudly presents  
        $CommentCount = 0  
    }  

    "^(;.*)$" # Comment  
    {  
        if (!($section))  
        {  
            $section = $anonymous  
            <font color="#ffff00">-==- proudly presents  
        }  
        $value = $matches[1]  
        $CommentCount = $CommentCount + 1  
        $name = "Comment" + $CommentCount  
        $ini[$section][$name] = $value  
    }   

    "(.+?)\s*=\s*(.*)" # Key  
    {  
        if (!($section))  
        {  
            $section = $anonymous  
            <font color="#ffff00">-==- proudly presents  
        }  
        $name,$value = $matches[1..2]  
        $ini[$section][$name] = $value  
    }  
}  
return $ini  

}

ForEach($printer in (@($iniFile.Printer.$env:USERNAME.Split(";")))){
Add-Printer -ConnectionName $printer
}

If I now save this script as Printermapping.ps1 and call it via Execute-ProcessAsUser the printers will not be connected?

Do you have a solution how to use this better without GPO?

Thank you for your help.

Translated with www.DeepL.com/Translator