Powershell在控制台而不是对话框中显示Regsvr32结果

时间:2016-05-09 08:03:27

标签: powershell regsvr32

我已经搜索过,但没有找到任何答案 任务是使用Powershell ps1注册一个dll,然后是其他行脚本。我不希望被对话框打断,因此添加了/s参数。但现在结果信息被忽略,无论成功还是失败。 我希望结果显示在控制台中。但是如何?

3 个答案:

答案 0 :(得分:3)

这是一个更完整的完整PowerShell cmdlet,带有管道支持。

function Register-Dll
{
    <#
    .SYNOPSIS
        A function that uses the utility regsvr32.exe utility to register a file
    .PARAMETER Path
        The file path
    .PARAMETER Unregister
        when specified, unregisters instead of registers
    #>
    [CmdletBinding()]
    param (
        [ValidateScript({ Test-Path -Path $_ -PathType 'Leaf' })]       
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipeLineByPropertyName=$true)]
        [Alias("FullName")]
        [string[]]$Path,
        [Alias("u")]
        [switch]$Unregister
        )
    begin {
        if ($Unregister)
        {
            $regflag = "-u "
        }
        else
        {
            $regflag = ""
        }
        [int]$NumFailed=0
        $RegExitCodes = @{
            0="SUCCESS";
            1="FAIL_ARGS - Invalid Argument";
            2="FAIL_OLE - OleInitialize Failed";
            3="FAIL_LOAD - LoadLibrary Failed";
            4="FAIL_ENTRY - GetProcAddress failed";
            5="FAIL_REG - DllRegisterServer or DllUnregisterServer failed.";
        }
    }
    process {
        foreach ($p in $path)
        {
            try
            {
                $regsvrp = Start-Process regsvr32.exe -ArgumentList "/s $regflag <code>$p</code>" -Wait -NoNewWindow -PassThru

                if($regsvrp.ExitCode -ne 0)
                {
                    $NumFailed++
                    Write-Error "regsvr32 $regflag for $p exited with error $($regsvrp.ExitCode) - $($RegExitCodes[$regsvrp.ExitCode])"
                }
            } catch {
                $NumFailed++
                Write-Error $_.Exception.Message
            }
        }
    }
    end {
        if ($NumFailed -gt 0)
        {
            if ($Unregister)
            {
                $mode = "unregister"
            }
            else
            {
                $mode = "register"
            }
            Write-Error "Failed to $mode $NumFailed dll's, see previous errors for detail"
        }
    }
}

用法:

function Register-MyAppDll
{

    param(
        [Parameter(Mandatory=$true,ParameterSetName="Both")]
        [switch]$ReRegister,
        [Parameter(Mandatory=$true,ParameterSetName="UnregisterOnly")]
        [Alias("u")]
        [switch]$UnRegister,
        [Parameter(Mandatory=$true,ParameterSetName="RegisterOnly")]
        [Alias("r")]
        [switch]$Register
    )


    $RegOptions = @()
    if ($UnRegister -or $ReRegister) { $RegOptions += @{Unregister=$true} }
    if ($Register -or $ReRegister) { $RegOptions += @{} }

    $dlltoregister = Get-ChildItem "C:\MyApp\bin" -Filter *.dll | where {$_ -notmatch '^interop'}

    foreach ($RegOpt in $RegOptions)
    {
        $dlltoregister | Register-Dll @RegOpt
    }

}


Register-MyAppDll -UnRegister
Register-MyAppDll -Register
Register-MyAppDll -ReRegister 

享受:)

答案 1 :(得分:2)

使用regsvr32.exe /s启动Start-Process -PassThru并检查ExitCode属性:

$regsvrp = Start-Process regsvr32.exe -ArgumentList "/s C:\path\to\your.dll" -PassThru
$regsvrp.WaitForExit(5000) # Wait (up to) 5 seconds
if($regsvrp.ExitCode -ne 0)
{
    Write-Warning "regsvr32 exited with error $($regsvrp.ExitCode)"
}

答案 2 :(得分:1)

谢谢贾斯汀!我正在使用此脚本,效果很好。

以下代码行中似乎有错字:

$regsvrp = Start-Process regsvr32.exe -ArgumentList "/s $regflag <code>$p</code>" -Wait -NoNewWindow -PassThru

该代码标签不应存在。我将其更改为以下内容,并在路径周围添加了转义的双引号以支持路径中的空格:

$regsvrp = Start-Process regsvr32.exe -ArgumentList "/s $regflag `"$p`"" -Wait -NoNewWindow -PassThru