好的,所以我有以下 Windows 批處理腳本,它可以為所有連接的存盤設備(內部硬碟驅動器/外部 USB 驅動器)提取資訊,它作業得很好。但是,我想在它說的后面添加每個檢測到的驅動器的名稱:Volume x:\像這樣:Volume x:\ NAME OF HDD HERE。
call :setESC
call :hdd-info
endlocal
echo. Press any key to exit &>nul timeout /t -1 &exit /B
REM - Setup ANSI Escape Character ------------------------------------------------
:setESC
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set ESC=%%b
exit /B 0
)
REM ------------------------------------------------------------------------------
:: - Get Storage / HDD Info ------------------------------------------------------
:hdd-info
:: Enable use of Unicode symbols
chcp 65001 >nul
set "GB=1073741824"
for /f "skip=1 delims=" %%i in ('%SystemRoot%\System32\wbem\wmic.exe logicaldisk get deviceid^,freespace^,size^') do (for /f "tokens=1-3" %%j in ("%%i") do call :output %%j %%k %%l)
:: Disable Unicode symbols
chcp 1252 >nul
goto :eof
:output
if "%3"=="" (
:: echo. Volume %optical_drive_letter%\ - %optical_drive_type% ^(%optical_drive_name%^)
goto :eof
)
for /f "tokens=1-4" %%i in (
'mshta vbscript:Execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(FormatNumber(%3/%GB%, 2) & "" "" & FormatNumber((%3-%2)/%GB%, 2) & "" "" & FormatNumber(%2/%GB%, 2) & "" "" & Round((%3-%2)*50/%3)):Close"^)'
) do (
set "size=%%i"
set "used=%%j"
set "free=%%k"
set /a "nUsed=%%l, nFree=50-%%l"
)
echo( Volume %1\
echo( Total Size : %size:~-10%GB
echo( Used Space : %used:~-10%GB
echo( Free Space : %free:~-10%GB
for /l %%i in (1 1 %nUsed%) do <nul set /p "=%ESC%[91m▓%ESC%[30m"
for /l %%i in (1 1 %nFree%) do <nul set /p "=%ESC%[92m▓%ESC%[30m"
echo( &echo(
goto :eof
:: ------------------------------------------------------------------------------
輸出如下:https : //imgshare.io/image/list-hdds.rN55xP
現在,我還有以下內容顯示光驅作為我需要的布局/資訊的示例。IE:在卷標后添加的設備名稱。
請注意 此代碼僅作為示例。
:: - Get Optical Drive(s) Information -----------------------------------------------
for /f "skip=2 tokens=2,3 delims=," %%i in (
'%SystemRoot%\System32\wbem\wmic.exe logicaldisk where "drivetype=5" get Caption^ /format:csv'
) Do (
set "optical_drive_letter=%%i"
)
for /f "tokens=2,3 delims=," %%a in (
'%SystemRoot%\System32\wbem\wmic.exe cdrom where "mediatype!=11" get name^,mediatype /format:csv'
) Do (
set "optical_drive_type=%%a"
set "optical_drive_name=%%b"
)
::echo( Volume %optical_drive_letter%\
::echo( %optical_drive_description%/%optical_drive_type% (%optical_drive_name%)
:: ------------------------------------------------------------------------------
echo( Volume %optical_drive_letter%\
echo( %optical_drive_type% (%optical_drive_name%)
echo(
因此,為了澄清上述內容,與初始腳本一樣顯示卷標簽。但是,它也顯示設備名稱。
https://imgshare.io/image/list-optical.rN5bbu
I know I can use wmic.exe logicaldisk get Caption to get this in CMD so can use something like this in a batch-file:
for /f "delims=^= tokens=2" %%a in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_DiskDrive GET Caption /format:list ^| findstr ^=') do set logicaldisk_name=%%a
My issue is that I do not know how to alter the original script to also get and show each devices name for each physical drive that is detected.
Can anyone please help with this as I seem to be pulling my hair out trying to make this work for all connected drives?
Full script can be found here: https://pastebin.com/q5QS4EmF
Many thanks,
Bri
uj5u.com熱心網友回復:
不啟動WMIC.exe, 和mshta.exe, (呼喚VBScript引擎);并結合powershell.exe其他答案中提供的更好的內容,這是一個示例批處理檔案 你可以玩,或學習。
@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile ^
"Get-CimInstance -ClassName Cim_Diskdrive -PipelineVariable Disk | "^
"ForEach-Object { $_ | Get-CimAssociatedInstance -ResultClassName Cim_DiskPartition -PipelineVariable Partition } | "^
"ForEach-Object { $_ | Get-CimAssociatedInstance -ResultClassName Cim_LogicalDisk } | "^
"Format-Table @{ Label = 'Letter'; Expression = { $_.DeviceId } }, "^
" @{ Label = 'Model'; Expression = { $Disk.Model.Trim() } }, "^
" @{ Label = 'Capacity'; Expression = { '{0:F0} GB' -F ($Disk.Size / 1000000000) } }, "^
" @{ Label = 'Partition'; Expression = { $Partition.Index } }, "^
" @{ Label = 'Label'; Expression = { $_.VolumeName } }, "^
" @{ Label = 'Size'; Expression = { '{0:F1} GiB' -F ($_.Size / 1GB) } }, "^
" @{ Label = 'Used'; Expression = { '{0:F1} GiB' -F (($_.Size - $_.FreeSpace) / 1GB) } }, "^
" @{ Label = 'FreeSpace'; Expression = { '{0:F1} GiB' -F ($_.FreeSpace / 1GB) } }, "^
" @{ Label = 'PercentFree'; Expression = { '{0:P1}' -F ($_.FreeSpace / $_.Size) } } -AutoSize"
@Pause
我知道您希望得到答案,您可以將這些答案合并到您發布的代碼提交中,但是為了獲得您需要的結果,您的整個腳本需要重新撰寫,并且生成的代碼會明顯變慢,更多復雜,很難理解和適應。
uj5u.com熱心網友回復:
如果我正確理解了您的問題,您想確定所有連接的邏輯磁盤的驅動器號、卷名和磁盤使用屬性。
- 打開 PowerShell ISE。
- 將以下代碼粘貼到腳本窗格中。
- 按播放(綠色箭頭)。
$DriveInformation = Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object DeviceID, VolumeName, Description, @{'Name' = 'Total Size (GB)'; Expression = {[int]($_.Size/1GB)}}, @{'Name' = 'Used (GB)'; Expression = {[int](($($_.Size)-$($_.FreeSpace))/1GB)}}, @{'Name' = 'Available (GB)'; Expression = {[int]($_.FreeSpace/1GB)}}
$DriveInformation
獎勵
如果您想將其匯出到檔案,您可以通過管道將 $DriveInformation 的結果轉換為特定格式。示例如下;
$DriveInformation | Export-Csv "C:\temp\driveinformation.csv" -NoTypeInformation -Force
Start-Process "C:\temp\driveinformation.csv"
Edit1:
我應該注意,您可以將腳本另存為 .ps1,它可以像批處理檔案一樣運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369564.html
