I have a script that checks a list of computer names for a list of known services. It gives the status of those services and it also is supposed to list the StartType. Is there a reason why the StartType is not being given in the output?
In the output, the PSComputerName, ServiceName, and Status columns contain data but the StartType column remains blank.
$myServices = $PSScriptRoot + "\services.txt" # $PSScriptRoot references current directory
$myServers = $PSScriptRoot + "\servers.txt"
$Log = $PSScriptRoot + "\svclog.csv"
Remove-Item -Path $Log
$serviceList = Get-Content $myServices
$results = Get-Content $myServers
Invoke-command -ComputerName $results -ScriptBlock {
Param($serviceList)
Get-Service $serviceList | Select -Property ServiceName, Status, StartType
} -ArgumentList $serviceList,$Null | Select -Property PSComputerName, ServiceName, Status, StartType |
Export-Csv -NoTypeInformation -Path $Log
I have tried it on Version 5 build: 14409 Revision: 1012
&
Version 5 build: 10586 Revision 117
The Get-Service cmdlet may not return the StartType property, but wmi does house that information. This should work for you:
ForEach ($Service in $myServices)
{
Get-WmiObject -ComputerName #(Get-Content -Path $myServers) -Class Win32_Service -Filter "Name='$Service'" |
Select-Object -Property PSComputerName, ServiceName, Status, StartType |
Export-Csv -NoTypeInformation -Path $Log -Append
}
Update to use Invoke-Command:
Invoke-Command -ComputerName $results -ScriptBlock {
ForEach ($service in $using:serviceList)
{
Get-WmiObject -Class Win32_Service -Filter "Name='$service'" |
Select-Object -Property PSComputerName, Name, Status, StartMode
}
} | Export-Csv -NoTypeInformation -Path $Log
In powershell I am trying to do the following:
$name = "computername"
#get installed programs
Write-Host "****APPLICATIONS"
gwmi win32_Product -ComputerName $name | select name
#gets services
write-host "****SERVICES"
Get-Service -ComputerName $name | ft
the expected output would be
****APPLICATIONS
name
of
app
****SERVICES
running services here
more services here
the actual result is
****APPLICATIONS
****SERVICES
name
of
app
running services here
more services here
I have attempted to do start-job then wait-job , but running gwmi as a job seems to output nothing to the console and sending the output to a separate file defeats the purpose of other parts of the script
I also attempted to use start-sleep and it still finishes both write-host commands before proceeding
Try this:
$name = "computername"
Write-Host "`n****APPLICATIONS`n"
gwmi win32_Product -ComputerName $name | % {$_.name}
write-host "`n****SERVICES"
Get-Service -ComputerName $name | ft
If you want the results alphabetical:
$name = "computername"
Write-Host "`n****APPLICATIONS`n"
$apps = gwmi win32_Product -ComputerName $name | % {$_.name}
$apps | sort
write-host "`n****SERVICES"
Get-Service -ComputerName $name | ft
Param(
$ComputerName = 'AT805061'
)
# Get installed programs
Write-Host "`n****APPLICATIONS`n"
Get-WmiObject win32_Product -ComputerName $ComputerName | Select-Object -ExpandProperty Name | Sort-Object
# Get services
Write-Host "`n****SERVICES`n"
Get-Service -ComputerName $ComputerName | Where-Object -Property Status -eq -Value Running | Select-Object -ExpandProperty Name | Sort-Object
I would like to return output for below in rows and cols style not one after another.
$path = "$([Environment]::GetFolderPath("Desktop"))\Installed Hotfixes Info.txt";
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . | fl * | Format-Table -AutoSize |fl > $path; notepad $path;
Remove the Format-List (fl):
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . |
Format-Table -AutoSize |
Out-File $path
Better yet, export the output as CSV:
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . |
Export-Csv $path -NoType
Could you please inform me how can I make my script to format its output in a single table for all "services" per "server"?
Please find my current PowerShell script below:
$serverList = gc computer.txt
$serviceList = gc service.txt
if ((Test-Path OUTPUT.txt) -eq $true) {
Write-Host "Deleting existing OUTPUT file"
Remove-Item OUTPUT.txt
}
ForEach ($server in $serverList)
{
$style = #{Expression={$server};Label="Server Name";width=30}, `
#{Expression={$_.Name};Label="Service Name";width=20}, `
#{Expression={$_.StartMode};Label="StartMode";width=10}, `
#{Expression={$_.State};Label="State";width=10}, `
#{Expression={$_.ProcessId};Label="ProcessId";width=10}
Write-Host "Starting Service Check on $server"
ForEach ($service in $serviceList)
{
Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name='$service'" | Select-Object -Property Name, StartMode, State, ProcessId | Format-Table $style | Out-File OUTPUT.txt -Append
}
Write-Host "Service Check Completed on $server"
}
WMI appends the property PSComputername. You can just append it in your select-object-command like so:
Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name='$service'" | Select-Object -Property PScomputername, Name, StartMode, State, ProcessId | Format-Table $style | Out-File OUTPUT.txt -Append
I can only get the command to return the services on the first computer in the text file.
Is there a better way than for-each for this task?
Get-Service *vault* -ComputerName (Get-Content c:\users\sean\desktop\js.txt) | select name,status,machinename | sort machinename | format-table -autosize
Try it without the get-content. Try this:
Get-Service *vault* -ComputerName c:\users\sean\desktop\js.txt | select name,status,machinename | sort machinename | format-table -autosize
If that doesn't work, then try:
$Computers = Get-Content c:\users\sean\desktop\js.txt
Get-Service *vault* -computername $Computers | Select name,status,machinename |sort machinename |format-table -autosize
If you are eager for a one-liner then try this:
Get-Content c:\users\sean\desktop\js.txt | Get-Service *vault* | Select name,status,machinename |sort machinename |format-table -autosize
I would try the top one first. I would test, but I don't have access to anything I can do a proper test right now.
$Computers = get-content .\desktop\test.txt
$Service = "Vault"
foreach ($computer in $computers) {
$computer
$Servicestatus = get-service -name $Service -ComputerName $computer
}
$Servicestatus | select-object Name,Status,MachineName | format-table -Autosize
This works for me, it gives me each of the computers in the text file, and it looks for the service.
This is what I use. I get the list of computers from an OU in AD.
Import-Module ActiveDirectory
$ou = "OU=Servers,DC=Domain,DC=com"
$servers = Get-ADComputer -Filter * -SearchBase $ou | select-object -expandproperty name
Foreach ($server in $servers){
$Data = Get-Service -ServiceName *IIS*,*TomCat*,*httpd* -ComputerName $server | select machinename,name | sort machinename | format-table -AutoSize
Write($Data) | Out-File .\WebServices.txt -Append
}
$servers = Get-Content .\servers.txt
Foreach ($server in $servers) {
"$server"
Get-Service -ComputerName $Server -name -like "*vault*"
"-------------------"
}
Following a memory limitation limit with older versions of PowerShell, I was required to refresh my code:
Old code:
gwmi win32_service -computer $allcomputers | Select-Object __SERVER,Name,state,startmode,StartName
New code:
`$servers = Get-Content "computers.txt"
Foreach ($server in $servers) {
Get-WmiObject -Class WIN32_service -ComputerName $server |
Select-Object __SERVER,Name,state,startmode,StartName |
Export-Csv -path "Report.CSV" -NoTypeInformation -Append
}`
This is how you can get list of all services in your AD domain:
Get-ADComputer -Filter {OperatingSystem -Like “Windows 10*”} | ForEach-Object {Get-WmiObject -Class Win32_Service -Computer $_.Name}
More useful examples on this (get list of services for all computer listed in a text file, etc.):
https://www.action1.com/kb/list_of_services_on_remote_computer.html
Get-Service -ComputerName ... has a bug in PowerShell 2.0 that only returns the first computer. This is fixed in newer versions so if you upgrade to PowerShell 3.0 or newer, your original code will work fine.
As a workaround, use a foreach-loop to run Get-Service once for each computer:
Get-Content c:\users\sean\desktop\js.txt |
ForEach-Object { Get-Service -Name *vault* -ComputerName $_ } |
Select-Object -Property Name, Status, MachineName |
Sort-Object -Property MachineName |
Format-Table -AutoSize
Nick's solution totally doesn't work for me. I ended up writing a quick and dirty one that works:
$servers = Get-Content .\servers.txt
Foreach ($server in $servers) {
"$server"
Get-Service *vault*
"-------------------"
}