Best way to check if an PowerShell Object exist?

前端 未结 7 2004
无人及你
无人及你 2020-12-10 10:19

I am looking for the best way to check if a Com Object exists.

Here is the code that I have; I\'d like to improve the last line:

$ie = New-Object -Co         


        
相关标签:
7条回答
  • 2020-12-10 10:42

    What all of these answers do not highlight is that when comparing a value to $null, you have to put $null on the left-hand side, otherwise you may get into trouble when comparing with a collection-type value. See: https://github.com/nightroman/PowerShellTraps/blob/master/Basic/Comparison-operators-with-collections/looks-like-object-is-null.ps1

    $value = @(1, $null, 2, $null)
    if ($value -eq $null) {
        Write-Host "$value is $null"
    }
    

    The above block is (unfortunately) executed. What's even more interesting is that in Powershell a $value can be both $null and not $null:

    $value = @(1, $null, 2, $null)
    if (($value -eq $null) -and ($value -ne $null)) {
        Write-Host "$value is both $null and not $null"
    }
    

    So it is important to put $null on the left-hand side to make these comparisons work with collections:

    $value = @(1, $null, 2, $null)
    if (($null -eq $value) -and ($null -ne $value)) {
        Write-Host "$value is both $null and not $null"
    }
    

    I guess this shows yet again the power of Powershell !

    0 讨论(0)
  • 2020-12-10 10:44

    I had the same Problem. This solution works for me.

    $Word = $null
    $Word = [System.Runtime.InteropServices.Marshal]::GetActiveObject('word.application')
    if ($Word -eq $null)
    {
        $Word = new-object -ComObject word.application
    }
    

    https://msdn.microsoft.com/de-de/library/system.runtime.interopservices.marshal.getactiveobject(v=vs.110).aspx

    0 讨论(0)
  • 2020-12-10 10:45

    In your particular example perhaps you do not have to perform any checks at all. Is that possible that New-Object return null? I have never seen that. The command should fail in case of a problem and the rest of the code in the example will not be executed. So why should we do that checks at all?

    Only in the code like below we need some checks (explicit comparison with $null is the best):

    # we just try to get a new object
    $ie = $null
    try {
        $ie = New-Object -ComObject InternetExplorer.Application
    }
    catch {
        Write-Warning $_
    }
    
    # check and continuation
    if ($ie -ne $null) {
        ...
    }
    
    0 讨论(0)
  • 2020-12-10 10:50

    Type-check with the -is operator returns false for any null value. In most cases, if not all, $value -is [System.Object] will be true for any possible non-null value. (In all cases, it will be false for any null-value.)

    My value is nothing if not an object.

    0 讨论(0)
  • 2020-12-10 10:52

    I would stick with the $null check since any value other than '' (empty string), 0, $false and $null will pass the check: if ($ie) {...}.

    0 讨论(0)
  • 2020-12-10 10:52

    You can also do

    if ($ie) {
        # Do Something if $ie is not null
    }
    
    0 讨论(0)
提交回复
热议问题