1

ワークフローと 2 つの単純な関数を含む Windows PowerShell 3.0 スクリプトがあります。ワークフローがなければ、Log関数内で関数を使用できますが、ワークフローでは使用DoSomethingできません。スクリプトは次のとおりです。

function DoSomething()
{
    # This is NOT logged
    Log("This will fail...")
}

function global:Log([string]$Message)
{
    Add-Content -Path "C:\my.log" -Value $Message
}

workflow New-CustomWorkflow
{
    try
    {
        # This is logged
        Log("Starting with the workflow")
        DoSomething
    }
    catch
    {
        # This is logged
        Log($_)
    }
}

New-CustomWorkflow

の内容はmy.log次のようになります。

ワークフロー
System.Management.Automation.RemoteException: 'Log' という用語は、コマンドレット、関数、スクリプト ファイル、または操作可能なプログラムの名前として認識されません。名前のスペルを確認するか、パスが含まれている場合は、パスが正しいことを確認してから再試行してください。Microsoft.PowerShell.Activities.PSActivity.OnResumeBookmark (NativeActivityContext コンテキスト、ブックマーク ブックマーク、オブジェクト値) で System.Activities.Runtime.BookmarkCallbackWrapper.Invoke (NativeActivityContext コンテキスト、ブックマーク ブックマーク、オブジェクト値) で System.Activities.Runtime.BookmarkWorkItem.Execute で(ActivityExecutor エグゼキュータ、BookmarkManager ブックマーク マネージャ)

これは可能ですか?私は何を間違っていますか?

4

1 に答える 1

1

ワークフローで呼び出しているもののほとんどは、try/catch や PowerShell コマンドのように見えるものを含むワークフロー アクティビティです。舞台裏で何が起こっているかを確認するには、次のことを試してください。

(Get-Command New-CustomWorkflow).XamlDefinition

頭が爆発するのを待ちます。:-)

ところで、ネストされた関数とワークフローを持つことができます。これは私のために働く:

workflow New-CustomWorkflow
{
    function DoSomething()
    {
        workflow Log([string]$Message)
        {
            Add-Content -Path "$home\my.log" -Value $Message
        }

        # This is NOT logged
        Write-Output -InputObject "In DoSomething"
        Log -Message "This will fail..."
    }

    try
    {
        # This is logged
        Write-Output -InputObject "Before inline"
        Log -Message "Starting with the workflow"
        Write-Output -InputObject "After inline"
        DoSomething
    }
    catch
    {
        # This is logged
        Write-Output -Input "Doh $_"
        Log -Message $_
    }
}

New-CustomWorkflow
于 2013-09-25T17:42:36.497 に答える