[SQL Server] Collecter les informations systèmes et matérielles des serveurs

/*

 

EXEC xp_cmdshell 'powershell.exe -ExecutionPolicy Bypass -command \\MonServeurFichiers\DBA\script_2.ps1'

 

powershell.exe -ExecutionPolicy Bypass -command \\MonServeurFichiers\DBA\script_1.ps1

*/

 

/*

Invoke-Command -ComputerName "Monserveur_1","Monserveur_2" -ScriptBlock {& cmd /c "ECHO %COMPUTERNAME% & wmic diskdrive get size"} | Out-File "\\MonServeurFichiers\DBA\informationsDisques.txt"

 

*/

 

/*

Invoke-Command -ComputerName "Monserveur_1","Monserveur_2" -ScriptBlock {& cmd /c "systeminfo"} | Out-File "\\MonServeurFichiers\DBA\informationsSystemes.txt"

 

*/

/*

Invoke-Command -ComputerName "Monserveur_1","Monserveur_2" -ScriptBlock {& cmd /c "ECHO %COMPUTERNAME% & WMIC CPU Get NumberOfCores"} | Out-File "\\MonServeurFichiers\DBA\NumberOfCores.txt"

 

*/

 

/*

Invoke-Command -ComputerName "Monserveur_1","Monserveur_2" -ScriptBlock {& cmd /c "ECHO %COMPUTERNAME% & WMIC CPU Get NumberOfLogicalProcessors"} | Out-File "\\MonServeurFichiers\DBA\NumberOfLogicalProcessors.txt"

 

*/

-- =============================================

-- Author:     Zouhaier KHARROUBI

-- Create date: 09/11/2020

-- Description: Collecter les informations systèmes et matérielles des serveurs

-- =============================================

/*

 

Sur powershell lancer les deux commandes ci-dessous

--Récupérer le nom de disque avec sa taille

Invoke-Command -ComputerName "Monserveur_1","Monserveur_2" -ScriptBlock {& cmd /c "ECHO %COMPUTERNAME% & wmic diskdrive get name,size"} | Out-File "\\MonServeurFichiers\DBA\informationsDisques.txt"

 

--Récupérer la taille du disque sans le nom de disque

Invoke-Command -ComputerName "Monserveur_1","Monserveur_2" -ScriptBlock {& cmd /c "ECHO %COMPUTERNAME% & wmic diskdrive get size"} | Out-File "\\MonServeurFichiers\DBA\informationsDisques.txt"

 

--Récupérer NumberOfCores

Invoke-Command -ComputerName "Monserveur_1","Monserveur_2" -ScriptBlock {& cmd /c "ECHO %COMPUTERNAME% & WMIC CPU Get NumberOfCores"} | Out-File "\\MonServeurFichiers\DBA\NumberOfCores.txt"

 

--Récupérer NumberOfLogicalProcessors

Invoke-Command -ComputerName "Monserveur_1","Monserveur_2" -ScriptBlock {& cmd /c "ECHO %COMPUTERNAME% & WMIC CPU Get NumberOfLogicalProcessors"} | Out-File "\\MonServeurFichiers\DBA\NumberOfLogicalProcessors.txt"

 

--Récupérer les informations retournée par la commande systeminfo

Invoke-Command -ComputerName "Monserveur_1","Monserveur_2" -ScriptBlock {& cmd /c "systeminfo"} | Out-File "\\MonServeurFichiers\DBA\informationsSystemes.txt"

*/

 

       -- Add the parameters for the stored procedure here

DECLARE      @Lancer_Requete            BIT=1

 

