从Start-Process参数返回值

时间:2017-06-28 16:26:48

标签: powershell

我有一个PowerShell函数(Add-EventLogSource),用于检查事件日志源是否存在。如果它不存在且shell没有升高,我启动一个新的,升高的shell并再次调用该函数。

我似乎无法获得正确的返回值。如果事件日志源不存在,并且我调用Add-EventLogSource,则我不会将返回值一直返回到最初调用Add-EventLogSource的实例。有谁能看到这个问题?代码如下所示:

Function Add-EventLogSource {
Param (
    [Parameter(Mandatory=$True)]
    $EventLogSource
)

    # Check if $EventLogSource exists as a source. If the shell is not elevated and the check fails to access the Security log, assume the source does not exist.
    Try {
        $sourceExists = [System.Diagnostics.EventLog]::SourceExists("$EventLogSource")
    }
    Catch {
        $sourceExists = $False
    }

    If ((([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”)) -AND ($sourceExists -eq $False)) { # Shell is elevated...
        Try {
            New-EventLog –LogName Application –Source $EventLogSource -ErrorAction Stop
        }
        Catch {
            Return "Error"
        }

        Return "Created"
    }
    ElseIf ($sourceExists -eq $False) {
        $return = Start-Process PowerShell –Verb RunAs -ArgumentList "Add-EventLogSource -EventLogSource $EventLogSource; start-sleep 5" -Wait

        Return $return
    }
    Else {
        Return "Exists"
    }
}

感谢。

0 个答案:

没有答案