Link to home
Start Free TrialLog in
Avatar of motioneye
motioneyeFlag for Singapore

asked on

Powershell - How to embedded multiple sql query into powershell

Guys,
I need to know how I can run multiple  sql query in the  powershell script and output the result into csv file, for example each query will return  some set of records  and  the output of the query  should look exactly as per  xls report attached on the output structures.
Actually I did posted on the same help from the link https://www.experts-exchange.com/questions/28999380/How-to-run-in-powershell-for-multiple-sql-query.html but what was  lack there  is the output of the report does not meet my interest.

***************************************************************************************
Check SQL Server Logins Audit
***************************************************************************************
*/
SET NOCOUNT ON;
PRINT N'6.03 -  Auditing Enabled for Failed and Successful Logins';
DECLARE @AuditLevel int
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', 
   N'Software\Microsoft\MSSQLServer\MSSQLServer', 
   N'AuditLevel', @AuditLevel OUTPUT
SELECT @@servername as ServerName, CASE WHEN @AuditLevel = 0 THEN 'None'
   WHEN @AuditLevel = 1 THEN 'Successful logins only'
   WHEN @AuditLevel = 2 THEN 'Failed logins only'
   WHEN @AuditLevel = 3 THEN 'Both failed and successful logins' 
   END AS [AuditLevel] 

   SET NOCOUNT OFF;
/*
***************************************************************************************
Check SQL Authentication Setting
***************************************************************************************
*/
SET NOCOUNT ON;
print  N'3.1 Set The Server Authentication Property To Windows Authentication mode';
SELECT @@servername as ServerName, CASE SERVERPROPERTY('IsIntegratedSecurityOnly')   
WHEN 1 THEN 'Windows Authentication'   
WHEN 0 THEN 'Windows and SQL Server Authentication'   
END as [Authentication Mode] 

SET NOCOUNT OFF;

/*
***************************************************************************************
Check The DB Trustworthy ON or OFF 
***************************************************************************************
*/
SET NOCOUNT ON;
print  N'2.9 Set the Trustworthy Database Property to Off';
SELECT name as DatabaseName, CASE  when is_trustworthy_on  =1  Then 'SET' ELSE 'Not SET' END AS DB_trustworthy_status , 
                             CASE  when is_trustworthy_on  =1  Then 'Failed' ELSE 'Passed'  END AS trustworthy_CIS_status 
from sys.databases where database_id >4
SET NOCOUNT OFF;
/*
***************************************************************************************
Check SQL Configuration Setting
***************************************************************************************
*/
SET NOCOUNT ON;

print  N'SQL Configurations Checks';
IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE name = '##Configurations' AND type in (N'U'))
DROP TABLE ##Configurations;
CREATE TABLE  ##Configurations(
[Name] varchar(100) NULL,
[Value] varchar(10)  NULL,
[Description] varchar(100) NULL
)

Insert into ##Configurations select name,convert( varchar(20),value),[description] from sys.configurations where name in 
( 'Ad Hoc Distributed Queries',
'CLR Enabled',
'Cross DB Ownership Chaining',
'Database Mail XPs',
'Ole Automation Procedures' ,
'Remote Access',
'Remote Admin Connections',
'contained database authentication',
'Scan For Startup Procs',
'xp_cmdshell',
'Default Trace Enabled',
'Login Auditing',
  'SQL Mail XPs')
  
 SELECT 
       name,description,
	   CASE WHEN  value = 0   THEN 'Passed'
	   WHEN  value = 1   and name= 'Default Trace Enabled'  THEN 'Passed'
	   ELSE 'FAILED'  END AS CIS_SQL_Configuration_Status 
  FROM ##configurations 

  SET NOCOUNT OFF;

Open in new window

Document2.xlsx
SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of motioneye

ASKER

Hi
I save the above script and run as ps1 file. the scrip ran but there were no output.

PS H:\temp>
PS H:\temp>
PS H:\temp>
PS H:\temp> .\Test-SqlServerCrosschecks.ps1 serer\instance2
PS H:\temp>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Qlemo,
It is now working, btw do you know how to save the result in csv instead just prompt it in the ps console ? I really want this to be in csv format then later we will use that to import in the database.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Qlemo,
Thanks for that, I was actually forgotten that the parameter that we can include to save in csv  and it's again you have  helped me with that :).This si working fine now and I can modify and make necessary adjustment as I like.
The actual report and scripts was really long so I want to simplify the code only to get to know me how to build the scripts for that in powershell.
I was particularly interested in making the script as generic as reasonable to allow further modifications easily.
you read my mind though :)