यहाँ मेरी स्क्रिप्ट कोड अब तक है:

$DynAssembly = New-Object System.Reflection.AssemblyName('SysUtils')
$AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('SysUtils', $False)

$TypeBuilder = $ModuleBuilder.DefineType('Kernel32', 'Public, Class')
$PInvokeMethod = $TypeBuilder.DefinePInvokeMethod('QueryDosDevice', 'kernel32.dll', ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [UInt32], [Type[]]@([String], [Text.StringBuilder], [UInt32]), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
$DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
$SetLastError = [Runtime.InteropServices.DllImportAttribute].GetField('SetLastError')
$SetLastErrorCustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('kernel32.dll'), [Reflection.FieldInfo[]]@($SetLastError), @($true))
$PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
$Kernel32 = $TypeBuilder.CreateType()

$Max = 65536
$StringBuilder = New-Object System.Text.StringBuilder($Max)

Get-WmiObject Win32_Volume | ? { $_.DriveLetter } | Sort-Object DriveLetter | % {
$ReturnLength = $Kernel32::QueryDosDevice($_.DriveLetter, $StringBuilder, $Max)

   if ($ReturnLength)
    {
    $DriveMapping = @{
    DriveLetter = $_.DriveLetter
    VolumeName = $_.Label
    FileSystem = $_.FileSystem
    SerialNumber = $_.SerialNumber
    "Capacity (GB)" = $_.Capacity | Measure-Object -Sum | Foreach {"{0}" -f ([math]::round(($_.Sum / 1GB),2))}
    "FreeSpace (GB)" = $_.FreeSpace | Measure-Object -Sum | Foreach {"{0}" -f ([math]::round(($_.Sum / 1GB),2))}
    DevicePath = $StringBuilder.ToString()
    } 

    New-Object -Type PSObject -Property $DriveMapping | Select-Object DriveLetter, VolumeName, FileSystem, "Capacity (GB)", "FreeSpace (GB)", SerialNumber, DevicePath | Format-Table -AutoSize
    }
}

लेकिन मैं अपने टेबल आउटपुट में एकाधिक शीर्षलेख देखता हूं। ऐसा लगता है कि PSObjects को मर्ज किया जाना चाहिए।

कृपया सहायता करें। धन्यवाद!

0
user3022917 27 जून 2019, 17:29

1 उत्तर

सबसे बढ़िया उत्तर

समस्या यह है कि Foreach को संसाधित करते समय आपके कोड में Format-Table -AutoSize है। इसे केवल आउटपुट पर लागू करें:

$output = 1..4 | %{

    $DriveMapping = @{
        DriveLetter = "DriveLetter$_"
        VolumeName = "Label$_"
        FileSystem = "FileSystem$_"
        SerialNumber = "SerialNumber$_"
        "Capacity (GB)" = "Capacity$_"
        "FreeSpace (GB)" = "FreeSpace$_"
        DevicePath = "StringBuilder$_"
    } 

    New-Object -Type PSObject -Property $DriveMapping | Select-Object DriveLetter, VolumeName, FileSystem, "Capacity (GB)", "FreeSpace (GB)", SerialNumber, DevicePath #| Format-Table -AutoSize
}
$output | Format-Table -AutoSize
1
T-Me 27 जून 2019, 14:47