Get disk properties from the computer

Hi all,

Please use below script to get complete disk details from the endpoint,

Note: Script uses windows “diskpart” tool get details.


import os
import sys
global temp
temp=os.environ['TEMP']
path1=temp+'\\disk.txt'

script='''
list disk
'''
f=open(path1,'w+')
f.write(script)
f.close()
os.chdir(temp)
def showdetails(disk):    
    path2=temp+'\\partitions.txt'    
    script='''
    select disk %s
    detail disk
    '''
    new_script= script % (disk)
    f=open(path2,'w+')
    f.write(new_script)
    f.close()
    os.chdir(temp)
    p=os.popen('diskpart /s "'+path2+'"').readlines()
    parts=0
    for i in range(len(p)):
        if '----------  ---  -----------  -----  ----------  -------  ---------  --------' in p[i]:
            parts=i+1
    if parts==0:
        print 'Disk Details'
        for i in range(0,len(p)):
            print p[i]
        return()
    else:
        partsn=len(p)-parts+1
        for j in range(0,len(p)):
            print p[j]


    out=[]
    new_disk='select disk %s 
' % (disk)
    out.append(new_disk)
    for i in range(partsn):
        out.append('select volume '+str(i)+'
')
        out.append('detail volume 
')  
    path3=temp+'\\details.txt'
    f=open(path3,'w+')
    for i in out:
        f.write(i)
    f.close()
    os.chdir(temp)
    q=os.popen('diskpart /s "'+path3+'"').readlines()
    for i in range(0,len(q)):
        print q[i]



r=os.popen('diskpart /s "'+path1+'"').readlines()

flag=0

for i in range(len(r)):
    if '--------  -------------  -------  -------  ---  ---' in r[i]:
        flag=i+1

diskn=len(r)-flag


if diskn==1:
    showdetails(disk=0)
else:
    for i in range(diskn):
        showdetails(disk=i)




Sample output

20170314-Get-disk-properties-from-the-computer.json (2.48 KB)

Sample output (continue):

disk5.png

hi
nice script.
but may be can include more technical details as well, like powershell:
[“powershell”,“Get-WMIObject -class Win32_DiskDrive”]
[“powershell”,“Get-PhysicalDisk”]
[“powershell”,“Get-PhysicalDisk | Get-StorageReliabilityCounter”]
[“powershell”,“Get-WmiObject -namespace root\wmi –class MSStorageDriver_FailurePredictStatus -ErrorAction Silentlycontinue | Select InstanceName, PredictFailure, Reason”]

and also your another procedure:
os.popen(‘echo list disk > script.txt’).read()
print os.popen(‘diskpart /s script.txt’).read()
os.popen(‘del script.txt’).read()
print os.popen(‘wmic logicaldisk get caption, description, drivetype, providername, volumename’).read()

Hi @phcsolutions

Thank you for sharing your ideas. We will update procedure to include your details

Thanks
Kannan

HI @phcsolutions

I have updated the script, As you requested. Please check it and provide your feedback

It gives the information of disk properties and in detail of disk details of an endpoint

Note: Script uses windows “diskpart” tool get details.



import os
import sys
import time
import ctypes
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)
def diskpart():
 print "*****DISK PROPERTIES*****"
 with disable_file_system_redirection():
            print os.popen("echo list disk |diskpart").read()
            print os.popen('wmic logicaldisk get caption, description, drivetype, providername, volumename').read()
def win32():
      with disable_file_system_redirection():
            print os.popen('powershell "Get-WMIObject -class Win32_DiskDrive"').read()
            print os.popen('powershell "Get-PhysicalDisk"').read()
            print os.popen('powershell "Get-PhysicalDisk | Get-StorageReliabilityCounter"').read()
def showdetails(disk):
 temp=os.environ['TEMP']
 path2=temp+'\\partitions.txt'
 script='''
 select disk %s
 detail disk
  '''
 new_script= script % (disk)
 f=open(path2,'w+')
 f.write(new_script)
 f.close()
 os.chdir(temp)
 p=os.popen('diskpart /s "'+path2+'"').readlines()
 parts=0
 for i in range(len(p)):
  if '----------  ---  -----------  -----  ----------  -------  ---------  --------' in p[i]:
   parts=i+1
 if parts==0:
  print 'Disk Details'
  for i in range(0,len(p)):
   print p[i]
   return()
 else:
  partsn=len(p)-parts+1
  for j in range(0,len(p)):
   print p[j]
 out=[]
 new_disk='select disk %s 
' % (disk)
 out.append(new_disk)
 for i in range(partsn):
  out.append('select volume '+str(i)+'
')
  out.append('detail volume 
')
 path3=temp+'\\details.txt'
 f=open(path3,'w+')
 for i in out:
  f.write(i)
 f.close()
 os.chdir(temp)
 with disable_file_system_redirection():
  q=os.popen('diskpart /s "'+path3+'"').readlines()
  for i in range(0,len(q)):
   print q[i]
def third():
  print" ***** DISK PROPERTIES *****"
  temp=os.environ['TEMP']
  path1=temp+'\\disk.txt'
  script='''
  list disk
  '''
  f=open(path1,'w+')
  f.write(script)
  f.close()
  os.chdir(temp)
  with disable_file_system_redirection():
   r=os.popen('diskpart /s "'+path1+'"').readlines()
  flag=0
  for i in range(len(r)):
   if '--------  -------------  -------  -------  ---  ---' in r[i]:
    flag=i+1
  diskn=len(r)-flag
  if diskn==1:
   showdetails(disk=0)
  else:
   for i in range(diskn):
    showdetails(disk=i)
diskpart()
win32()
third()



Sample Outputs :

20170613-disKproperties.json (3.96 KB)