locked
ForEach-Object pipe out $_ not working RRS feed

  • Question

  • Morning all,

    1. Working

    $computerlist= get-content '\\share\Server.txt'
    #write-host $computerlist
    ForEach-Object {get-service -ServiceName '*SQL*' -ComputerName $computerlist | Format-table -Property MachineName,Name,StartType,Status,ServiceName,StartType }
    

    2. Not working

    $computerlist= get-content '\\share\Server.txt' |
    #write-host $computerlist
    ForEach-Object {get-service -ServiceName '*SQL*' -ComputerName $_ |Format-table -Property MachineName,Name,StartType,Status,ServiceName,StartType}
    #export-csv -Path '\\share\SQL_services.csv' -NoTypeInformation
    

    Wednesday, March 25, 2020 10:53 AM

All replies

  • You are overcomplicating this. This should be enough.
    $computerlist = get-content '\\share\Server.txt'
    get-service -ServiceName '*SQL*' -ComputerName $computerlist | 
        Format-table -Property MachineName,Name,StartType,Status,ServiceName
    
    $computerlist = get-content '\\share\Server.txt' 
    get-service -ServiceName '*SQL*' -ComputerName $computerlist |
        Select-Object -Property MachineName,Name,StartType,Status,ServiceName |
            export-csv -Path '\\share\SQL_services.csv' -NoTypeInformation
    You should start by learning the very basics of Powershell first.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Wednesday, March 25, 2020 12:35 PM
    Wednesday, March 25, 2020 12:15 PM
  • Thank you.

    I am getting an error for 2nd one.

    When do i need to use foreach-object.

    Get-Service : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    At line:3 char:48
    + get-service -ServiceName '*SQL*' -ComputerName $_ |
    +                                                ~~
        + CategoryInfo          : InvalidData: (:) [Get-Service], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetServiceCommand
     

    PS 

    Wednesday, March 25, 2020 12:33 PM
  • Thank you.

    I am getting an error for 2nd one.

    When do i need to use foreach-object.

    Get-Service : Cannot validate argument on parameter 'ComputerName'. The argument is 

    Oooops, my mistake. I pieced it together from your broken code. ;-) Fixed now in the original post.

    You should start by learning the very basics of Powershell first. You could fix those kinds of errors by yourself then. ;-)

    Read the help for Foreach-Object to learn how to use it. You should read the complete help inculding the examples.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Wednesday, March 25, 2020 12:41 PM
    Wednesday, March 25, 2020 12:39 PM
  • https://devblogs.microsoft.com/scripting/getting-to-know-foreach-and-foreach-object/

    Got that. I think i am doing more than one server, i can use this method.

    To get around this, we can make use of some other capabilities of Windows PowerShell by using ForEach or ForEach-Object to iterate through collections and to perform an action against each item in the collection. Each of these approaches can let you run through a collection and then perform actions in a script block. What you may not know is that each cmdlet has two approaches to how they take and handle the collections.

    Wednesday, March 25, 2020 12:41 PM
  • Got that. I think i am doing more than one server, i can use this method.

    Get-Service can work with a list of computers by itself. So you don't need a dedicated loop in this case. In other cases you will need. Learn basic Powershell from scratch and how to read the help for the cmdlets you're about to use. Then you will know when it's necessary to use a loop and when it's not.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, March 25, 2020 12:45 PM
  • Thanks. This time i trying CIM. It is fine up to format table.

    I cannot pipe out to export-csv from FT.

    $computers= get-content '\\share\Server.txt'
    #write-host $computers
    Get-CimInstance -ComputerName $computers -Query "Select * from Win32_Service where name like '%SQL%' and State = 'Running'" |
    Select-Object PSComputerName,DisplayName, Name,ServiceType,StartMode,StartName,State |
    Format-Table -AutoSize |
    export-csv -Path '\\share\SQL_services.csv' -NoTypeInformation
    
    

    Wednesday, March 25, 2020 1:03 PM
  • You cannot use a wildcard in a list of computers. Wildcards only work with service names.

    Please always take the time to read the CmdLet help.  First take the time to learn how to read and understand teh CmdLet help.

    help Get-Service -Parameter ComputerName


    \_(ツ)_/

    Wednesday, March 25, 2020 1:09 PM
  • Please. That's a ALL BASIC Powershell.

    You cannot use a format cmdlet and pipe that output to export-Csv. Format cmdlets should ALWAYS be the last in the pipeline. Omit it in this case completely.

    You cannot learn a complex technology by guessing and copying together some snippets you found on the internet you don't even  understand. You have to have a basic knowledge to understand the help you get.

    Please make a step back and start with the basics. Here you have two great options to start with:

    Windows PowerShell™ 4: TFM

    Getting Started with PowerShell 3.0

    Don't worry if it's not the current version. The basics are still the same.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Wednesday, March 25, 2020 1:14 PM
    Wednesday, March 25, 2020 1:14 PM
  • Got you. Thank you, will start again with the book.
    Wednesday, March 25, 2020 1:43 PM