Reputation: 47
I have a powershell script which I run with a batch file (with elevated rights) to install a network printer. I have configured 37 scripts and most of the code in those scripts is repetitive, so I am trying to make one universal script which will install the printer by input room number, by including a txt file which will hold all the printer information.
This is the batch file:
@echo off
set /p room_number=What is the room number:
if exist "...\%room_number%.ps1" (
Powershell.exe -executionpolicy remotesigned -File "...\%room_number%.ps1" "%room_number%"
)
else (
echo.
echo Powershell script for the printer in the room you specified has not been configured yet. You will need to add the printer maunally!
explorer shell:::{A8A91A66-3A7D-4424-8D24-04E180695C7A}
pause
)
This is the new txt file I have been working on:
roomnumber=1001
prdrloc=hp-lj-m401dn
prdrname=HP LaserJet 400 M401 PCL 6
hostname=TEST1
IP=192.168.7.10
devname=HP LaserJet Pro 400 M401dn-ROOM
roomnumber=1002
prdrloc=hp-lj-m404dn
prdrname=HP LaserJet Pro M404-M405 PCL-6 (V4)
hostname=TEST2
IP=192.168.7.11
devname=HP LaserJet Pro M404dn-ROOM
This is the powershell script in which I am trying to include the txt file, but I do not know how to get only the information concerning the room I specified.
# Room_number_1001
param($room_number)
$file = "...\printer_info.txt"
foreach($line in (Get-Content $file)) {
$a = $line.Split("=")
New-Variable -Name $a[0] -Value $a[1]
}
Get-ChildItem "...\$prdrloc" -Filter *.inf -Recurse | % {pnputil.exe /a $_.FullName}
Add-PrinterDriver -Name "$prdrname" -Verbose
Add-PrinterPort -Name "$hostname" -PrinterHostAddress "$IP" -Verbose
Add-Printer -PortName "$hostname" -Name "$devname" -DriverName "$prdrname"
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" -Name "LegacyDefaultPrinterMode" -Value 1 -Force
$PrinterName="$devname"
$DefaultPrinter = Get-WmiObject Win32_Printer -Filter "Name='$PrinterName'"
$DefaultPrinter.SetDefaultPrinter()
Any help would be much appreciated. Thanks guys. :)
Upvotes: 0
Views: 155
Reputation: 47
Scepticalist, thank you for your answer. It did not quite solve my problem but you sent me searching for the solution in the right direction. I implemented the csv suggestion. :)
I will post my new batch file, csv and powershell script which have given me the result I needed.
Batch:
@echo off
setlocal enabledelayedexpansion
set /p room_number=What is the room number:
set /A counter=0
for /f "usebackq tokens=1-6 delims=;" %%a in ("...\printerinfo.csv") do (
if "%%a" == "%room_number%" (
set /A counter=1
Powershell.exe -executionpolicy remotesigned -File "...\printerscript.ps1" "%room_number%"
)
)
if "%counter%"=="0" (
echo.
echo Powershell script for the printer in the room you specified has not been configured yet. You will need to add the printer maunally!
explorer shell:::{A8A91A66-3A7D-4424-8D24-04E180695C7A}
pause
)
CSV file:
room_nmb;prdr_loc;prdr_name;host_name;ip_address;dev_name
1001;hp-lj-m401dn;HP LaserJet 400 M401 PCL 6;TEST1;192.168.7.10;HP LaserJet Pro 400 M401dn-ROOM
1002;hp-lj-m404dn;HP LaserJet Pro M404-M405 PCL-6 (V4);TEST2;192.168.7.11;HP LaserJet Pro M404dn-ROOM
Powershell script:
param($room_number)
$roomnmb = @()
$prdrloc = @()
$prdrname = @()
$hostname = @()
$ip = @()
$devname = @()
Import-csv "...\printerinfo.csv" -delimiter ";" |
ForEach-Object {
$roomnmb = $_.room_nmb
if ($roomnmb -contains $room_number) {
$prdrloc = $_.prdr_loc
$prdrname = $_.prdr_name
$hostname = $_.host_name
$ip = $_.ip_address
$devname = $_.dev_name
Get-ChildItem "...\$prdrloc" -Filter *.inf -Recurse | % {pnputil.exe /a $_.FullName}
Add-PrinterDriver -Name "$prdrname" -Verbose
Add-PrinterPort -Name "$hostname" -PrinterHostAddress "$ip" -Verbose
Add-Printer -PortName "$hostname" -Name "$devname" -DriverName "$prdrname"
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" -Name "LegacyDefaultPrinterMode" -Value 1 -Force
$DefaultPrinter = Get-WmiObject Win32_Printer -Filter "Name='$devname'"
$DefaultPrinter.SetDefaultPrinter()
}
}
Cheers mate.
Upvotes: 0
Reputation: 3923
Firstly, I'd put your data into a csv. It'll be easier to maintain AND easier to work with in Powershell
$PrinterData = Import-Csv -Path '\path to printer config csv file'
$RoomRequired = '1001'
$PrinterData | Where-Object {$_.RoomNumber -in $RoomRequired} | ForEach-Object {
Get-ChildItem "...\$_.prdrloc" -Filter *.inf -Recurse | % {pnputil.exe /a $_.FullName}
Add-PrinterDriver -Name $_.prdrname -Verbose
Add-PrinterPort -Name $_.hostname -PrinterHostAddress $_.IP -Verbose
Add-Printer -PortName $_.hostname -Name $_.devname -DriverName $_.prdrname
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" -Name "LegacyDefaultPrinterMode" -Value 1 -Force
$PrinterName= $_.devname
$DefaultPrinter = Get-WmiObject Win32_Printer -Filter "Name='$Print
}
You can make $RoomRequired
an array of room numbers e.g. $RoomRequired = @("1001","1002")
if you want to do multiple rooms.
Upvotes: 1