Disk Properties of All Computers (Powershell Get-PhysicalDisk) in a Shared CSV File

Please use the script to get disk properties of each endpoint into single CSV file, collection of outputs to a CSV file. The shared location should be read and written by everyone.

Please note that the procedure will support only Windows 8 and above now.

Steps:

  • Accesses the Shared Folder with given Username and Password
  • Runs the Powershell Command "Get-PhysicalDisk" in the Endpoint
  • Creates a File "DiskProperties.csv" and Writes the output if the CSV file is not exist
  • If exists, Appends the output to the File "DiskProperties.csv"
Instructions: Please update the valid data for the given variable, sharedpath should be updated surrounded by r'networkpath' sharedpath=r'\\hostname\path' user='username' pwd='password'

Please run the script as System User or Logged In User (If the Administrator logged in only)

sharedpath=r'\\hostname\path'
user='username'
pwd='password'

import os
import socket
import time
n=socket.gethostname()
ip=socket.gethostbyname(n)
def ecmd(cmd, r=False):
    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)
    from subprocess import PIPE, Popen
    with disable_file_system_redirection():
        OBJ = Popen(cmd, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = OBJ.communicate()
    ret=OBJ.returncode
    if r:
        if ret==0:
            return out.strip()
        else:
            return err.strip()
    else:
        return ret

enableshare='net use %s %s /user:%s /persistent:no'%(sharedpath, pwd, user)
disableshare='net use %s /delete'%sharedpath
es=ecmd(enableshare)
time.sleep(10)
if es==0:
    c=['ComputerName', 'IPAddress', 'MediaType', 'FriendlyName', 'SerialNumber', 'CanPool', 'OperationalStatus', 'HealthStatus', 'Usage', 'Size']
    c1=[]
    c1.append(n)
    c1.append(ip)
    for i in c[2:]:
        c1.append(ecmd('powershell (get-physicaldisk).%s'%i, True))
    pf=os.path.join(sharedpath, 'DiskProperties.csv')
    if os.path.isfile(pf):
        with open(pf, 'a') as fa:
            text=''
            for a in c1:
                if a==c1[-1]:
                    text+='%s GB'%str(round(float(a)/(1024*1024*1024)))
                else:
                    text+='%s,'%a
            fa.write('%s
'%text)
    else:
        with open(pf, 'w') as fw:
            header=''
            for h in c:
                if h==c[-1]:
                    header+='%s'%h
                else:
                    header+='%s,'%h
            fw.write('%s
'%header)
            text=''
            for a in c1:
                if a==c1[-1]:
                    text+='%s GB'%str(round(float(a)/(1024*1024*1024)))
                else:
                    text+='%s,'%a
            fw.write('%s
'%text)
    ecmd(disableshare)
    print pf
    print '
'
    with open(pf) as fr:
        print fr.read()
else:
    raise Exception(es, sharedpath)

Script in JSON:
20170613-Disk-Properties-of-All-Computers.json

Execution Logs:

\W81P64\Comodo1234567890\DiskProperties.csv

ComputerName,IPAddress,MediaType,FriendlyName,Seri alNumber,CanPool,OperationalStatus,HealthStatus,Us age,Size
W10H64,10.108.51.149,UnSpecified,VBOX HARDDISK,VB0e32b64a-8bad3b3e,False,OK,Healthy,Auto-Select,35.0 GB
W10P64,10.108.51.187,UnSpecified,VBOX HARDDISK,VB6056d23d-9817c4eb,False,OK,Healthy,Auto-Select,35.0 GB

After completing all execution, you can check the given network path, there will be a file DiskProperties.csv waiting for you.

20170613-Disk-Properties-of-All-Computers.json (3.58 KB)

I keep getting an error reporting: For some reason the Path seems to be the issue but I don’t know why, it is written exactly as in the example and the path exists with “Everyone” sharing permissions… Please help!

Traceback (most recent call last): File "<string>", line 77, in <module> Exception: (1, '\\\\TLC-ITDEPT\\Reports from RMM')

Hi @jessie ,

Thanks for letting us know. Can you please share the error you receive so that our developer team can investigate the issue.

Kind Regards,
PremJK

@jessie ,

Our Scripts Team has checked the errors you mentioned. Please see recommendation below:

Folder Name Should not Contain Spaces: Reports from RMM.

Replace spaces with Special Characters or Remove Spaces

Correct Way : \TLC-ITDEPT\Reports_from_RMM