簡體   English   中英

如何從 PowerShell 調用 C# 腳本並通過 arguments

[英]How to call a C# script from PowerShell and pass arguments

我想用 3 個 arguments 調用 PowerShell 腳本。

第一個參數是 C# 腳本文件的路徑。

在 PowerShell 腳本中,我想在 C# 腳本中執行一個方法,並將所有 arguments 傳遞給它。

# RunBackup.ps1
$csharp = Get-Content -Path $args[0]
$job = Start-Job -ScriptBlock {
    Add-Type -TypeDefinition "$csharp"
    $obj = New-Object Main
    $result = $obj.Execute($args)
    $result
}
Wait-Job $job
Receive-Job $job

// Main.cs
using System;

public class Main
{
    public int Execute(string[] args)
    {
        Console.WriteLine(args[0]);
        return 0;
    }
}

但是當我使用./RunBackup Main.cs 1 2 3運行 PS 腳本時,出現以下錯誤。

Cannot bind argument to parameter 'TypeDefinition' because it is an empty string.
    + CategoryInfo          : InvalidData: (:) [Add-Type], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.AddTypeCommand
    + PSComputerName        : localhost
 
Cannot find type [Main]: verify that the assembly containing this type is loaded.
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
    + PSComputerName        : localhost

You cannot call a method on a null-valued expression.
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
    + PSComputerName        : localhost

您正在嘗試訪問$csharp變量,但它不在子ScriptBlock中的 scope 中,因此根據 PowerShell 變量初始化和引用的默認寬松規則,它會自動設置為$null 您可以使用Set-StrictMode來驗證這一點,以防止和警告這種默認行為:

$csharp = Get-Content -Path $args[0]
$job = Start-Job -ScriptBlock {
    Set-StrictMode -Version Latest

    Add-Type -TypeDefinition $csharp
    $obj = New-Object Main
    $result = $obj.Execute($args)
    $result
}
Wait-Job $job
Receive-Job $job

Output:

The variable '$csharp' cannot be retrieved because it has not been set.
    + CategoryInfo          : InvalidOperation: (csharp:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined
    + PSComputerName        : localhost
 
Cannot find type [Main]: verify that the assembly containing this type is loaded.
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
    + PSComputerName        : localhost
 
The variable '$obj' cannot be retrieved because it has not been set.
    + CategoryInfo          : InvalidOperation: (obj:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined
    + PSComputerName        : localhost
 
The variable '$result' cannot be retrieved because it has not been set.
    + CategoryInfo          : InvalidOperation: (result:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined
    + PSComputerName        : localhost

要修復,您需要將$csharp的值顯式傳遞給ScriptBlock

$csharp = Get-Content -Path $args[0]
$job = Start-Job -ScriptBlock {
    Add-Type -TypeDefinition $input # powershell implicit param provided by InputObject
    $obj = New-Object Main
    $result = $obj.Execute($args)
    $result
} -InputObject $csharp
Wait-Job $job
Receive-Job $job

當然,現在您會得到不同的錯誤,但這些錯誤不在此問題的 scope 范圍內!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM