I posted something similar to this last week, but this is evolved into something a little more complicated. This should be a two-fold operation. First, on the PrintServer, then, deployed across 100-200 machines. I am working on a PSise script (pretty much my first time) that should be able to disseminate and group all the subnet addresses by 3rd octet. Then it should then it should determine if the attached printers and their ports meet the new schema requirement that the 4th octet be within the range of 50-60. If not, determine the next available address within the proper subnet and range, and reassign the ports to the next one available. I will need to track the changes of each subnet so the user machines will line up with the changes made to the print server. So if the PrintServer has 192.168.1.23 >> 192.168.2.50 && 192.168.3.105 >> 192.168.3.52, this same change needs to happen for all machines in subnets 1 and 3 respectively.
I know this function probably is not setup correctly, so if I have things out of place, feel free to let me know. I get the feeling all the code that should be performing the changes should go in the -end of the processing -begin, -process, -end segment.
Here's the code I have so far, and I am getting some values that I would expect. I'm just not getting them into proper arrays and piping them down for further use. I have a feeling I may need a -join somewhere, but like I said, I do not have any experience with Powershell.
function verify-printerport
{
[CmdletBinding()]
[OutputType([int])]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$third = @{},
$fourth = @{},
$thirds = @{},
$fourths = @{},
# Param2 Gets WMI Object -Class -ExtendedProperty -Sorts
$set1 = ( Get-WmiObject -Class Win32_TCPIPPrinterPort | select -ExpandProperty HostAddress | sort )
)
Begin
{
$set1
$thirds = @{0=100, 202, 203, 204, 209, 211, 212, 213, 214, 215, 216, 217, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230}
$fourths = @{49=50..60}
}
Process
{
foreach ($ips in $set1)
{
Where
{
(($ips.HostAddress -Split '\.')[2] -contains $thirds)
} |
ForEach ($getThird)
{
$third.Add($getThird.IndexOf($ips.HostAddress),$getThird.ToArray)
where
{
$getThird.ForEach($ips.HostAddress[2] -eq $thirds)
}
}
}
foreach ($ips2 in $set1) {
where
{
(($ips2.HostAddress -split '\.')[3] -notcontains $fourths)
} |
foreach ($getFourth)
{
$fourth.Add($getFourth,$inRange.Length)
if ( $getFourth.foreach($set1.HostAddress -split '\.'[3] -lt 50) -or
$getFourth.foreach($set1.HostAddress -split '\.'[3] -gt 60))
{
where
{
($set1.ForEach($inRange.HostAddress -split '\.')[3] -contains $fourths)
}
set $inRange.StartsWith($set1.Length)
$inRange.Length ++
}
foreach ($inRange in $getFourth)
{
if ($inRange.ForEach($setRange.HostAddress -split '\.'[3] -contains $fourths))
{
$inRange.GetLength($setRange.GetValue($setRange.Length))
}
}
$inRange += $set1.Length
}
}
}
End
{
}
}
$thirds and $fourths referring to 3rd/4th octet. $thirds is all the subnets we have. $fourths is the range all printers should be.
Any help you can offer would be greatly appreciated. It's taken me several days and long hours just to get here and I'm about at wit's end. Some of this is derived from searches, other from my other post, some from me trying to apply some of my other programming experience (fyi, I'm still in school) and a lot of it just plain guess work. Thanks in advance for taking the time!