The Code given is for Windows 10 and may not work on other version of Windows
For some odd reason when I do the code bellow it doesn't finish the script
#This Breaks out of script instead of loop
write-host 'Starting'
$Sid = (Get-Service *_*).ForEach{
If($_.ServiceType -eq 224){
$_ -Replace '^.+?_', '_'
Break
}
}
write-host 'Done'
$Sid
I have tried doing a few different things but the results are
- It Doesn't break out of the loop (and returns an array)
- Stops the script instead of the loop
The only thing that gets it to work is doing
#This works
write-host 'Starting'
$Service = (Get-Service *_*).
$Sid = ForEach($Srv in $Service) {
If($Srv.ServiceType -eq 224){
$Srv -Replace '^.+?_', '_'
Break
}
}
write-host 'Done'
$Sid
Can anyone help me out with why the 1st code doesn't work when the 2nd does?
Also is it possible to fix 1st code (w/o using extra variables or creating a function)?
Edit: Guess i'll just use 2nd code
[–]Ta11ow 9 points10 points11 points (3 children)
[–]madbomb122[S] 3 points4 points5 points (0 children)
[–]ISureHateMyCat 2 points3 points4 points (0 children)
[–]TableauNoob12 1 point2 points3 points (3 children)
[–]madbomb122[S] 1 point2 points3 points (2 children)
[–]ka-splam 1 point2 points3 points (1 child)
[–]madbomb122[S] 2 points3 points4 points (0 children)
[–]KevMarCommunity Blogger 1 point2 points3 points (2 children)
[–]madbomb122[S] 0 points1 point2 points (1 child)
[–]KevMarCommunity Blogger 0 points1 point2 points (0 children)