[英]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.