Get-WmiObject现在弃用了什么?

时间:2018-03-23 15:28:42

标签: powershell

我试图在PowerShell 6.0.2中使用以下函数,但显然已弃用Get-WmiObject。任何人都可以帮我弄清楚如何用替换它的Get-CimInstance替换它吗?

Get-WmiObject位于以下代码的PROCESS区域内。

如有兴趣,请提供完整的功能代码。

function Get-DiskFree
{
[CmdletBinding()]
param
(
    [Parameter(Position=0,
               ValueFromPipeline=$true,
               ValueFromPipelineByPropertyName=$true)]
    [Alias('hostname')]
    [Alias('cn')]
    [string[]]$ComputerName = $env:COMPUTERNAME,

    [Parameter(Position=1,
               Mandatory=$false)]
    [Alias('runas')]
    [System.Management.Automation.Credential()]$Credential =
    [System.Management.Automation.PSCredential]::Empty,

    [Parameter(Position=2)]
    [switch]$Format
)

BEGIN
{
    function Format-HumanReadable
    {
        param ($size)
        switch ($size)
        {
            {$_ -ge 1PB}{"{0:#.#'P'}" -f ($size / 1PB); break}
            {$_ -ge 1TB}{"{0:#.#'T'}" -f ($size / 1TB); break}
            {$_ -ge 1GB}{"{0:#.#'G'}" -f ($size / 1GB); break}
            {$_ -ge 1MB}{"{0:#.#'M'}" -f ($size / 1MB); break}
            {$_ -ge 1KB}{"{0:#'K'}" -f ($size / 1KB); break}
            default {"{0}" -f ($size) + "B"}
        }
    }

    $wmiq = 'SELECT * FROM Win32_LogicalDisk WHERE Size != Null AND DriveType >= 2'
}

PROCESS
{
    foreach ($computer in $ComputerName)
    {
        try
        {
            if ($computer -eq $env:COMPUTERNAME)
            {
                $disks = Get-WmiObject -Query $wmiq `
                         -ComputerName $computer -ErrorAction Stop
            }
            else
            {
                $disks = Get-WmiObject -Query $wmiq `
                         -ComputerName $computer -Credential $Credential `
                         -ErrorAction Stop
            }

            if ($Format)
            {
                # Create array for $disk objects and then populate
                $diskarray = @()
                $disks | ForEach-Object { $diskarray += $_ }

                $diskarray | Select-Object @{n='Name';e={$_.SystemName}},
                    @{n='Vol';e={$_.DeviceID}},
                    @{n='Size';e={Format-HumanReadable $_.Size}},
                    @{n='Used';e={Format-HumanReadable `
                    (($_.Size)-($_.FreeSpace))}},
                    @{n='Avail';e={Format-HumanReadable $_.FreeSpace}},
                    @{n='Use%';e={[int](((($_.Size)-($_.FreeSpace))`
                    /($_.Size) * 100))}},
                    @{n='FS';e={$_.FileSystem}},
                    @{n='Type';e={$_.Description}}
            }
            else
            {
                foreach ($disk in $disks)
                {
                    $diskprops = @{'Volume'=$disk.DeviceID;
                               'Size'=$disk.Size;
                               'Used'=($disk.Size - $disk.FreeSpace);
                               'Available'=$disk.FreeSpace;
                               'FileSystem'=$disk.FileSystem;
                               'Type'=$disk.Description
                               'Computer'=$disk.SystemName;}

                    # Create custom PS object and apply type
                    $diskobj = New-Object -TypeName PSObject `
                               -Property $diskprops
                    $diskobj.PSObject.TypeNames.Insert(0,'BinaryNature.DiskFree')

                    Write-Output $diskobj
                }
            }
        }
        catch
        {
            # Check for common DCOM errors and display "friendly" output
            switch ($_)
            {
                { $_.Exception.ErrorCode -eq 0x800706ba } `
                    { $err = 'Unavailable (Host Offline or Firewall)';
                        break; }
                { $_.CategoryInfo.Reason -eq 'UnauthorizedAccessException' } `
                    { $err = 'Access denied (Check User Permissions)';
                        break; }
                default { $err = $_.Exception.Message }
            }
            Write-Warning "$computer - $err"
        }
    }
}

END {}
}

以下是我在加载该功能后将运行的PowerShell命令,取自此站点:http://binarynature.blogspot.com/2010/04/powershell-version-of-df-command.html

$cred = Get-Credential 'example\administrator'
$servers = 'dc01','db01','exch01','sp01'
Get-DiskFree -Credential $cred -cn $servers -Format |
    ? { $_.Type -like '*fixed*' } |
    select * -ExcludeProperty Type |
    Out-GridView -Title 'Windows Servers Storage Statistics'

1 个答案:

答案 0 :(得分:0)

如EBGreen所述,可以通过将CREATE OR ALTER PROCEDURE dbo.selectBooks AS BEGIN DECLARE @ArchivedBooksAS TABLE ([Books] varchar(100)) INSERT INTO @ArchivedBooks(Books) SELECT name FROM ArchivedBooks IF NOT EXIST (SELECT 0 FROM @ArchivedBooks WHERE Books = @name) BEGIN EXEC dbo.insertBook @name END SELECT id, bookId, name, CASE WHEN @ArchivedBooks.Book IS NULL THEN 'noArchiveBook' ELSE 'archivedBook' END AS Status FROM books LEFT JOIN @ArchivedBooks ON @ArchivedBooks.Book = Books.Book END 更改为Get-WmiObject来解决此问题。该函数中只有两行需要重写:

当前(使用Get-WmiObject)

Get-CimInstance

已更改(使用Get-CimInstance)*

$disks = Get-WmiObject -Query $wmiq -ComputerName $computer -ErrorAction Stop
$disks = Get-WmiObject -Query $wmiq -ComputerName $computer -Credential $Credential -ErrorAction Stop


这是已经进行了这些更改的完整功能(并按我的喜好进行了整理)。我可以确认它可以在PowerShell Core v6.1.2上运行

$disks = Get-CimInstance -Query $wmiq -ComputerName $computer -ErrorAction Stop
$disks = Invoke-Command -ArgumentList $wmiq { param($wmiq) Get-CimInstance -Query $wmiq } -ComputerName $computer -Credential $Credential -ErrorAction Stop | Select-Object DeviceID, DriveType, ProviderName, FreeSpace, Size, VolumeName
相关问题