locked
Foreach Array issue RRS feed

  • Question

  • I'm trying to filter out everything in the $remove array and display the remaining installed programs! it works if i replace Foreach-object with Where-Object and no array to filter one object.

    Otherwise I just get a lot of false statements spit back at me.

    $remove = @("Visual C*","Microsoft*")
    $giveto = "\\path\here\UserFilesEdited.txt"
    $svc = Get-WmiObject -Class Win32_Service -Computer $compname `
             -Filter "Name='RemoteRegistry'" -ErrorAction SilentlyContinue `
    
    if (-not $svc) {
      "Cannot connect to $compname. Please use manual method."
      
    
    }
    
    if ($svc.State -eq 'Stopped') { $svc.StartService() }
    
    $remove | get-wmiobject -class Win32_Product -ComputerName $CompName | foreach-object {$_.name -notlike $remove} | Format-Wide -Property Name -Column 1
    
    
    <#
    get-wmiobject -class Win32_Product -ComputerName $CompName| gm | select-string -pattern $remove -notmatch | Out-File $giveto
    get-content $giveto
    #>
    
    

    • Edited by Animal Chubs Thursday, February 1, 2018 2:28 PM
    • Edited by jrv Thursday, February 1, 2018 2:55 PM Formatting
    • Moved by jrv Thursday, February 1, 2018 3:28 PM abandoned
    Thursday, February 1, 2018 2:19 PM

All replies

  • Do not post colorized text. Please post code correctly with code posting tool.  Your post is unreadable mostly.

    \_(ツ)_/

    Thursday, February 1, 2018 2:26 PM
  • I'd fix it but apparently this was marked as spam...
    Thursday, February 1, 2018 2:32 PM
  • I'd fix it but apparently this was marked as spam...

    It has not been marked.  Please fix the post.


    \_(ツ)_/

    Thursday, February 1, 2018 2:35 PM
  • Your post has been flagged as spam. If you believe that this was done in error, please post to the thread HERE.


    Thursday, February 1, 2018 2:51 PM
  • Does this help?

    $remove = 'one|two'
    echo one,two,three | where { $_ -notmatch $remove }
    
    three

    Or

    $remove = 'one','two'
    echo one,two,three | where { $_ -notin $remove }
    
    three


    • Edited by JS2010 Thursday, February 1, 2018 4:10 PM
    Thursday, February 1, 2018 2:52 PM
  • already going well beyond the point of caring. Just remove the post
    Thursday, February 1, 2018 2:53 PM
  • There are no flags on your post.  Perhaps you have an issue with your system or browser.


    \_(ツ)_/

    Thursday, February 1, 2018 2:55 PM
  • Your question does not make much sense.  What does "false statements spit back to me mean?"

    Please rethink you statement and code and try to be clear about your issue.

    "notlike" cannot be used with an array.  Use "notin".


    \_(ツ)_/


    • Edited by jrv Thursday, February 1, 2018 3:00 PM
    Thursday, February 1, 2018 2:59 PM
  • Your post has been flagged as spam. If you believe that this was done in error, please post to the thread HERE.



    This happens for new forums users that have not yet been verified. You can expedite verification by replying to this thread with your request.


    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Thursday, February 1, 2018 3:49 PM