forked from PackeTsar/Win-Printer-IP-Change
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWin-Printer-IP-Change.ps1
99 lines (71 loc) · 2.49 KB
/
Win-Printer-IP-Change.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
param(
[string]$old_ip = '',
[string]$new_ip = ''
)
#$old_ip=$args[0]
#$new_ip=$args[1]
if (!$old_ip -Or !$new_ip) {
echo `n;
$fromps = $FALSE
$old_ip = Read-Host 'Enter the Old Printer IP';
$new_ip = Read-Host 'Enter the New Printer IP';
echo `n;
} Else {
$fromps = $TRUE
}
echo "Going from Old IP ($old_ip) to New IP ($new_ip)"
echo "Getting Printers and Ports"
$ports = Get-PrinterPort
$printers = Get-Printer
foreach ($port in $ports ){
If ($port.PrinterHostAddress -eq $old_ip) {
$pname = $port.Name;
echo "Found Port: $port.Name"
}
}
foreach ($printer in $printers ){
If ($printer.PortName -eq $pname) {
$myprintername = $printer.Name;
echo "Found Printer: $printer.Name"
break
}
}
if (!$pname) {
echo `n;
Write-Host "Port Not Found. Exiting." -foreground Red;
echo `n;
if (!$fromps) {[void](Read-Host 'Press Enter to close window');Exit} Else {Exit}
}
if (!$myprintername) {
echo `n;
Write-Host "Port found but no printer using it" -foreground Yellow;
echo `n;
[void](Read-Host 'Press Enter to delete the unused port...');
echo "Deleting printer port $pname";
Remove-PrinterPort -Name $pname;
echo `n;
Write-Host "Port removed" -foreground Green;
echo `n;
if (!$fromps) {[void](Read-Host 'Press Enter to close window');Exit} Else {Exit}
}
$newport = "PORT_$new_ip"
echo `n
Write-Host "Upon confirmation, we will be:" -foreground Green
Write-Host " - Adding a new Printer Port ($newport) with IP ($new_ip)" -foreground Green
Write-Host " - Applying new Printer Port ($newport) to Printer ($myprintername)" -foreground Green
Write-Host " - Removing old Printer Port ($pname)" -foreground Green
echo `n
#[void](Read-Host 'Press Enter to make IP switch...')
if (!$fromps) {[void](Read-Host 'Press Enter to make IP switch...')}
Add-PrinterPort -Name $newport -PrinterHostAddress $new_ip
Set-Printer -name $myprintername -PortName $newport
Remove-PrinterPort -Name $pname
echo `n
Write-Host "Modified $myprintername" -foreground Green
echo `n
#[void](Read-Host 'Press Enter to print test page...')
# Invoke-CimMethod -MethodName printtestpage -InputObject (
# Get-CimInstance win32_printer -Filter "name LIKE '$myprintername'")
#[void](Read-Host 'Press Enter to check print jobs...')
#Get-PrintJob -PrinterName $myprintername | Format-Table | Out-String|% {Write-Host -foreground Green $_}
if (!$fromps) {[void](Read-Host 'Press Enter to close window');Exit} Else {Exit}