0

Iam trying to write a script to notify if a VMware VM Custom attribute has a value or if the value is null. I need the VM name and the Output Value (either Null or Not Null). Here is what I have but doesn't return the accurate information

$vms = Get-VM

foreach ($vm in $vms) {

    $tag = $vm | Get-Annotation -CustomAttribute "Backup"
   
    if ($tag.value -eq '$null'){
        Write-Output "$vm Attribute doesnt have a value"
    }
    else {
        Write-Output "$vm Attribute has a value assigned"
    }
}
4

3 回答 3

2

除非您专门寻找文字字符串 value '$null',否则您可能希望将比较更改为$null -eq $tag.value

您可以创建一个具有 2 个属性的新对象:

$vms = Get-VM

foreach ($vm in $vms) {

    $tag = $vm | Get-Annotation -CustomAttribute "Backup"
   
    if ($null -eq $tag.value){
        $result = "$vm Attribute doesnt have a value"
    }
    else {
        $result = Write-Output "$vm Attribute has a value assigned"
    }

    # output object with Name + result
    [pscustomobject]@{
        Name = $vm.Name
        Result = $result
    }
}

另一种可能更符合 PowerShell 习惯的方法是使用Select-Objectcmdlet 创建一个类似的对象:

Get-VM |Select-Object Name,@{Name='HasBackupAttribute';Expression={ $null -eq ($_ | Get-Annotation -CustomAttribute "Backup").Value }}
于 2021-03-09T00:53:23.603 回答
0

注释值是字符串。当不存在值时,字符串被认为是空的而不是 null。所以$null -eq value测试不会产生预期的结果。您可以简单地执行该值的隐式布尔转换以确定它是否为空。

$vms = Get-VM
foreach ($vm in $vms) {

    $tag = $vm | Get-Annotation -CustomAttribute "Backup"
    # Empty string returns false. Nonempty string returns true.
    if ($tag.value){
        Write-Output "$vm Attribute has a value assigned"
    }
    else {
        Write-Output "$vm Attribute doesn't have a value"
    }
}

你会发现$tag.value | Get-Member返回一个 type System.String。所以value看似没有价值的时候,其实是一个空字符串。您可以执行各种测试来确定该值是否为空。if语句中的空字符串值计算为False。请参阅下面的一些示例,这些示例都可以在if语句中使用。

$Empty = [pscustomobject]@{value = ''}
$NotEmpty = [pscustomobject]@{value = 'My String'}

# Test 1 Using IsNullOrEmpty static method
[string]::IsNullOrEmpty($Empty.value)
True
[string]::IsNullOrEmpty($NotEmpty.value)
False

# Test 2 Using [bool] type accelerator
[bool]$Empty.value
False
[bool]$NotEmpty.value
True

# Test 3 Using length property. Empty string has length of 0.
$Empty.value.length -eq 0
True
$NotEmpty.value.length -eq 0
False

# Test 4 Using if statement. ! in front of an expression negates its boolean value
if ($Empty.value) { "Value is not empty" } else { "Value is empty" }
Value is empty
if ($NotEmpty.value) { "Value is not empty" } else { "Value is empty" }
Value is not empty
if (!$Empty.value) { "Value is empty" }
Value is empty
于 2021-03-09T16:05:34.177 回答
0

在处理 $null 比较时,请参阅此 SO Q&A:
在 PowerShell 中,为什么 $null -lt 0 = $true?那可靠吗?

我目前没有 VMware,但在我的小型 Hyper-V 实验室中,运行以下命令会提供显示的结果:

Try
{
    (
        Get-VM | 
        Where-Object -Property FloppyDrive -eq $null | 
        Select Name, FloppyDrive -ErrorAction SilentlyContinue
    ).Count
}
Catch { Write-Warning -Message 'No no records returned'}
# Results
<#
4
#>

Try
{
    (
        Get-VM | 
        Where-Object -Property FloppyDrive -ne $null | 
        Select Name, FloppyDrive -ErrorAction SilentlyContinue
    ).Count
}
Catch { Write-Warning -Message 'No no records returned'}
# Results
<#
WARNING: No no records returned
#>

使用这些结果也是一样的。

Try
{
    (
        Get-VM | 
        Where-Object -Property FloppyDrive -Match $null | 
        Select Name, FloppyDrive -ErrorAction SilentlyContinue
    ).Count
}
Catch { Write-Warning -Message 'No no records returned'}

Try
{
    (
        Get-VM | 
        Where-Object -Property FloppyDrive -NotMatch $null | 
        Select Name, FloppyDrive -ErrorAction SilentlyContinue
    ).Count
}
Catch { Write-Warning -Message 'No no records returned'}

在您的用例中,请尝试以下重构:

(Get-VM).Name | 
foreach {
    if ((Get-VM -Entity $PSitem | Get-Annotation -CustomAttribute 'Backup') -eq $null)
    { "$PSItem - Attribute doesnt have a value"    }
    else {"$PSItem - Attribute has a value assigned"}
}

或者...

    (Get-VM).Name | 
foreach {
    if ((Get-VM -Entity $PSitem | Get-Annotation -CustomAttribute 'Backup') -Match $null)
    { "$PSItem - Attribute doesnt have a value"    }
    else {"$PSItem - Attribute has a value assigned"}
}
于 2021-03-09T07:47:59.290 回答