topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 5:42 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: PowerShell Pickle with Registry Updates  (Read 5958 times)

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,646
    • View Profile
    • Donate to Member
PowerShell Pickle with Registry Updates
« on: September 15, 2022, 03:35 PM »
Greetings,
    So I'm in a bit of a pickle trying to update the registry with PowerShell. I need to go into a key, list it's sub keys, and update a value in all of the subkeys on local machine. Reason this is being done with PowerShell, is it has to be pushed out to 80 machines...so nobody wants to do it manually 80 times  :D

   The script (scrounged off the internet) I've been beating on all afternoon is below, it will open the key and list the values I'm after … But I cannot get it to update them. Note: the updated value is/will be based on the current value. So I'm looking for a get/replace/set type of operation on the HostName entry's value which is an IP address.

Code: PowerShell [Select]
  1. $registry = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports" -Recurse
  2.  
  3. foreach ($a in $registry) {
  4.     ($a | Get-ItemProperty).Psobject.Properties |
  5.     #Exclude powershell-properties in the object
  6.     Where-Object { $_.Name -like 'HostName' } |
  7.     Select-Object Name, Value
  8. }

So instead of displaying the HostName entry's value, I need it to change the third octet of the IP address to a 4 so that 192.168.1.152 becomes 192.168.4.152

Anybody smarter than I am got a bit of mercy handy for a tired old fool?

TIA
-Stoic Joker

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,646
    • View Profile
    • Donate to Member
Re: PowerShell Pickle with Registry Updates
« Reply #1 on: September 16, 2022, 09:28 AM »
So... Crickets it is then..

Well for the next poor sod what ends up wondering, I did manage to cobble together an answer:
Code: PowerShell [Select]
  1. $registry = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports" -Recurse
  2.  
  3. foreach($a in $registry) {
  4.  
  5.         $X = $a.ToString()
  6.         $X = $X.Replace("HKEY_LOCAL_MACHINE", "HKLM:")
  7.         Get-ItemProperty -Path "$X" HostName | %{Set-ItemProperty -Path $_.PSPath HostName -Value ( $_.HostName -Replace ".1.", ".4." )}
  8. }
  9.  
  10. Restart-Service -Name Spooler


Object being: we're having to readdress a large network that is running out of addresses to an entirely different - non overlapping - subnet. So when all of the machines (DHCP) jump over to the new address range, nobody will be able to access their printers because they all print to direct IP. 80 computers, pointing at 60+ different printers, in what is basically a mesh pattern.

Nobody want's to do that shit through the GUI by hand.

So this (pushed to each machine via management software) will update the Printer Port IP Address targets on each machine to the new address range … Without requiring anyone to romp all over the complex touching machines.

superboyac

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 6,347
    • View Profile
    • Donate to Member
Re: PowerShell Pickle with Registry Updates
« Reply #2 on: September 16, 2022, 02:09 PM »
LOL loving the attitude.  This is how I am normally LOLLL.