BEGIN

       -- SET NOCOUNT ON added to prevent extra result sets from

       -- interfering with SELECT statements.

       SET NOCOUNT ON;

       SET FMTONLY OFF;

 

       IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#ListeFichiersImport]'))

             BEGIN

                    DROP TABLE #ListeFichiersImport

             END

 

       IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#FichierImport]'))

             BEGIN

                    DROP TABLE #FichierImport

             END

 

       IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#FichierImportTemp]'))

             BEGIN

                    DROP TABLE #FichierImportTemp

             END

 

       IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#InformationsServeurs]'))

             BEGIN

                    DROP TABLE #InformationsServeurs

             END

 

       IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#Serveurs_LiesTemp]'))

             BEGIN

                    DROP TABLE #Serveurs_LiesTemp

             END

 

       IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#Serveurs_Lies]'))

             BEGIN

                    DROP TABLE #Serveurs_Lies

             END

 

       IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#Requete]'))

             BEGIN

                    DROP TABLE #Requete

             END

 

 

       CREATE TABLE #ListeFichiersImport

       (

             Num_Lig                          INT IDENTITY (1,1),

             NomFichier                VARCHAR(800)        NULL,

       )

 

 

       CREATE TABLE #FichierImport

       (

       Num_Lig                                 INT IDENTITY (1,1),

       Ligne                            NVARCHAR(1500)  NULL

       )

 

       CREATE TABLE #FichierImportTemp

       (

       Num_Lig                                 INT IDENTITY (1,1),

       Ligne                            NVARCHAR(1500)  NULL

       )

 

       CREATE TABLE #InformationsServeurs

       (

       Num_Lig                                       INT IDENTITY (1,1),

       Environnement                    VARCHAR(35)    NULL,

       NomServeurPhysique               NVARCHAR(300) NULL,

       NomServeurLogique                NVARCHAR(300) NULL,

       DateDernierDemarrage       NVARCHAR(65)  NULL,

       ReplicaRole                             VARCHAR(50)   NULL,

       TypeServeur                             NVARCHAR(300) NULL,

       VersionWindows                          NVARCHAR(300) NULL,

       VersionSqlServer                 NVARCHAR(500) NULL,

       NbreInstanceSQL                         TINYINT               NULL,

       NbreBases                               INT                   NULL,

       Processeur                              VARCHAR(300)  NULL,

       NumberOfCores                    INT                   NULL,

       NumberOfLogicalProcessors  INT                   NULL,

       TailleMemoireGO                         NUMERIC(15,2) NULL,

       TailleDisqueGO                          NUMERIC(15,2) NULL,

       MessageErreur                    NVARCHAR(MAX) NULL

       )

 

       CREATE TABLE #Serveurs_LiesTemp

       (

       SRV_NAME                         VARCHAR(150),

       SRV_PROVIDERNAME           VARCHAR(150),

       SRV_PRODUCT                      VARCHAR(150),

       SRV_DATASOURCE                   VARCHAR(150),

       SRV_PROVIDERSTRING         VARCHAR(150),

       SRV_LOCATION               VARCHAR(150),

       SRV_CAT                                 VARCHAR(150)

       )

 

       CREATE TABLE #Serveurs_Lies

       (

       Num_Lig                                 INT IDENTITY (1,1),

       SRV_NAME                         VARCHAR(150),

       SRV_DATASOURCE                   VARCHAR(150),

       ServerName                       VARCHAR(150) NULL,

       NbreBases                        INT                 NULL,

       MessageErreur              NVARCHAR(MAX) NULL

       ) 

 

       CREATE NONCLUSTERED INDEX [IDX_#Serveurs_Lies_ServerName] ON #Serveurs_Lies

       (

             ServerName ASC

       )

 

       CREATE TABLE #Requete

       (

       NomServeurPhysique               NVARCHAR(300) NULL,

       Requete                                       NVARCHAR(MAX) NULL

       )

 

       DECLARE  @Requete                             NVARCHAR(3000)

                    ,@NomServeurPhysique       NVARCHAR(300)

                    ,@NomServeurLogique              NVARCHAR(300)

                    ,@Serveur_Lie                    NVARCHAR(150)

                    ,@NomInstance                    NVARCHAR(150)

                    ,@LocalHot                              VARCHAR(50)

                    ,@VersionSqlServer               NVARCHAR(500)

                    ,@NomFichierImport               VARCHAR(800)

                    ,@Chemin_FichierImport           VARCHAR(MAX)

                    ,@Nbre_Lig                              INT

                    ,@Num_Lig                               INT

                    ,@Nbre_Inst                             INT

                    ,@Num_Inst                              INT

                    ,@Ligne                                        NVARCHAR(1500)

                    ,@Valeur                                BIGINT

                    ,@ValeurMemoireGO_V1       NVARCHAR(150)

                    ,@ValeurMemoireGO_V2       NVARCHAR(150)

                    ,@Nbre_Chaine                    INT

                    ,@Num_Chaine                     INT

                    ,@NbreBases                             INT

                   

 

    -- Insert statements for procedure here

 

       IF @Lancer_Requete=1

             BEGIN

 

                    SELECT @LocalHot = QUOTENAME(@@SERVERNAME)

 

                    SELECT @Chemin_FichierImport='\\MonServeurFichiers\DBA\'

 

                    INSERT INTO #Serveurs_LiesTemp

                    EXEC sp_linkedservers 

 

 

                    INSERT INTO #Serveurs_Lies (SRV_NAME,SRV_DATASOURCE)

                    SELECT DISTINCT SRV_NAME,SRV_DATASOURCE FROM #Serveurs_LiesTemp WHERE SRV_NAME NOT IN('serveuru_x1','serveur_x2')

                    --AND SRV_NAME='MonInstanceSQL'

 

 

                    ------Traiter les fichiers qui possèdent les informations des espaces disques

      

                    SELECT @Requete =  N'DIR "' + @Chemin_FichierImport + 'informationsDisques*.txt" /B'

      

                    INSERT INTO #ListeFichiersImport (NomFichier)

                    EXEC Master..xp_cmdshell @Requete

                                

                    SELECT @Num_Lig=1,@Nbre_Lig=COUNT(Num_Lig) FROM #ListeFichiersImport

 

                    WHILE @Num_Lig <= @Nbre_Lig

                          BEGIN

                                 SELECT @NomFichierImport=NomFichier FROM #ListeFichiersImport WHERE Num_Lig=@Num_Lig   

 

                                 IF ISNULL(@NomFichierImport,'') LIKE 'informationsDisques%'

                                       BEGIN

                                              SELECT @Requete = 'TYPE ' + @Chemin_FichierImport + @NomFichierImport

 

                                              INSERT INTO #FichierImport (Ligne)

                                              EXEC Master..xp_cmdshell @Requete

                                       END

                   

                                 SELECT @Num_Lig = @Num_Lig + 1

 

                          END

 

                    --Consolider les informations des disques

                    SELECT @Num_Lig=1,@Nbre_Lig=COUNT(Num_Lig) FROM #FichierImport

 

                    WHILE @Num_Lig <= @Nbre_Lig

                          BEGIN

                                 SELECT @Ligne=Ligne FROM #FichierImport WHERE Num_Lig=@Num_Lig

 

                                 IF ISNULL(@Ligne,'')<>'Size' AND ISNULL(@Ligne,'')<>'' AND ISNUMERIC(ISNULL(@Ligne,''))=0 AND CHARINDEX('WMIC',@Ligne,0)=0

                                       BEGIN

 

                                              UPDATE #InformationsServeurs

                                              SET

                                              TailleDisqueGO=ROUND(((@Valeur/1023.697916666667)/1023.697916666667)/1023.697916666667,2)

                                              WHERE NomServeurPhysique=@NomServeurPhysique

 

                                              SELECT @NomServeurPhysique=LTRIM(RTRIM(@Ligne)),@Valeur=0

                                

                                              INSERT INTO #InformationsServeurs (NomServeurPhysique) VALUES (@NomServeurPhysique)

                                       END

                                 IF ISNUMERIC(ISNULL(@Ligne,''))=1

                                       BEGIN

                                              SELECT @Valeur=@Valeur + RTRIM(LTRIM(@Ligne))

                                       END

 

                                 SELECT @Num_Lig = @Num_Lig + 1

                          END

 

                    UPDATE #InformationsServeurs

                    SET

                          TailleDisqueGO=((@Valeur/1024)/1024)/1024

                    WHERE NomServeurPhysique=@NomServeurPhysique

 

                    UPDATE #InformationsServeurs

                    SET

                          NomServeurLogique= CASE NomServeurPhysique WHEN 'serveur_PH1' THEN 'serveur_3'

                                                                                     WHEN 'serveur_PH1' THEN 'serveur_4'

                                                                                     ELSE NULL END

 

                    ------Traiter les fichiers qui possèdent les informations du NumberOfCores

                    TRUNCATE TABLE #ListeFichiersImport

                    TRUNCATE TABLE #FichierImport

                    TRUNCATE TABLE #FichierImportTemp

 

                    SELECT @Requete =  N'DIR "' + @Chemin_FichierImport + 'NumberOfCores*.txt" /B'

      

                    INSERT INTO #ListeFichiersImport (NomFichier)

                    EXEC Master..xp_cmdshell @Requete

                                

                    SELECT @Num_Lig=1,@Nbre_Lig=COUNT(Num_Lig) FROM #ListeFichiersImport

 

                    WHILE @Num_Lig <= @Nbre_Lig

                          BEGIN

                                 SELECT @NomFichierImport=NomFichier FROM #ListeFichiersImport WHERE Num_Lig=@Num_Lig   

 

                                 IF ISNULL(@NomFichierImport,'') LIKE 'NumberOfCores%'

                                       BEGIN

                                              SELECT @Requete = 'TYPE ' + @Chemin_FichierImport + @NomFichierImport

 

                                              INSERT INTO #FichierImportTemp (Ligne)

                                              EXEC Master..xp_cmdshell @Requete

                                       END

                   

                                 SELECT @Num_Lig = @Num_Lig + 1

 

                          END

                   

                    INSERT INTO #FichierImport (Ligne)

                    SELECT Ligne FROM  #FichierImportTemp WHERE ISNULL(LTRIM(RTRIM(Ligne)),'')<>''

                    ORDER BY Num_Lig ASC

 

                    --Consolider les informations du NumberOfCores

                    SELECT @NomServeurPhysique=NULL,@Valeur=NULL

                    SELECT @Num_Lig=1,@Nbre_Lig=COUNT(Num_Lig) FROM #FichierImport

 

                    WHILE @Num_Lig <= @Nbre_Lig

                          BEGIN

                                 BEGIN TRY

                                       SELECT @Ligne=Ligne FROM #FichierImport WHERE Num_Lig=@Num_Lig

                   

                                       IF ISNULL(@Ligne,'')<>'NumberOfCores' AND ISNULL(@Ligne,'')<>'' AND ISNUMERIC(ISNULL(@Ligne,''))=0

                                              BEGIN

 

                                                    UPDATE #InformationsServeurs

                                                    SET

                                                           NumberOfCores=@Valeur

                                                    WHERE NomServeurPhysique=@NomServeurPhysique

 

                                                    SELECT @NomServeurPhysique=LTRIM(RTRIM(@Ligne)),@Valeur=0

                                

                                              END

                                       IF ISNUMERIC(ISNULL(@Ligne,''))=1

                                              BEGIN

                                                    SELECT @Valeur=ISNULL(@Valeur,0) + CAST(RTRIM(LTRIM(@Ligne)) AS INT)

                                              END

                                 END TRY

                                 BEGIN CATCH

                                       UPDATE #InformationsServeurs

                                       SET

                                              MessageErreur=ERROR_MESSAGE()

                                       WHERE NomServeurPhysique=@NomServeurPhysique

 

                                 END CATCH

 

                                 SELECT @Num_Lig = @Num_Lig + 1

                          END

 

                          UPDATE #InformationsServeurs

                          SET

                                 NumberOfCores=@Valeur

                          WHERE NomServeurPhysique=@NomServeurPhysique

 

                    ------Traiter les fichiers qui possèdent les informations du NumberOfLogicalProcessors

                    TRUNCATE TABLE #ListeFichiersImport

                    TRUNCATE TABLE #FichierImport

                    TRUNCATE TABLE #FichierImportTemp

 

                    SELECT @Requete =  N'DIR "' + @Chemin_FichierImport + 'NumberOfLogicalProcessors*.txt" /B'

      

                    INSERT INTO #ListeFichiersImport (NomFichier)

                    EXEC Master..xp_cmdshell @Requete

                                

                    SELECT @Num_Lig=1,@Nbre_Lig=COUNT(Num_Lig) FROM #ListeFichiersImport

 

                    WHILE @Num_Lig <= @Nbre_Lig

                          BEGIN

                                 SELECT @NomFichierImport=NomFichier FROM #ListeFichiersImport WHERE Num_Lig=@Num_Lig   

 

                                 IF ISNULL(@NomFichierImport,'') LIKE 'NumberOfLogicalProcessors%'

                                       BEGIN

                                              SELECT @Requete = 'TYPE ' + @Chemin_FichierImport + @NomFichierImport

 

                                              INSERT INTO #FichierImportTemp (Ligne)

                                              EXEC Master..xp_cmdshell @Requete

                                       END

                   

                                 SELECT @Num_Lig = @Num_Lig + 1

 

                          END

 

                    INSERT INTO #FichierImport (Ligne)

                    SELECT Ligne FROM  #FichierImportTemp WHERE ISNULL(LTRIM(RTRIM(Ligne)),'')<>''

                    ORDER BY Num_Lig ASC

 

                    --Consolider les informations du NumberOfLogicalProcessors

                    SELECT @NomServeurPhysique=NULL,@Valeur=NULL

                    SELECT @Num_Lig=1,@Nbre_Lig=COUNT(Num_Lig) FROM #FichierImport

 

                    WHILE @Num_Lig <= @Nbre_Lig

                          BEGIN

                                 SELECT @Ligne=Ligne FROM #FichierImport WHERE Num_Lig=@Num_Lig

 

                                 IF ISNULL(@Ligne,'')<>'NumberOfLogicalProcessors' AND ISNULL(@Ligne,'')<>'' AND ISNUMERIC(ISNULL(@Ligne,''))=0

                                       BEGIN

 

                                              UPDATE #InformationsServeurs

                                              SET

                                                     NumberOfLogicalProcessors=@Valeur

                                              WHERE NomServeurPhysique=@NomServeurPhysique

 

                                              SELECT @NomServeurPhysique=LTRIM(RTRIM(@Ligne)),@Valeur=0

                                

                                       END

                                 IF ISNUMERIC(ISNULL(@Ligne,''))=1

                                       BEGIN

                                              SELECT @Valeur=ISNULL(@Valeur,0) + CAST(RTRIM(LTRIM(@Ligne)) AS INT)

                                       END

 

                                 SELECT @Num_Lig = @Num_Lig + 1

                          END

 

                          UPDATE #InformationsServeurs

                          SET

                                 NumberOfLogicalProcessors=@Valeur

                          WHERE NomServeurPhysique=@NomServeurPhysique               

 

                           /******************************************************/

 

                          ------Traiter les fichiers qui possèdent les informations systèmes

                          TRUNCATE TABLE #ListeFichiersImport

                          TRUNCATE TABLE #FichierImport

      

                          SELECT @Requete =  N'DIR "' + @Chemin_FichierImport + 'informationsSystemes*.txt" /B'

      

                          INSERT INTO #ListeFichiersImport (NomFichier)

                          EXEC Master..xp_cmdshell @Requete

                                

                          SELECT @Num_Lig=1,@Nbre_Lig=COUNT(Num_Lig) FROM #ListeFichiersImport

 

                          WHILE @Num_Lig <= @Nbre_Lig

                                 BEGIN

                                       SELECT @NomFichierImport=NomFichier FROM #ListeFichiersImport WHERE Num_Lig=@Num_Lig     

 

                                       IF ISNULL(@NomFichierImport,'') LIKE 'informationsSystemes%'

                                              BEGIN

                                                    SELECT @Requete = 'TYPE ' + @Chemin_FichierImport + @NomFichierImport

 

                                                    INSERT INTO #FichierImport (Ligne)

                                                    EXEC Master..xp_cmdshell @Requete

                                              END

                   

                                       SELECT @Num_Lig = @Num_Lig + 1

 

                                 END

 

                          --Consolider les informations systèmes

                          SELECT @Num_Lig=1,@Nbre_Lig=COUNT(Num_Lig) FROM #FichierImport

 

                          WHILE @Num_Lig <= @Nbre_Lig

                                 BEGIN

                                       SELECT @Ligne=NULL

                                       SELECT @Ligne=Ligne FROM #FichierImport WHERE Num_Lig=@Num_Lig

                         

                                       IF @Ligne LIKE '%Host%Name:%' OR @Ligne LIKE '%Nom de l%h%te:%'

                                              BEGIN

                                                    SELECT @NomServeurPhysique=RTRIM(LTRIM(SUBSTRING(@Ligne,CHARINDEX(':',@Ligne,0)+1,LEN(@Ligne))))

                                              END

 

                                       IF @Ligne LIKE '%OS%Name:%' OR @Ligne LIKE '%Nom%du%syst%me%exploitation:%'

                                              BEGIN

                                                    UPDATE #InformationsServeurs

                                                    SET

                                                    VersionWindows=RTRIM(LTRIM(SUBSTRING(@Ligne,CHARINDEX(':',@Ligne,0)+1,LEN(@Ligne))))

                                                    WHERE NomServeurPhysique=@NomServeurPhysique

                                              END

 

                                       IF @Ligne LIKE '%System%Manufacturer:%' OR @Ligne LIKE '%Fabricant%du%syst%me:%'

                                              BEGIN

                                                    UPDATE #InformationsServeurs

                                                    SET

                                                           TypeServeur= RTRIM(LTRIM(SUBSTRING(@Ligne,CHARINDEX(':',@Ligne,0)+1,LEN(@Ligne))))

                                                    WHERE NomServeurPhysique=@NomServeurPhysique

                                              END

 

                                       IF (@Ligne LIKE '%Processor(s):%' OR @Ligne LIKE 'Processeur(s):%' ) AND @Ligne NOT LIKE '%OS Build Type:%' 

                                              BEGIN

                                                    UPDATE #InformationsServeurs

                                                    SET

                                                           Processeur= RTRIM(LTRIM(SUBSTRING(@Ligne,CHARINDEX(':',@Ligne,0)+1,LEN(@Ligne))))

                                                    WHERE NomServeurPhysique=@NomServeurPhysique

                                              END

 

                                       IF @Ligne LIKE 'System%Boot%Time:%'

                                              BEGIN

                                                    UPDATE #InformationsServeurs

                                                    SET

                                                           DateDernierDemarrage= RTRIM(LTRIM(SUBSTRING(@Ligne,CHARINDEX(':',@Ligne,0)+1,LEN(@Ligne))))

                                                    WHERE NomServeurPhysique=@NomServeurPhysique

                                              END

 

                                       IF @Ligne LIKE '%Total%Physical%Memory:%' OR @Ligne LIKE 'M%moire%physique%totale:%'

                                              BEGIN

                                                    SELECT @ValeurMemoireGO_V1 = RTRIM(LTRIM(SUBSTRING(@Ligne,CHARINDEX(':',@Ligne,0)+1,LEN(@Ligne))))

 

                                                    SELECT @Nbre_Chaine=LEN(@ValeurMemoireGO_V1),@Num_Chaine=1,@ValeurMemoireGO_V2=''

 

                                                    WHILE @Num_Chaine<=@Nbre_Chaine

                                                           BEGIN

                                                                  IF ISNUMERIC(SUBSTRING(@ValeurMemoireGO_V1,@Num_Chaine,1))=1 AND SUBSTRING(@ValeurMemoireGO_V1,@Num_Chaine,1)<>',' AND SUBSTRING(@ValeurMemoireGO_V1,@Num_Chaine,1)<>'.'

                                                                        BEGIN

                                                                               SELECT @ValeurMemoireGO_V2 = @ValeurMemoireGO_V2 + SUBSTRING(@ValeurMemoireGO_V1,@Num_Chaine,1)

                                                                        END

 

                                                                  SELECT @Num_Chaine = @Num_Chaine + 1

                                                           END

                                

                                                    UPDATE #InformationsServeurs

                                                    SET

                                                           TailleMemoireGO= ROUND(CAST(@ValeurMemoireGO_V2 AS BIGINT)/1023.697916666667,0)

                                                    WHERE NomServeurPhysique=@NomServeurPhysique

                                              END

 

                                       SELECT @Num_Lig = @Num_Lig + 1

 

                                 END

 

                    SELECT @Num_Lig=1,@Nbre_Lig=COUNT(Num_Lig) FROM #InformationsServeurs

 

                    WHILE @Num_Lig<=@Nbre_Lig

                          BEGIN

                                 --SELECT @NbreBases=NULL,

                                 --@NomInstance=NULL,

                                 --@ReplicaRole=NULL,

                                 --@NomServeurPhysique=NULL,

                                 --@Serveur_Lie=NULL

 

                                 SELECT @NomServeurPhysique=NomServeurPhysique, @NomServeurLogique=CASE WHEN NomServeurLogique IS NULL THEN NomServeurPhysique ELSE NomServeurLogique END

                                 FROM #InformationsServeurs WHERE Num_Lig=@Num_Lig

 

                                 SELECT @Serveur_Lie= SRV_NAME FROM #Serveurs_Lies WHERE SRV_NAME LIKE @NomServeurPhysique + '%' OR SRV_NAME LIKE @NomServeurLogique + '%'

 

                                 --Récupérer la version SQL Server

                                 BEGIN TRY

                                       SELECT @Requete     = CASE WHEN @LocalHot<>@Serveur_Lie THEN N'SELECT @VersionSqlServer_Out=VersionSqlServer FROM OPENQUERY(' + QUOTENAME(@Serveur_Lie) + N',''SELECT @@VERSION AS VersionSqlServer'')' ELSE 'SELECT @VersionSqlServer_Out=@@VersionSqlServer' END

                                       EXECUTE sp_executesql @Requete,N'@VersionSqlServer_Out VARCHAR(150) OUTPUT',@VersionSqlServer_Out=@VersionSqlServer OUTPUT

                                 END TRY

                                 BEGIN CATCH

                                       UPDATE #Serveurs_Lies

                                       SET

                                              MessageErreur=ERROR_MESSAGE()

                                       WHERE SRV_NAME LIKE @NomServeurPhysique + '%' OR SRV_NAME LIKE @NomServeurLogique + '%'

                                 END CATCH

 

                                 --Récupérer le nombre de bases

                                 SELECT @Num_Inst=1,@Nbre_Inst=COUNT(SRV_NAME) FROM #Serveurs_Lies

 

                                 WHILE @Num_Inst <= @Nbre_Inst

                                       BEGIN

                                              BEGIN TRY

 

                                                    SELECT @Serveur_Lie= SRV_NAME FROM #Serveurs_Lies

                                                    WHERE (SRV_NAME LIKE @NomServeurPhysique + '%' OR SRV_NAME LIKE @NomServeurLogique + '%'

                                                    OR SRV_DATASOURCE LIKE @NomServeurPhysique + '%' OR SRV_DATASOURCE LIKE @NomServeurLogique + '%')

                                                    AND Num_Lig=@Num_Inst

 

                                                    SELECT @Requete     = CASE WHEN @LocalHot<>@Serveur_Lie THEN N'SELECT @NbreBases_Out=NbreBases,@NomInstance_Out=NomInstance FROM OPENQUERY(' + QUOTENAME(@Serveur_Lie) + N',''SELECT COUNT(name) NbreBases,@@SERVERNAME AS NomInstance FROM sys.databases'')' ELSE 'SELECT @NbreBases=COUNT(name) NbreBases,@@SERVERNAME AS NomInstance FROM sys.databases' END

                                                    EXECUTE sp_executesql @Requete,N'@NbreBases_Out INT OUTPUT,@NomInstance_Out NVARCHAR(150) OUTPUT',@NbreBases_Out=@NbreBases OUTPUT,@NomInstance_Out=@NomInstance OUTPUT

 

                                                    INSERT INTO #Requete (NomServeurPhysique,Requete       )

                                                    VALUES (@NomServeurPhysique,@Requete)

 

                                                    UPDATE #Serveurs_Lies

                                                    SET

                                                            NbreBases=CASE WHEN @NbreBases IS NULL THEN NbreBases ELSE @NbreBases END

                                                           ,ServerName= CASE WHEN @NomInstance IS NULL THEN ServerName ELSE @NomInstance END

                                                    WHERE (SRV_NAME LIKE @NomServeurPhysique + '%' OR SRV_NAME LIKE @NomServeurLogique + '%') AND Num_Lig=@Num_Inst

                                                   

 

                                              END TRY

 

                                              BEGIN CATCH

                                                    UPDATE #Serveurs_Lies

                                                    SET

                                                           MessageErreur=ERROR_MESSAGE()

                                                    WHERE (SRV_NAME LIKE @NomServeurPhysique + '%' OR SRV_NAME LIKE @NomServeurLogique + '%') AND Num_Lig=@Num_Inst

                                              END CATCH

                                                                                                 

                                              SELECT @Num_Inst = @Num_Inst + 1

                                       END

                   

                                 SELECT @NbreBases=SUM(NbreBases) FROM (

                                 SELECT DISTINCT SRV_NAME,NbreBases FROM #Serveurs_Lies WHERE SRV_NAME LIKE @NomServeurPhysique + '%' OR SRV_NAME LIKE @NomServeurLogique + '%'

                                 ) T

            

                                 UPDATE #InformationsServeurs

                                 SET

                                       Environnement=      (SELECT TOP 1 Environnement  FROM [SKYNET].[dbo].[backups_state]

                                                                 WHERE Collect_date>=CAST(DATEADD(DAY,-7,GETDATE()) AS DATE) AND (instance LIKE @NomServeurPhysique + '%' OR instance LIKE @NomServeurLogique +'%' ) )

                                       ,NbreInstanceSQL = (SELECT COUNT(instance) FROM (SELECT DISTINCT instance  FROM [SKYNET].[dbo].[backups_state]

                                                                  WHERE Collect_date>=CAST(DATEADD(DAY,-7,GETDATE()) AS DATE) AND (instance LIKE @NomServeurPhysique + '%' OR instance LIKE @NomServeurLogique +'%' ) ) T1)

                                       ,VersionSqlServer=@VersionSqlServer      

                                       ,NbreBases=@NbreBases

                                       ,ReplicaRole=(SELECT TOP 1 Replica_role FROM [ADMINDBA].[dbo].[vnn_ag_replica_role_base]

                                       WHERE Collect_date>=CAST(DATEADD(DAY,-7,GETDATE()) AS DATE) AND (Availability_replica LIKE @NomServeurPhysique + '%' OR Availability_replica LIKE @NomServeurLogique +'%' ))

                                 WHERE Num_Lig=@Num_Lig

            

                                 SELECT @Num_Lig = @Num_Lig + 1

                          END

 

                          SELECT * FROM #Serveurs_Lies

                          SELECT DISTINCT NomServeurPhysique,Requete FROM #Requete

 

                          IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = OBJECT_ID(N'[tempdb]..[InformationsServeurs]'))

                                 BEGIN

                                       DROP TABLE [tempdb]..[InformationsServeurs]

                                 END

 

                          SELECT * INTO [tempdb]..[InformationsServeurs] FROM #InformationsServeurs

                          ORDER BY        Environnement,NomServeurPhysique,ReplicaRole

 

                          DROP TABLE #ListeFichiersImport

                          DROP TABLE #FichierImport

                          DROP TABLE #InformationsServeurs

                          DROP TABLE #Serveurs_LiesTemp

                          DROP TABLE #Serveurs_Lies

                          DROP TABLE #Requete

 

             END

       ELSE

             BEGIN

                    SELECT DISTINCT [Environnement]

               ,[NomServeurPhysique]

               ,[NomServeurLogique]

               ,[DateDernierDemarrage]

               ,[ReplicaRole]

               ,[TypeServeur]

               ,[VersionWindows]

               ,[VersionSqlServer]

               ,[NbreInstanceSQL]

               ,[NbreBases]

               ,[Processeur]

               ,[NumberOfCores]

               ,[NumberOfLogicalProcessors]

               ,[TailleMemoireGO]

               ,[TailleDisqueGO]

               ,[MessageErreur]

               ,1 AS NumeroServeur                  

                    FROM [tempdb]..[InformationsServeurs]

                    ORDER BY     Environnement,NomServeurPhysique,ReplicaRole

             END

 

 

 

END

 

 

 

 

GO