error with my powershell scripts

motioneye used Ask the Experts™
Someone in EE has sharing me on this scripts use to extract accounts services. but it return me below error.

PS F:\PowerShell_Myscripts> .\checkservices.ps1
Missing expression after unary operator '-'.
At F:\PowerShell_Myscripts\checkservices.ps1:5 char:10
+         - <<<< namespace root\Microsoft\SqlServer\ComputerManagement
PS F:\PowerShell_Myscripts>
$Instances = get-content "F:\PowerShell_Myscripts\AllServers.txt"
$sqlservice = Get-WmiObject     
        -namespace root\Microsoft\SqlServer\ComputerManagement
        -class SqlService
        -filter "ServiceName='MSSQL`$_'"
} >> F:\PowerShell_Myscripts\result.txt

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PowerShell Developer
Top Expert 2010

The line needs to be continued using the ` character. See below. Without that it treats each of the parameters (those starting -namespace, -class and -filter) as separate commands.

I would use Export-CSV at the end as well, feel free to change that back to redirection if that's what you prefer.

$Instances = get-content "F:\PowerShell_Myscripts\AllServers.txt"
$Instances | %{
  Get-WmiObject `
    -namespace root\Microsoft\SqlServer\ComputerManagement `
    -class SqlService `
    -filter "ServiceName='MSSQL`$_'"
} | Export-CSV "F:\PowerShell_Myscripts\result.csv"

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial