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:
Please log in to answer
Posted by:
rileyz
8 years ago
Posted by:
alphabeta
8 years ago
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 8 years ago