/bundles/itninjaweb/img/Breadcrumb_cap_w.png
Hi all. I have the powershell script below which deletes specific registry entry based on part of the GPO Name. It also runs against a specific set of the computers on our network, which we name along the lines of 'Site-RoomNumber-ComputerNumber'. At the minute the script below runs in sequence against all the computers i.e it won't move onto the second computer until it has deleted the entry and gpupdated the first, it won't move onto the third computer until it has deleted the entry and gpupdated the second etc. Would someone be able to inform me where to add in some code to launch the main code block in separate windows and to run in parallel? 

Thanks in advance

Clear-Host
$Search = read-host "Software to delete"
$SiteName = read-host "Site and Room"
$ComNum = read-host "Number of Computers"
For ($i=1; $i -le $ComNum; $i++) {
    if ($i -lt 10) {Invoke-Command -ComputerName $SiteName"-0"$i -ScriptBlock {
        $Keys = Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Group Policy\AppMgmt'
        $Items = $Keys | Foreach-Object {Get-ItemProperty $_.PsPath }
        ForEach ($Item in $Items) {
            if (($Item.'GPO Name') -match $Search) {
                Remove-Item $Item.PSPath
            }
        }
        Invoke-Command -ScriptBlock {C:\Windows\System32\gpupdate.exe /Force /Boot}
    }-ArgumentList $Search
}
}
    
    Else {Invoke-Command -ComputerName $SiteName-$i -ScriptBlock {
        $Keys = Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Group Policy\AppMgmt'
        $Items = $Keys | Foreach-Object {Get-ItemProperty $_.PsPath }
        ForEach ($Item in $Items) {
        if (($Item.'GPO Name') -match $Search) {
            Remove-Item $Item.PSPath
            }
        }
        Invoke-Command -ScriptBlock {C:\Windows\System32\gpupdate.exe /Force /Boot}
    }-ArgumentList $Search
}
Answer Summary:
Cancel
0 Comments   [ - ] Hide Comments

Comments

Please log in to comment

Answer Chosen by the Author


Answers

1
  • rileyz has the best answer. Credit to them.
Answered 10/20/2016 by: alphabeta
Fifth Degree Brown Belt

Please log in to comment
Answer this question or Comment on this question for clarity

Answers

1
Hahaha, I dont think you know what your asking, this is not easy!

What you're asking for a is script that triggers asynchronous jobs, not an easy script to write. BUT if you want to have a go, then start with Start-Job cmdlet, that will get on your way. 

Good luck!!
Answered 10/18/2016 by: rileyz
Red Belt

Please log in to comment
1
Never said it was going to be easy to answer. That's why I hope someone more knowledgeable than me (like yourself) could point me in the right direction. Thanks.
Answered 10/18/2016 by: alphabeta
Fifth Degree Brown Belt

  • You'll need to rewrite the whole function.

    I would use a variable to set the threads you want to run, that means you can scable it.

    You need to figure out a looping system to check for the each job until a result comes back.

    Actually coming to think about it now, it might not be to bad to do this.

    Have a look at this: https://gallery.technet.microsoft.com/scriptcenter/Multi-threading-Powershell-d2c0e2e5

    Load your hostnames into an array and feed that into the script.
Please log in to comment