Getting the serial number for a PhysicalDrive

I don’t find a solution to get the required info in a single script, but this way will help to find the serial number for the physicaldrive.

  1. run below wmic command for command prompt to get the disk number and serial number
    • WMIC disk drive get name, serial number
  2. run below PowerShell script to get disk number and size. by comparing the size of a disk in file explorer we can get the drive name, then get the serial number from the first step.
    • Function Main {
    • $diskdrives = get-wmiobject Win32_DiskDrive | sort Index
    • $colSize = @{Name=’Size’;Expression={Get-HRSize $_.Size}}
    • foreach ( $disk in $diskdrives ) {
    • $scsi_details = ‘SCSI ‘ + $disk.SCSIBus + ‘:’ +
    • $disk.SCSILogicalUnit + ‘:’ +
    • $disk.SCSIPort + ‘:’ +
    • $disk.SCSITargetID
    • write $( ‘Disk ‘ + $disk.Index + ‘ – ‘ + $scsi_details +
    • ‘ – ‘ + ( Get-HRSize $disk.size) )
    • $part_query = ‘ASSOCIATORS OF {Win32_DiskDrive.DeviceID=”‘ +
    • $disk.DeviceID.replace(‘\’,’\’) +
    • ‘”} WHERE AssocClass=Win32_DiskDriveToDiskPartition’
    • $partitions = @( get-wmiobject -query $part_query |
    • sort StartingOffset )
    • foreach ($partition in $partitions) {
    • $vol_query = ‘ASSOCIATORS OF {Win32_DiskPartition.DeviceID=”‘ +
    • $partition.DeviceID +
    • ‘”} WHERE AssocClass=Win32_LogicalDiskToPartition’
    • $volumes = @(get-wmiobject -query $vol_query)
    • write $( ‘ Partition ‘ + $partition.Index + ‘ ‘ +
    • ( Get-HRSize $partition.Size) + ‘ ‘ +
    • $partition.Type
    • )
    • }
    • function Get-HRSize {
    • [CmdletBinding()]
    • param(
    • [Parameter(Mandatory=$True, ValueFromPipeline=$True)]
    • [INT64] $bytes
    • )
    • process {
    • if ( $bytes -gt 1pb ) { “{0:N2} PB” -f ($bytes / 1pb) }
    • elseif ( $bytes -gt 1tb ) { “{0:N2} TB” -f ($bytes / 1tb) }
    • elseif ( $bytes -gt 1gb ) { “{0:N2} GB” -f ($bytes / 1gb) }
    • elseif ( $bytes -gt 1mb ) { “{0:N2} MB” -f ($bytes / 1mb) }
    • elseif ( $bytes -gt 1kb ) { “{0:N2} KB” -f ($bytes / 1kb) }
    • else { “{0:N} Bytes” -f $bytes }
    • }
    • }
    • Main

get the backup drive info for remote servers using powershell

step 1: save this below tsql as backup_drive_details.sql file

SELECT
distinct left(physical_device_name,1) AS Backup_Drive,@@SERVERNAME
FROM msdb.dbo.backupset JOIN msdb.dbo.backupmediafamily
ON(backupset.media_set_id=backupmediafamily.media_set_id)
group by physical_device_name

step 2: replace the file paths with yours in below powershell block

$servers = Get-Content “C:\Users\yogi\Desktop\serverlist.txt”

foreach ($ser in $servers)
{
$a =Invoke-Sqlcmd -inputfile “C:\Users\yogi\Desktop\backup_drive_details.sql” -serverinstance $ser | out-file -FilePath “C:\Users\S74697580WUS\Desktop\backup_drive_details.txt” -Append

}

Get the instant file initialization status using Powershell

TSQL:

SELECT @@SERVERNAME AS [Server Name] ,
— RIGHT(@@version, LEN(@@version) – 3 – CHARINDEX(‘ ON ‘, @@VERSION)) AS [OS Info] ,
— LEFT(@@VERSION, CHARINDEX(‘-‘, @@VERSION) – 2) + ‘ ‘
— + CAST(SERVERPROPERTY(‘ProductVersion’) AS NVARCHAR(300)) AS [SQL Server Version] ,
service_account ,
instant_file_initialization_enabled
FROM sys.dm_server_services
WHERE servicename LIKE ‘SQL Server (%’

  1. save above Tsql as ifiq.sql
  2. save the server names in a text file as serverlist.txt
  3. make sure all paths are updated and run below script in powershell

$servers = Get-Content “C:\Users\Desktop\serverlist.txt”

$output =foreach ($ser in $servers)
{
Invoke-Sqlcmd -inputfile “C:\Users\Desktop\ifiq.sql” -serverinstance $ser

}

$output | format-table |out-file -FilePath “C:\Users\Desktop\ifiq.txt” -Append