top of page

PowerShell Server Baseline | SQL Insert | Input Script

1) The initial run will execute the Input Script adding the server names

as well as all running services found on each system into a SQL database table.

 

#=====================================================

# Get Service Information and Export to CSV 

#=====================================================

#Get List of Online Accessible Servers
$GetServers = Get-Content C:\temp\ServerList.txt 

#Query Each Server for a List of Running Services
$Results=Invoke-Command -ComputerName $GetServers -ScriptBlock {Get-Service | Where{$_.Status -eq "Running"}}

#Export Select Results to File and for Reference File
$Results | Select PSComputerName,Name, DisplayName | Export-csv C:\temp\MyServices.csv -NoTypeInformation 

#=====================================

# Inset Data Into SQL DB Table 

#======================================

#Import Data For Insert to SQL 
$ImportData=Import-CSV C:\temp\MyServices.csv

#Downloads and Installs SQL Module and Will Overwrite and Older Version Module Installed
Install-Module -Name SqlServer -AllowClobber

#Loop Through Each Record and Add to SQL Table
Foreach($Comp in $ImportData){

    $insertquery="
                INSERT INTO [dbo].[ComputerServices]
                ([PSComputerName]
                ,[Name]
                ,[DisplayName])
         VALUES
               ('$($Comp.PSComputerName)'
               ,'$($Comp.Name)'
               ,'$($Comp.Displayname)')"

    #Command to Insert to SQL     
    Invoke-Sqlcmd -ServerInstance Desktop-Main -database ServerManager -Query $insertquery
}

 

bottom of page