有没有办法只从 Get-PSDrive 的 "free" 字段中提取值?

Is there a way to extract only the value from "free" field of Get-PSDrive?

我正在尝试从驱动器中获取空闲 space 的值,这是在数据库服务器中执行自动程序所必需的。

我得到了这个脚本:

    $query_drive_mount_point = @"
    select distinct
    convert(varchar(512), b.volume_mount_point) as [volume],
    convert(varchar(512), b.logical_volume_name) as [logical_volume]
    from sys.master_files as [a]
    CROSS APPLY sys.dm_os_volume_stats(a.database_id, a.[file_id]) as [b]
    "@

        [regex]$get_drive = '\w\:\'
        [regex]$get_drive_name = '\w'
        [regex]$get_drive_space = '\d'
        $mount_point = Invoke-Sqlcmd -ServerInstance "$server_ip$sql_engine,$sql_port" -Username "$sql_user" -Password "$sql_password" -Database Master -Query "$query_drive_mount_point" 

        $get_disk = $get_drive.Matches($mount_point) | Foreach-Object {$_.Value}
        $get_disk_name = $get_drive_name.Matches($get_disk) | Foreach-Object {$_.Value}

        $size_bytes_string = Get-PSDrive $get_disk_name | Select-Object -Property Free
        [int]$size_bytes = $get_drive_space.Matches($size_bytes_string) | ForEach-Object {$_.Value} 
        $size_giga = ( ( ( $size_bytes )/1024 )/1024 )/1024

这段代码运行没有问题,直到这一行:

[int]$size_bytes = $get_drive_space.Matches($size_bytes_string) | ForEach-Object {$_.Value} 

它抛出这个错误:

There was not found overload for "Matches" and args numbers is "1".
En línea: 1 Carácter: 1
+ $size_bytes = $get_drive_space.Matches($size_bytes_string) | ForEach- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

最后一个错误是我翻译的,os是西班牙语。

同样,objective 仅存储免费的 space 值。

如果您有 PS 版本 5:

PS C:\> (($PSVersionTable).PSVersion)

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      18362  145     
  • 如果不是版本 5,请尝试导入存储模块。

您可以使用以下命令行开关更轻松地获取此信息:

Get-disk # the command name
Get-Command -Module storage -Verb get -Noun disk # module

联机代码 select 第一个硬盘(磁盘 0)的大小:

[math]::Round((Get-Disk -Number 0).Size /1GB)

- 编辑:

=========================================================================

如果你加载这个程序集,你可以获得你需要的所有信息,

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$sql = New-Object ('Microsoft.SqlServer.Management.Smo.Server') 

您可以运行此commandlet 来查找您可以获得的信息:

    (($sql | gm) | measure).count 

例如:

    write-host $dbs.Parent
    $dbs=$s.Databases

我相信您需要一个名为 属性 的文件组来获取有关 ldf&mdf 文件的信息。

您需要 select 您需要的 属性 中的值,所以这一行:

$size_bytes_string = Get-PSDrive $get_disk_name | Select-Object -Property Free to 

应改为以下内容:

$size_bytes_string = Get-PSDrive $get_disk_name | Select-Object -ExpandProperty Free

或者您也可以按如下格式设置:

($size_bytes_string = Get-PSDrive $get_disk_name).Free