Created
January 13, 2012 14:58
-
-
Save drlongnecker/1606790 to your computer and use it in GitHub Desktop.
Setting the default printer based on network subnet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$homeRange = "10.1.1.*" | |
$remoteRange = "10.7.104.*" | |
$remotePrinter = "W3Printer_01" | |
$homePrinter = "AdminPrinter" | |
function Set-DefaultPrinter([string]$printerPath) { | |
$printers = gwmi -class Win32_Printer -computer . | |
Write-Host -fore cyan "Default Printer: $printerPath" | |
$dp = $printers | ? { $_.deviceID -match $printerPath } | |
$dp.SetDefaultPrinter() | Out-Null | |
} | |
$ipAddress = @() | |
$ipAddress = gwmi win32_NetworkAdapterConfiguration | | |
? { $_.IPEnabled -eq $true } | | |
% { $_.IPAddress } | | |
% { [IPAddress]$_ } | | |
? { $_.AddressFamily -eq 'internetwork' } | | |
% { $_.IPAddressToString } | |
Write-Host -fore cyan "Your current network is $ipAddress." | |
switch -wildcard ($ipAddress) { | |
$homeRange { Set-DefaultPrinter $homePrinter } | |
$remoteRange { Set-DefaultPrinter $remotePrinter } | |
default { Set-DefaultPrinter $homePrinter } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment