/build/static/layout/Breadcrumb_cap_w.png

How do I call new Powershell window from within Powershell

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
}

0 Comments   [ + ] Show comments

Answers (3)

Answer Summary:
Posted by: rileyz 7 years ago
Red Belt
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!!
Posted by: alphabeta 7 years ago
Black Belt
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.

Comments:
  • 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. - rileyz 7 years ago
Posted by: alphabeta 7 years ago
Black Belt
1

Top Answer

  • rileyz has the best answer. Credit to them.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