Powershell 查找默认网络打印机IP地址
我需要使用powershell脚本查找默认网络打印机的IP地址 我有以下到目前为止,它给我的打印机名称,但我需要的IP地址以及Powershell 查找默认网络打印机IP地址,powershell,Powershell,我需要使用powershell脚本查找默认网络打印机的IP地址 我有以下到目前为止,它给我的打印机名称,但我需要的IP地址以及 #Declare Global Variables Set-Variable -Name DefaultPrinter -Scope Global -Force cls If ((Test-Path $env:APPDATA"\DefaultPrinter.txt") -eq $true) { Remove-Item -Path $env:APPDAT
#Declare Global Variables
Set-Variable -Name DefaultPrinter -Scope Global -Force
cls
If ((Test-Path $env:APPDATA"\DefaultPrinter.txt") -eq $true) {
Remove-Item -Path $env:APPDATA"\DefaultPrinter.txt" -Force
}
$ComputerName = "localhost"
$DefaultPrinter = Get-WmiObject -Class win32_printer -ComputerName $ComputerName -Filter "Default='true'"
$DefaultPrinterPortIP = Get-WmiObject -Class Win32_TCPIPPrinterPort -ComputerName $ComputerName -Filter "name='$($DefaultPrinter.PortName)'" | Select-Object -ExpandProperty HostAddress
Write-Host "Default Printer: " -NoNewline
If ($DefaultPrinter.Name && $DefaultPrinterPortIP.HostAddress -ne $null) {
$DefaultPrinter.Name | Out-File -FilePath $env:APPDATA"\DefaultPrinter.txt" -Force -Encoding "ASCII"
Write-Host $DefaultPrinter.Name
} else {
$DefaultPrinter = "No Default Printer"
$DefaultPrinter | Out-File -FilePath $env:APPDATA"\DefaultPrinter.txt" -Force -Encoding "ASCII"
Write-Host $DefaultPrinter
#Cleanup Global Variables
Remove-Variable -Name DefaultPrinter -Scope Global -Force
它没有给我pritner的ip地址,并且一直不返回任何信息。这实际上是可行的,并返回一个ip地址,这是由于我的计算机受到限制,在网络上进行了测试,我们实际上想使用它,它工作得很好 感谢所有在此过程中做出贡献的人
谢谢,这是因为
Win32\u打印机
不包含IP地址信息:您知道如何获取IP地址信息吗?您知道打印机的主机名吗?如果是这样,您可以使用DNS通过使用[System.Net.DNS]::GetHostByName()
.Net方法来查找所需的信息