Passing CSV imported parameters to multiple fuctions

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Sam Boutros Sam Boutros 2 months, 2 weeks ago.

  • Author
    Posts
  • #66100
    Profile photo of Sachin Ghagare
    Sachin Ghagare
    Participant

    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

  • #66123
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #66127
    Profile photo of Sachin Ghagare
    Sachin Ghagare
    Participant

    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

  • #66136
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    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     
    
    

You must be logged in to reply to this topic.