-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-RemoteWSDPortIP.ps1
169 lines (106 loc) · 4.35 KB
/
Get-RemoteWSDPortIP.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
Function Search-RemoteRegistryHive {
<#
.SYNOPSIS
Search Registry path for specific keyword
.DESCRIPTION
Search-RegistryHive will search the given path for specifc keywords and output the full path to Registry keys
that contain that keyword
.EXAMPLE
Search-RegistryHive -path 'HKLM:\SYSTEM\ControlSet001\Control\Print\Printers' -searchText $Port
Example searching the registry for printers that match the $port search text criteria
.NOTES
Works on PowerShell 3.0 and higher
#>
[CmdletBinding()]
Param (
[Parameter( Mandatory = $true,
HelpMessage = 'Keyword to search Registry for')]
[string]$searchText,
[Parameter()]
[String]$path,
[Parameter()]
[String]$ComputerName
)
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
Get-ChildItem -Path $Using:path -Recurse | ForEach-Object {
if ((Get-itemproperty -Path $_.pspath) -match $Using:searchText) {
Write-Output -InputObject $_.Name
}
}
}
}
Function Get-RemoteWSDPortIP {
<#
.SYNOPSIS
Describe purpose of "Get-RemoteWSDPortIP" in 1-2 sentences.
.DESCRIPTION
Add a more complete description of what the function does.
.PARAMETER ComputerName
Describe parameter -ComputerName.
.EXAMPLE
Get-RemoteWSDPortIP -ComputerName 'SERVER01'
Fetches any instance of a pritner with a WSD named port from SERVER01 and resolves the IP Address(es)
.NOTES
Place additional notes here.
.LINK
URLs to related sites
The first link is opened by Get-Help -Online Get-RemoteWSDPortIP
.INPUTS
List of input types that are accepted by this function.
.OUTPUTS
List of output types produced by this function.
#>
[CmdletBinding()]
Param (
[String[]]$ComputerName
)
Begin {}
Process {
Foreach ($Computer in $ComputerName) {
Try {
$Printer = Get-Printer -ComputerName $Computer | Where-Object {$_.PortName -like 'WSD*'}
foreach ($port in $Printer.PortName) {
$Params = @{
'Path' = 'HKLM:\SYSTEM\ControlSet001\Control\Print\Printers'
'SearchText' = $Port
'ComputerName' = $Computer
}
$Subkeys = (Search-RemoteRegistryHive @Params) -replace '^[^\\]*', 'HKLM:'
$Subkeys | ForEach-Object {
if ($_ -like '*PrinterDriverData') {
$Splat = @{
'ScriptBlock' = {Get-ItemProperty -Path $Using:_}
'ComputerName' = $Computer
}
$KeyProps = Invoke-Command @Splat
$props = [Ordered]@{
'ComputerName' = $Computer
'Printer' = ($Printer | Where-Object {$_.PortName -eq $Port}).Name
'WSDPort' = $Port
'IPAddress' = ($KeyProps.HPEWSIPAddress).Split(',')[0]
}
$Object = New-Object -TypeName PSObject -Property $props
$object.PSObject.typenames.insert(0,'WSDPort.IPAddress')
Write-Output -InputObject $Object
}
}
}
} Catch {
# get error record
[Management.Automation.ErrorRecord]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]@{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# output information. Post-process collected info, and log info (optional)
Write-Output -InputObject $info
}
}
}
End {}
}