用参数运行exe并捕获退出代码的脚本

时间:2014-06-02 15:27:11

标签: powershell

我有一个支持在无人值守模式下运行的导入工具。它接受这样的论点:

importer.exe -organization“DEV”-dataFile“E:\ importData.txt”-rightsFile“E:\ importRights.txt”-logFile“C:\ LogFile.log”

现在上面是工具本身如何接受参数。

我正在编写ps脚本以使用上述参数启动该工具。

<#
    .SYNOPSIS
    Executes the PWR Bulk Data Importer tool in unattended mode.

    .DESCRIPTION
    This script executes the PWR Bulk Data Importer tool in unattended mode. The Import files: Data and rights must be supplied and log file must also be provided.

    .PARAMETER ImportToolExe
    The full path and exe of the tool.

    .PARAMETER Organization
    The identifier of the organization.

    .PARAMETER DataFile
    The full path and filename of data file.

    .PARAMETER RightsFile
    The full path and filename of rights file.

    .PARAMETER LogFile
    The full path and filename of log file (will be created and if already exists, it'll be over-writtien).

    .PARAMETER IsForced
    If true, tool will run in override mode omitting all deletion warnings.

    .EXAMPLE
    Executer -ImportToolExe "D:\tool\PWR Bulk Data Importer.exe" -Organization "VTDEV" -DataFile "E:\importData.txt" -RightsFile "E:\importRights.txt" -LogFile "C:\LogFile.log"
    Executer -ImportToolExe "D:\tool\PWR Bulk Data Importer.exe" -Organization "VTDEV" -DataFile "E:\importData.txt" -RightsFile "E:\importRights.txt" -LogFile "C:\LogFile.log" -IsForced true
#>

param(
    [Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)]
    [string]$ImportToolExe,

    [Parameter(Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = $true)]
    [string]$Organization,

    [Parameter(Mandatory = $true, Position = 2, ValueFromPipelineByPropertyName = $true)]
    [string]$DataFile,

    [Parameter(Mandatory = $true, Position = 3, ValueFromPipelineByPropertyName = $true)]
    [string]$RightsFile,

    [Parameter(Mandatory = $true, Position = 4, ValueFromPipelineByPropertyName = $true)]
    [string]$LogFile,

    [Parameter(Mandatory = $false)]
    [bool]$IsForced
)

Write-Output ""
Write-Output "Script to execute Bulk Data Importer"
Write-Output ""

$params = "-organization " + $Organization + " -dataFile " + $DataFile + " -rightsFile " + $RightsFile + " -logFile " + $LogFile

Write-Output "Debuging"
Write-Output ($ImportToolExe + " " + $params)

Try
{
    Write-Output " "
    Write-Output "Executing..."
    Invoke-Expression ($ImportToolExe + " " + $params)

    Write-Output "Finished."
    Write-Output "Checking exit code."    

}
Catch [system.exception]
{
    " "
    "Exception while trying to execute"
    Write-Output $_.Exception.GetType().FullName; 
    Write-Output $_.Exception.Message;
    return
}
Finally
{
    Write-Output " "
}

$IsImportSuccess = $false

IF ($lastexitcode -eq 0)
{
    Write-Output "Import successful."
    $IsImportSuccess = $true
}
ELSE
{
    Write-Output "Import failed."
    $IsImportSuccess = $false
}

IF ($IsImportSuccess -eq $true)
{
    Try
    {
        $SMTPServer = "smtp.gmail.com" 
        $SMTPClient = New-Object Net.Mail.SMTPClient( $SmtpServer, 587 )  
        $SMTPClient.EnableSSL = $true 
        $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( "GMAIL_USERNAME", "GMAIL_PASSWORD" ); 


        $emailMessage = New-Object System.Net.Mail.MailMessage
        $emailMessage.From = $EmailFrom
        foreach ( $recipient in $Arry_EmailTo )
        {
            $emailMessage.To.Add( $recipient )
        }
        $emailMessage.Subject = $EmailSubj
        $emailMessage.Body = $EmailBody

        # Do we have any attachments?
        # If yes, then add them, if not, do nothing
        # if ( $Arry_EmailAttachments.Count -ne $NULL ) 
        # {
        #   $emailMessage.Attachments.Add()
        # }

        $emailMessage.Attachments.Add($LogFile)

        $SMTPClient.Send( $emailMessage )
    }
    Catch [system.exception]
    {
        " "
        "Exception while emailing"
        write-host $_.Exception.GetType().FullName; 
        write-host $_.Exception.Message;
        return
    }
}

不,我输出错误:

Script to execute Bulk Data Importer

Debuging
D:\tool\PWR Bulk Data Importer.exe -organization VTDEV -dataFile E:\importData.txt -rightsFile E:\importRights.txt -logFile C:\LogFile.log

Executing...

Exception while trying to execute
System.Management.Automation.CommandNotFoundException
The term 'D:\tool\PWR' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

我正面临着PS的学习曲线,并且到目前为止从SO读取了这个脚本。

我看到打印调试行的主要问题:我的所有引号都已消失。我的经理告诉我,Invoke-Expression不是一个好主意。他建议我使用Start-something

现在我被卡住了。任何指针都将非常受欢迎,并且也会有赞成。

1 个答案:

答案 0 :(得分:0)

在try / catch中添加-ErrorAction Stop,这应该会触发所需的操作:

try{
    ...
    Invoke-Expression ($ImportToolExe + " " + $params) -ErrorAction Stop
    ...
}
catch{
    ## catch code
}

您还可以尝试Invoke-Command启动该操作,以便在您的方案中使用以下内容替换Invoke-Expression

Invoke-command -ScriptBlock {&$ImportToolExe $args[0]} -ArgumentList $params