C:\Windows\System32\shutdown.exe /r
2017年12月13日 星期三
2017年10月15日 星期日
Get mail account from AD at MSSQL
1. Create the linked server
EXEC sp_addlinkedserver @server = 'ADSI',
@srvproduct = 'Active Directory Services 2.5',
@provider = 'ADSDSOObject',
@datasrc = 'adsdatasource'
2. Security
3. SQL Function Script
ALTER FUNCTION [dbo].[GetEmailFromAD]
(
-- Add the parameters for the function here
@pUserID varchar(250)
)
RETURNS varchar(250)
AS
BEGIN
-- Declare the return variable here
DECLARE @pMail varchar(250)
-- Add the T-SQL statements to compute the return value here
SELECT @pMail = mail
FROM OpenQuery (ADSI, '
SELECT displayName,
mail,
sAMAccountName
FROM ''LDAP://...:389/DC=...,DC=...,DC=...''
WHERE objectCategory = ''User''
')
WHERE sAMAccountName = RIGHT(@pUserID,LEN(@pUserID)-CHARINDEX('\',@pUserID))
-- Return the result of the function
RETURN @pMail
END
2017年10月13日 星期五
Declare Table at MSSQL
DECLARE @RowsToProcess int,
@CurrentRow int
--Declare a temp table to stock the search result
DECLARE @TempTable TABLE
(
RowID int not null primary key identity,
DataInt int default 0,
DataNvarchar nvarchar(20) null,
DataBit bit null,
DataDecimal decimal(15,2) default 0fault 0,
ThisYearQuota decimal(15,2) default 0
)
--Insert the search result to @TempTable
INSERT INTO @TempTable
SELECT *
FROM
(
SELECT *
FROM DataTable
) z
--Max(Row)
SET @RowsToProcess = @@ROWCOUNT
--Start Row
SET @CurrentRow = 0
--Process row one by one
WHILE @CurrentRow < @RowsToProcess
BEGIN
SET @CurrentRow = @CurrentRow + 1
--Select single rew data
SELECT * FROM @TempTable WHERE RowID = @CurrentRow
END
2017年10月5日 星期四
JDE Julian Date in SQL
DECLARE @pDate date = GETDATE(),
@pJulianDate int = null,
@pCovDate date = null,
@pJulianYear int = null
--Date to JulianDate
SET @pJulianDate = ((year(@pDate)-1900)*1000)+datepart(dayofyear,@pDate)
--JulianDate to Date
SET @pCovDate = (DateAdd(Day, @pJulianDate%1000 - 1,
CAST(CAST(CAST((@pJulianDate/1000+1900) AS Int)
AS VarChar(4)) + '-01-01 ' AS Date)))
--Get year from JulianDate
SET @pJulianYear = (@pJulianDate/1000) + 1900
SELECT @pDate, @pJulianDate, @pCovDate, @pJulianYear
2017年9月11日 星期一
Open Full Screen Chrome using cmd
The command as below:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --start-fullscreen www.google.com
Close and open IE using cmd
The command as below:
Close IE process
taskkill /f /im "iexplore.exe"
Open IE
"C:\Program Files\Internet Explorer\iexplore.exe"
2017年7月27日 星期四
Using PowerShell Script to get Server/PC Drive Space
公司裡目前沒有server的監控程式,所以需要手動的去監控每個server的空間還夠不夠,但server實在太多了,所以才想用以下簡單的script來把所有server的硬碟資訊彙整到excel。
PowerShell script如下:
gwmi win32_logicalDisk -filter "drivetype = 3" -ComputerName server1, server2, pc1, pc2 |
Select SystemName, deviceid,
@{Name="Free%";Expression={[math]::Round($_.Freespace/$_.Size,2)}},
@{Name="FreeGB";Expression={[math]::Round($_.Freespace/1GB,2)}},
@{Name="SizeGB";Expression={[math]::Round($_.Size/1GB,2)}} |
Sort Free% |
Export-Csv -Path C:\DriveSpace\DrivesSpace-$((Get-Date).ToString('dd-MMM-yyyy_Hmmss')).csv
- 把上列script貼到筆記本,更改server名字,另存成 .ps1檔,就可以執行。
- 結果會匯到C:\DriveSpace (自行更改路徑)存成.csvˇ檔案可以用excel開啟。
- 輸出的freespace及size是以GB為單位。
- 由於要使用Windows Task Scheduler自動執行,所以檔名有加上執行日期及時間,日期格式請自行更換。
用.bat來執行.ps1
script如下:
Powershell.exe -ExecutionPolicy Bypass -File "DriveSpace.ps1"
- 把上列script貼到筆記本,更改.ps1檔名字,另存成 .bat檔,就可以執行。
訂閱:
文章 (Atom)


