Powershell 现在让WmiObject弃用什么?
我正在尝试让以下函数在PowerShell 6.0.2中工作,但显然get-WMIOObject已被弃用。有人能帮我找出如何用替换它的Get-CimInstance替换它吗 Get-WmiObject位于下面代码的进程区域内 如果有人感兴趣,请提供完整的功能代码 函数获取磁盘空闲 { [CmdletBinding] param [ParameterPosition=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true] [别名'hostname'] [别名'cn'] [string[]$ComputerName=$env:ComputerName, [ParameterPosition=1, 必填项=$false] [别名'runas'] [系统.管理.自动化.凭证]$Credential= [系统.管理.自动化.PSCredential]::空, [参数位置=2] [开关]$格式 开始 { 函数格式可读 { 参数$大小 开关$size { {${0:.'P'}-f$size/1PB;break} {${0:.'T'}-f$size/1TB;break} {${0:.'G'}-f$size/1GB;break} {${0:.'M'}-f$size/1MB;break} {${0:'K'}-f$size/1KB;break} 默认值{0}-f$size+B} } } $wmiq='从Win32_LogicalDisk中选择*,其中大小!=Null,驱动器类型>=2' } 过程 { foreach$ComputerName中的$computer { 尝试 { if$computer-eq$env:COMPUTERNAME { $disks=获取WmiObject-查询$wmiq` -ComputerName$computer-错误操作停止 } 其他的 { $disks=获取WmiObject-查询$wmiq` -ComputerName$计算机-凭证$凭证` -错误动作停止 } if$格式 { 为$disk对象创建阵列,然后填充 $diskarray=@ $disks | ForEach对象{$diskarray+=$\u0} $diskarray | Select Object@{n='Name';e={$\系统名}, @{n='Vol';e={$\设备ID}, @{n='Size';e={Format HumanReadable$\ux.Size}, @{n='Used';e={Format HumanReadable` $\.Size-$\.FreeSpace}, @{n='Avail';e={Format HumanReadable$\ u0.FreeSpace}, @{n='Use%';e={[int]$\u0.Size-$\u0.FreeSpace` /$\大小*100}, @{n='FS';e={$\文件系统}, @{n='Type';e={$\u.Description} } 其他的 { foreach$磁盘,单位为$磁盘 { $diskprops=@{'Volume'=$disk.DeviceID; 'Size'=$disk.Size; 'Used'=$disk.Size-$disk.FreeSpace; “可用”=$disk.FreeSpace; “FileSystem”=$disk.FileSystem; “Type”=$disk.Description “计算机”=$disk.SystemName;} 创建自定义PS对象并应用类型 $diskobj=新对象-类型名称PSObject` -属性$diskprops $diskobj.PSObject.TypeNames.Insert0,'BinaryNature.DiskFree' 写入输出$diskobj } } } 接住 { 检查常见的DCOM错误并显示友好输出 开关$_ { {$\异常.ErrorCode-eq 0x800706ba}` {$err='不可用的主机脱机或防火墙'; 中断;} {$\.CategoryInfo.Reason-eq'UnauthorizedAccessException'}` {$err='拒绝访问检查用户权限'; 中断;} 默认值{$err=$\异常.Message} } 写入警告$computer-$err } } } 结束{} } 以下是加载函数后将运行的PowerShell命令,这些命令取自此站点:Powershell 现在让WmiObject弃用什么?,powershell,Powershell,我正在尝试让以下函数在PowerShell 6.0.2中工作,但显然get-WMIOObject已被弃用。有人能帮我找出如何用替换它的Get-CimInstance替换它吗 Get-WmiObject位于下面代码的进程区域内 如果有人感兴趣,请提供完整的功能代码 函数获取磁盘空闲 { [CmdletBinding] param [ParameterPosition=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true]
正如EBGreen所说,这可以通过将Get WmiObject更改为Get CimInstance来解决。该函数中只有两行需要重写: 当前使用Get WmiObject 使用Get-CimInstance进行更改* 这里是完整的功能与这些变化已经作出和清理了一点,我喜欢。我可以确认它正在PowerShell Core v6.1.2上运行
function Get-DiskFree {
[CmdletBinding()]
param (
[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('cn')]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Position=1, Mandatory=$false)]
[Alias('cr')]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Position=2)]
[Alias('f')]
[switch]$Format
)
begin {
$ErrorActionPreference = "Stop"
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-CimInstance -Query $wmiq -ComputerName $computer
}
else {
$disks = Invoke-Command -ArgumentList $wmiq { param($wmiq) Get-CimInstance -Query $wmiq } -ComputerName $computer -Credential $Credential `
| Select-Object DeviceID, DriveType, ProviderName, FreeSpace, Size, VolumeName
}
if ($Format) {
# Create array for $disk objects and then populate
$diskarray = @()
$disks | ForEach-Object { $diskarray += $_ }
$diskarray | Select-Object
@{Name='Name'; Expression={$_.SystemName}},
@{Name='Vol'; Expression={$_.DeviceID}},
@{Name='Size'; Expression={Format-HumanReadable $_.Size}},
@{Name='Used'; Expression={Format-HumanReadable (($_.Size)-($_.FreeSpace))}},
@{Name='Avail'; Expression={Format-HumanReadable $_.FreeSpace}},
@{Name='Use%'; Expression={[int](((($_.Size)-($_.FreeSpace))/($_.Size) * 100))}},
@{Name='FS'; Expression={$_.FileSystem}},
@{Name='Type'; Expression={$_.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 {
}
}
Get-WMIObject仍然可以正常工作,并将在可预见的未来继续工作。您已经说过,您的脚本应该可以正常工作,只需将Get-WMIObject更改为Get-CimInstance,将Win32_LogicalDisk更改为CIM_LogicalDisk。@EBGreen-我不确定类名是否会更改;你能指出这一事实的任何文档吗?好吧,运行代码会告诉你…我 如果您想从马口中获得它,只需打开powershell并键入:Get-CIMInstance*logicalDisk*
$disks = Get-WmiObject -Query $wmiq -ComputerName $computer -ErrorAction Stop
$disks = Get-WmiObject -Query $wmiq -ComputerName $computer -Credential $Credential -ErrorAction Stop
$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
function Get-DiskFree {
[CmdletBinding()]
param (
[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('cn')]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Position=1, Mandatory=$false)]
[Alias('cr')]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Position=2)]
[Alias('f')]
[switch]$Format
)
begin {
$ErrorActionPreference = "Stop"
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-CimInstance -Query $wmiq -ComputerName $computer
}
else {
$disks = Invoke-Command -ArgumentList $wmiq { param($wmiq) Get-CimInstance -Query $wmiq } -ComputerName $computer -Credential $Credential `
| Select-Object DeviceID, DriveType, ProviderName, FreeSpace, Size, VolumeName
}
if ($Format) {
# Create array for $disk objects and then populate
$diskarray = @()
$disks | ForEach-Object { $diskarray += $_ }
$diskarray | Select-Object
@{Name='Name'; Expression={$_.SystemName}},
@{Name='Vol'; Expression={$_.DeviceID}},
@{Name='Size'; Expression={Format-HumanReadable $_.Size}},
@{Name='Used'; Expression={Format-HumanReadable (($_.Size)-($_.FreeSpace))}},
@{Name='Avail'; Expression={Format-HumanReadable $_.FreeSpace}},
@{Name='Use%'; Expression={[int](((($_.Size)-($_.FreeSpace))/($_.Size) * 100))}},
@{Name='FS'; Expression={$_.FileSystem}},
@{Name='Type'; Expression={$_.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 {
}
}