none
My powershell script not working as I would expect RRS feed

  • Question

  • Hi guys

    I put this script together and it seems to be working. The only problem I have is that it gives the information out 3 times. Any help would be appreciated. Script below:

    $a = get-content "C:\UpdateCheck\Machinelist.txt"

    foreach ($i in $a)
    {
    $a|foreach-Object {Get-WmiObject win32_service -computername $i }|
    where {$_.state -eq "stopped" -and $_.startmode -eq "auto"}|
    Tee-object -filepath C:\updatecheck\$i.txt
    }

    Regards

    Scott

    Monday, August 19, 2013 1:27 PM

Answers

  • Try this slight adjustment:

    $a = get-content 'C:\UpdateCheck\Machinelist.txt'
    
    foreach ($i in $a) {
    
        Get-WmiObject win32_service -computername $i |
        Where {$_.state -eq "stopped" -and $_.startmode -eq "auto"} |
        Tee-object -filepath "C:\updatecheck\$i.txt"
    
    }

    One thing to watch out for - Tee-Object writes files with Unicode encoding. You may want to just use Out-File, since you're using it at the end of the pipline anyway.


    Don't retire TechNet!

    • Proposed as answer by jrv Monday, August 19, 2013 3:08 PM
    • Marked as answer by IamMred Sunday, October 13, 2013 2:53 AM
    Monday, August 19, 2013 2:38 PM

All replies

  • your code is weird you are passing $a then inside your pipes you are calling $i.. what? i think you want

    $a = get-content "C:\UpdateCheck\Machinelist.txt"
    $a|foreach-Object {Get-WmiObject win32_service -computername $_ }|
    where {$_.state -eq "stopped" -and $_.startmode -eq "auto"}|
    Tee-object -filepath C:\updatecheck\$i.txt

    any duplicates in your machine list?
    If its going through servers and they have matching services you are probably going to see same services multiple times..

    • Edited by ImMax Monday, August 19, 2013 1:32 PM
    Monday, August 19, 2013 1:29 PM
  • no there are only 3 on it at the moment. One has only one service stopped but shows 3 times. See below:

    ExitCode  : 0
    Name      : SysmonLog
    ProcessId : 0
    StartMode : Auto
    State     : Stopped
    Status    : OK

    ExitCode  : 0
    Name      : SysmonLog
    ProcessId : 0
    StartMode : Auto
    State     : Stopped
    Status    : OK

    ExitCode  : 0
    Name      : SysmonLog
    ProcessId : 0
    StartMode : Auto
    State     : Stopped
    Status    : OK

    Monday, August 19, 2013 1:32 PM
  • yeah it might be because you got 2 foreach loops the code is confused read above
    Monday, August 19, 2013 1:38 PM
  • Basically the $i refers to each line in the txt file. It probably would run without the -computername $i but still give me the output in triplicate. Let me test that

    Your edited version of the script only gave the output for one of the servers on the list and that was in duplicate. It also did not output to txt.

    Scott

    Monday, August 19, 2013 1:42 PM
  • ohh the text file would need to be changed to

    C:\updatecheck\$_.txt

    Monday, August 19, 2013 1:48 PM
  • The name of the text file needs to be changed? I don't see how that would stop it from going round 3 times. I will try that now and see if it makes any difference.

    Scott

    Monday, August 19, 2013 1:53 PM
  • No difference by doing that. My original script gives me the information that I need but in triplicate...I cant seem to work out why it is looping around 3 times....
    Monday, August 19, 2013 2:03 PM
  • Hi,

    In your foreach loop, why are you referencing $a again?


    Don't retire TechNet!

    Monday, August 19, 2013 2:31 PM
  • Try this slight adjustment:

    $a = get-content 'C:\UpdateCheck\Machinelist.txt'
    
    foreach ($i in $a) {
    
        Get-WmiObject win32_service -computername $i |
        Where {$_.state -eq "stopped" -and $_.startmode -eq "auto"} |
        Tee-object -filepath "C:\updatecheck\$i.txt"
    
    }

    One thing to watch out for - Tee-Object writes files with Unicode encoding. You may want to just use Out-File, since you're using it at the end of the pipline anyway.


    Don't retire TechNet!

    • Proposed as answer by jrv Monday, August 19, 2013 3:08 PM
    • Marked as answer by IamMred Sunday, October 13, 2013 2:53 AM
    Monday, August 19, 2013 2:38 PM
  • Guys

    I finally worked it out. Here is the script working as it should:

    $a = get-content "C:\UpdateCheck\Machinelist.txt"
    foreach ($i in $a)
    {
    Get-WmiObject win32_service -computername $i |
    where {$_.state -eq "stopped" -and $_.startmode -eq "auto"}|
    Tee-object -filepath C:\updatecheck\$i.txt
    }

    Monday, August 19, 2013 2:51 PM
  • Mike

    Didn't realise that about the Tee cmdlet. I will change that to out-file.

    Thanks

    Scott

    Monday, August 19, 2013 3:11 PM
  • Mike

    Yes I eventually realised that I was referencing $a again. No wonder it output in triplicate.

    Cheers

    Scott

    Monday, August 19, 2013 3:12 PM