Sql-Server

通過連結伺服器檢索 LOGINPROPERTY 資訊

  • July 18, 2019

當我在連結伺服器上執行以下查詢時,它會根據結果集在“DaysUntilExpiration”列中返回空值。

SELECT '<link server>' as Instance,
       SL.name AS LoginName 
     ,LOGINPROPERTY (SL.name, 'PasswordLastSetTime') AS PasswordLastSetTime 
     ,LOGINPROPERTY (SL.name, 'DaysUntilExpiration') AS DaysUntilExpiration 
     ,DATEADD(dd, CONVERT(int, LOGINPROPERTY (SL.name, 'DaysUntilExpiration')) 
                , CONVERT(datetime, LOGINPROPERTY (SL.name, 'PasswordLastSetTime'))) AS PasswordExpiration 
     ,SL.is_policy_checked AS IsPolicyChecked 
     ,LOGINPROPERTY (SL.name, 'IsExpired') AS IsExpired 
     ,LOGINPROPERTY (SL.name, 'IsMustChange') AS IsMustChange 
     ,LOGINPROPERTY (SL.name, 'IsLocked') AS IsLocked 
     ,LOGINPROPERTY (SL.name, 'LockoutTime') AS LockoutTime 
     ,LOGINPROPERTY (SL.name, 'BadPasswordCount') AS BadPasswordCount 
     ,LOGINPROPERTY (SL.name, 'BadPasswordTime') AS BadPasswordTime 
     ,LOGINPROPERTY (SL.name, 'HistoryLength') AS HistoryLength 
FROM <link server>.[master].sys.sql_logins AS SL 
WHERE is_expiration_checked = 1 
ORDER BY LOGINPROPERTY (SL.name, 'PasswordLastSetTime') DESC

上述查詢的結果集:

Instance                LoginName                                                                                                                        PasswordLastSetTime                                                                                                                                                                                                                                              DaysUntilExpiration                                                                                                                                                                                                                                              PasswordExpiration      IsPolicyChecked IsExpired                                                                                                                                                                                                                                                        IsMustChange                                                                                                                                                                                                                                                     IsLocked                                                                                                                                                                                                                                                         LockoutTime                                                                                                                                                                                                                                                      BadPasswordCount                                                                                                                                                                                                                                                 BadPasswordTime                                                                                                                                                                                                                                                  HistoryLength

[NLPAGINST11A\NINST11A] dpa                                                                                                                              2018-01-24 08:40:43.970                                                                                                                                                                                                                                          NULL                                                                                                                                                                                                                                                             NULL                    1               0                                                                                                                                                                                                                                                                0                                                                                                                                                                                                                                                                0                                                                                                                                                                                                                                                                1900-01-01 02:00:00.000                                                                                                                                                                                                                                          0                                                                                                                                                                                                                                                                1900-01-01 02:00:00.000                                                                                                                                                                                                                                          0
[NLPAGINST11A\NINST11A] svsql2xdr                                                                                                                        NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                    1               NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL
[NLPAGINST11A\NINST11A] SU_IS_RA                                                                                                                         NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                    1               NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL
[NLPAGINST11A\NINST11A] Obiee                                                                                                                            NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                    1               NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL                                                                                                                                                                                                                                                             NULL

預期結果:(同樣的查詢,不包括連結伺服器詳情,直接在伺服器上執行)

Instance                LoginName                                                                                                                        PasswordLastSetTime                                                                                                                                                                                                                                              DaysUntilExpiration                                                                                                                                                                                                                                              PasswordExpiration      IsPolicyChecked IsExpired                                                                                                                                                                                                                                                        IsMustChange                                                                                                                                                                                                                                                     IsLocked                                                                                                                                                                                                                                                         LockoutTime                                                                                                                                                                                                                                                      BadPasswordCount                                                                                                                                                                                                                                                 BadPasswordTime                                                                                                                                                                                                                                                  HistoryLength

[NLPAGINST11A\NINST11A] savematrix_user_2                                                                                                                2019-07-08 12:55:22.080                                                                                                                                                                                                                                          80                                                                                                                                                                                                                                                               2019-09-26 12:55:22.080 1               0                                                                                                                                                                                                                                                                0                                                                                                                                                                                                                                                                0                                                                                                                                                                                                                                                                1900-01-01 02:00:00.000                                                                                                                                                                                                                                          0                                                                                                                                                                                                                                                                2019-01-09 11:49:26.837                                                                                                                                                                                                                                          4
[NLPAGINST11A\NINST11A] subscriber_2                                                                                                                     2019-07-08 12:26:36.267                                                                                                                                                                                                                                          80                                                                                                                                                                                                                                                               2019-09-26 12:26:36.267 1               0                                                                                                                                                                                                                                                                0                                                                                                                                                                                                                                                                0                                                                                                                                                                                                                                                                1900-01-01 02:00:00.000                                                                                                                                                                                                                                          0                                                                                                                                                                                                                                                                2019-07-15 14:31:05.207                                                                                                                                                                                                                                          4
[NLPAGINST11A\NINST11A] arcsight                                                                                                                         2019-06-07 13:24:40.667                                                                                                                                                                                                                                          49                                                                                                                                                                                                                                                               2019-07-26 13:24:40.667 1               0                                                                                                                                                                                                                                                                0                                                                                                                                                                                                                                                                0                                                                                                                                                                                                                                                                1900-01-01 02:00:00.000                                                                                                                                                                                                                                          0                                                                                                                                                                                                                                                                2019-06-11 16:00:42.043                                                                                                                                                                                                                                          5

是否有可能通過我的查詢獲得通過的天數,或者您有其他方法可以建議嗎?

任何幫助,請!

通過將原始查詢包裝在OPENQUERY.

使用OPENQUERY確保整個查詢(包括LOGINPROPERTY內容)在遠端伺服器上執行。

看看這是否適合你。

SELECT *
FROM openquery(<LinkServer>, 
       '
SELECT ''<LinkServer>'' as Instance,
       SL.name AS LoginName 
     ,LOGINPROPERTY (SL.name, ''PasswordLastSetTime'') AS PasswordLastSetTime 
     ,LOGINPROPERTY (SL.name, ''DaysUntilExpiration'') AS DaysUntilExpiration 
     ,DATEADD(dd, CONVERT(int, LOGINPROPERTY (SL.name, ''DaysUntilExpiration'')) 
                , CONVERT(datetime, LOGINPROPERTY (SL.name, ''PasswordLastSetTime''))) AS PasswordExpiration 
     ,SL.is_policy_checked AS IsPolicyChecked 
     ,LOGINPROPERTY (SL.name, ''IsExpired'') AS IsExpired 
     ,LOGINPROPERTY (SL.name, ''IsMustChange'') AS IsMustChange 
     ,LOGINPROPERTY (SL.name, ''IsLocked'') AS IsLocked 
     ,LOGINPROPERTY (SL.name, ''LockoutTime'') AS LockoutTime 
     ,LOGINPROPERTY (SL.name, ''BadPasswordCount'') AS BadPasswordCount 
     ,LOGINPROPERTY (SL.name, ''BadPasswordTime'') AS BadPasswordTime 
     ,LOGINPROPERTY (SL.name, ''HistoryLength'') AS HistoryLength
FROM [master].sys.sql_logins AS SL 
WHERE is_expiration_checked = 1 
'
   )
ORDER BY lOGINNAME

引用自:https://dba.stackexchange.com/questions/243165