Author Posts

March 10, 2017 at 2:30 pm

Hello experts,

Being a newbie to Powershell, seeking advise from you...!

I'm a wintel support guy and looks after hundreds of servers running equal number of applications. my day in office starts with healthcheck of servers by looking applications services, disk space, URL status, scheduled job status ...etc. to make my job easy, i'm thinking to automate health check report using Powershell.

I have Server inventory sheet which contains ServerName, ServiceType, Prod/Dev Env....etc.
Ex:
ServerName ServiceType Prod/Dev Env
Svr01 App1 Prod
Svr02 App1 Dev
Svr03 App2 Prod
Svr04 App3 Prod

My question is, by importing CSV file, how do I pass multiple parameters to multiple functions to get healthcheck report for disk, application service status (many servers have specific application service name), last reboot...etc...

Expected output

Disk Information:
ServerName ServiceType Device ID Total Size, free Space
Svr01 App1 C: 100 10

Service Status:
ServerName App ServiceName Status Env
Svr01 AppService1 Running Prod
Svr02 AppService2 Stopped Prod
Svr03 AppService3 Running Prod

Thanks in Advance

March 10, 2017 at 3:53 pm

You would write a script.

Read through our free book (eBooks menu) on Creating HTML Reports in PowerShell. It walks through a very similar, simplistic example of this, including producing an HTML-formatted report at the end.

March 10, 2017 at 4:20 pm

Thanks Don for prompt response.

Below is the piece of code where I get output for single/first server only....How do I pass array to function? so that I can get output for all servers listed in CSV..

someting like

MyFuntion ($Server, $envt, App)
{
Out-something...!!
}

—-
$csvPath = 'F:\MyDocs\MyScripts\ServerMonitoring\TPAServerInventory.csv'

$Serverinv = Import-Csv -Path $ScrPath

foreach ($record in $Serverinv)
{
$Server = $($record.ServerName)
$Envt = $($record.Environment)
$App = $($record.ServiceType)
}

Function ChkSrv()
{
$servicechk = @()
foreach ($svr in $Server)
{
if ($App -eq 'Cognos')
{
$Stat = Get-Service -ComputerName $server -Name 'IBM Cognos 10' | select Name, Status
$objsrv = New-Object psobject
Add-Member -InputObject $objsrv -membertype noteproperty -name ServerName -value $Server
Add-Member -InputObject $objsrv -membertype noteproperty -name Servicename -value $stat.Name
Add-Member -InputObject $objsrv -membertype noteproperty -name Status -value $Stat.Status
$servicechk += $objsrv
}

}
Return $servicechk
}

chksrv | Format-Table -AutoSize
——————-

OutPut:

ServerName ServiceName Status
———- ———– ——–
SRV01 IBM Cognos 10 Running

March 10, 2017 at 6:39 pm

try:


#region Input
$csvPath = '.\TPAServerInventory.csv' # Columns: ServerName,ServiceName,Env
#endregion


#region Process
$Report = foreach ($Server in (Import-Csv $csvPath)) {
    New-Object -TypeName PSObject -Property @{
        ServerName    = $Server.ServerName 
        ServiceName   = $Server.ServiceName
        ServiceStatus = $(
            try {
                (Get-Service -ComputerName $Server.ServerName -Name $Server.ServiceName -ErrorAction Stop).Status
            } catch {
                'DoesNotExist'
            }
        )
        Environment   = $Server.Env
    } | select ServerName, ServiceName, ServiceStatus, Environment # to order output columns
}
#endregion


#region Output
$Report | Format-Table -AutoSize
#endregion

Sample output:


ServerName ServiceName ServiceStatus Environment
---------- ----------- ------------- -----------
localhost  WwanSvc           Stopped Prod       
localhost  wudfsvc           Running Dev        
localhost  App2         DoesNotExist Prod