Cant work out why this bit of PS isnt working

  • Getting service account being used is in the code I provided a few post back...

    Get-WmiObject win32_service | where {$_.DisplayName -match "SQL Server"} | select Name, DisplayName, State, StartName

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Hi Shawn,

    Yes, thank you for that, I get the info for the local machine, I need to work out how I can get it for the remote servers I am looking at.

    Regards,

    D.

  • Use the same line, just add the "-ComputerName".

    This:

    Get-WmiObject win32_service | where {$_.DisplayName -match "SQL Server"} | select Name, DisplayName, State, StartName

    Equates to this for running it on my local machine:

    Get-WmiObject win32_service -ComputerName localhost | where {$_.DisplayName -match "SQL Server"} | select Name, DisplayName, State, StartName

    You use the same command and pipeline commands, just adjusting for running it remotely. So your command would just be:

    Get-Service -ComputerName $ComputerName -Name SQL* | select Name, DisplayName, State, StartName

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Right ok, if there is one thing I have noticed about myself is that I seem to get deeper and deeper then end up going nowhere!

    I'm trying to get the results of the below query to go into a .csv file. One thing led to another and I end up with what is below.

    [/code]

    $folder = "H:\temp1"

    $currdate = Get-Date -Format "yyyy-MM-dd"

    $filename = "$($InstanceName)_ServiceInformation_$($currdate).csv"

    $fullpath = Join-Path $folder $filename

    $result= @()

    Get-Content H:\Documents\DevServers.txt | ForEach-Object {Get-WmiObject -Class Win32_Service -ComputerName "THE FQDN OF THE REMOTE MACHINE" | Where $_.Name -match "SQL"} | Select Name, DisplayName, State, Startname

    $result | Select Name, DisplayName, State, Startname |

    Export-Csv -Path $fullpath -NoTypeInformation

    [/code]

    and I end up with the following error...

    Where-Object : Cannot validate argument on parameter 'Property'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

    At H:\Documents\ListServicesWorkInProgress_2_12_2015.ps1:22 char:167

    + ... om.au" | Where $_.Name -match "SQL"} | Select Name, DisplayName, State, Startnam ...

    + ~~~~~~~

    + CategoryInfo : InvalidData: (:) [Where-Object], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.WhereObjectCommand

    I don't understand the Property parameter, I have tried variations of the parameter from Googling, but I am not sure where (and what ) I am supposed to put the info. Whatever I put in for the -property parameter does not seem to make any difference.

    Can somebody please point me in the right direction?

    Regards,

    D.

  • Unless you had a copy/paste error with your code, the error you are getting has nothing to do with the property.

    You are missing the opening curly brace for the "where" command.

    You have this:

    "...THE REMOTE MACHINE" | Where $_.Name -match "SQL"} | Select Name...

    Where it should be this:

    "...THE REMOTE MACHINE" | Where {$_.Name -match "SQL"} | Select Name...

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Thanks Shawn, the curly brace got me nearer to the goal, after a bit of jiggery pockery I got to this, but I am finding that it is creating the CSV, but when I open it its blank, it writes/displays to the console.

    $InstanceName = Get-Content H:\Documents\DevServers.txt

    $folder = "H:\temp1"

    $currdate = Get-Date -Format "yyyy-MM-dd"

    $filename = "$($InstanceName)_ServiceInformation_$($currdate).csv"

    $fullpath = Join-Path $folder $filename

    $result= @()

    ForEach-Object {Get-WmiObject -Class Win32_Service -ComputerName $InstanceName} | Where {$_.DisplayName -Match "SQL"} | Select Name, DisplayName, State, Startname

    $result | Select Name, DisplayName, State, Startname |

    Export-Csv -Path $fullpath -NoTypeInformation

    Any ideas, and thank you again. Things are just starting to fall into place there more I use PowerShell.

    Regards,

    D.

  • $result= @()

    ForEach-Object {Get-WmiObject -Class Win32_Service -ComputerName $InstanceName} | Where {$_.DisplayName -Match "SQL"} | Select Name, DisplayName, State, Startname

    $result | Select Name, DisplayName, State, Startname |

    Export-Csv -Path $fullpath -NoTypeInformation

    Your $results variable is not being set to what you are pulling from the foreach loop.

    $result= ForEach-Object {Get-WmiObject -Class Win32_Service -ComputerName $InstanceName} | Where {$_.DisplayName -Match "SQL"} | Select Name, DisplayName, State, Startname

    $result | Select Name, DisplayName, State, Startname |

    Export-Csv -Path $fullpath -NoTypeInformation

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • That's great, Shawn! Thank you!

    Regards,

    D.

Viewing 8 posts - 16 through 22 (of 22 total)

You must be logged in to reply to this topic. Login to reply