从scrath重写的问题使其更具可读性.
实际上我的问题归结为:
我不能有一个带有强制参数的函数,并将$ null传递给该参数:
Function Foo
{
Param
(
[Parameter(Mandatory = $true)][string] $Bar
)
Write-Host $Bar
}
Foo -Bar $null
Run Code Online (Sandbox Code Playgroud)
这回来了 foo : Cannot bind argument to parameter 'Bar' because it is an empty string.
同样,使它成为一个数组也失败了:
Function Foo
{
Param
(
[Parameter(Mandatory = $true)][string[]] $Bar
)
Write-Host $Bar
}
Foo -Bar 1, 2
> 1 2
Foo -Bar 1, 2, $null
> foo : Cannot bind argument to parameter 'Bar' because it is an empty string.
Run Code Online (Sandbox Code Playgroud)
它编程术语完全有可能有一个接受强制可为空参数的函数,但我找不到在PowerShell中这样做的方法.
怎么做?
我遇到了一些奇怪的行为,其默认参数似乎null
根据该函数是否使用了一个.
或&
在其中调用本机命令来更改值(或空字符串).
这是一个带有两个相同函数的示例脚本,唯一的区别是它们如何调用本机命令(cmd.exe
):
function Format-Type($value)
{
if ($value -eq $null) { '(null)' } else { $value.GetType().FullName }
}
function Use-Dot
{
param(
[string] $Arg = [System.Management.Automation.Language.NullString]::Value
)
Write-Host ".: $(Format-Type $Arg)"
. cmd.exe /c exit 0
}
function Use-Ampersand
{
param(
[string] $Arg = [System.Management.Automation.Language.NullString]::Value
)
Write-Host "&: $(Format-Type $Arg)"
& cmd.exe /c exit 0
}
Use-Dot
Use-Ampersand
Run Code Online (Sandbox Code Playgroud)
在PowerShell 5.1上,我得到以下输出,显示参数的值在两种情况下是不同的:
.: (null)
&: System.String
Run Code Online (Sandbox Code Playgroud)
以这种方式关联这种行为听起来很荒谬,因此我确信我必须在这里遗漏一些明显(或者可能非常微妙)的东西,但那又是什么呢?
-
问题PowerShell管道中的.
简写是什么?讨论了 …
public virtual int Fill(DataSetservices.Jobs_detailsDataTable dataTable,
global::System.Nullable<global::System.String> fromdate,
global::System.Nullable<global::System.DateTime> todate)
Run Code Online (Sandbox Code Playgroud)
我dataset.xsd
在C#中编写了上面的代码,但它抛出了一个错误:
错误1
类型'string'必须是非可空值类型才能在泛型类型或方法'System.Nullable'中将其用作参数'T'
建议我如何使用字符串,因为我想使用字符串,没有别的
在 PowerShell 中,可以定义 C# 代码并执行它。将 $null 传递到以下最简单的函数中表明 not null 被传递到函数中
Add-Type -TypeDefinition @"
public static class foo
{
public static void fooo(string a)
{
if(a!=null)
{
System.Console.WriteLine("Not Null. Is '" + a + "'");
}
}
}
"@ -Language CSharp
Run Code Online (Sandbox Code Playgroud)
按如下方式调用它会导致输出 Not Null。是 ''。这表明 $null 在 C# 中不为空。'IsNullOrEmpty' 或 'IsNullOrWhiteSpace' 之类的方法返回 true,因此 PowerShell 必须隐式地将 $null 转换为字符串。
[foo]::fooo($Null)
Run Code Online (Sandbox Code Playgroud)
除了在 C# 中调用 String.IsNullOrEnpty 之外,是否有更好的解决方法?注意:这与指定的“Add_Type”的 C# 语言无关。我正在使用 PowerShell V5。
我在PowerShell ISE和VS Code中尝试了这个代码,结果相同.没有断点,输出是EMPTY
,但是在行中有断点"NULL"
,输出是NULL
(如预期的那样).为什么?
function demo {
param(
[string] $value = [NullString]::Value
)
if ($null -eq $value) {
"NULL"
} elseif ($value -eq '') {
"EMPTY"
} else {
"$value"
}
}
demo
Run Code Online (Sandbox Code Playgroud)
我现在知道,当你对参数使用类型修饰符[string]时,PowerShell总是将非字符串值(例如$ null或[NullString] :: Value)转换为(空)字符串.好吧,我可以忍受这一点,但是如果调试在这种情况下如此奇怪,那么很难自己解决这个问题.
请考虑以下代码:
function Test
{
[CmdletBinding()]
param
(
[parameter(Mandatory=$true)]
[AllowNull()]
[String]
$ComputerName
)
process{}
}
Test -ComputerName $null
Run Code Online (Sandbox Code Playgroud)
基于官方文档AllowNull
我期待的$ComputerName
可能是[string]
或$null
.但是,运行上面的代码会导致以下错误:
[14,24:Test]无法将参数绑定到参数'ComputerName',因为它是一个空字符串.
为什么$ComputerName
在这种情况下不传递$ null用于工作?
powershell ×5
nullable ×2
string ×2
breakpoints ×1
c# ×1
dataset ×1
debugging ×1
null ×1
null-string ×1
parameters ×1