【问题标题】:Powershell function call changing passed string into intPowershell函数调用将传递的字符串更改为int
【发布时间】:2021-03-20 08:01:59
【问题描述】:

所以我正在使用那种有问题的 Sapien powershell studio 来制作一个 powershell 驱动的 GUI 应用程序,并且我正在尝试执行 ADSI 查询。

$nameOfDeviceInput 是一个 System.Windows.Forms.TextBox

在一个表单上,我有以下功能:

$buttonPerformAction_Click={
    if (FindInAD($nameOfDeviceInput.Text).Count -gt 0)
    {
        $buttonPerformAction.BackColor = 'Red'
        $buttonPerformAction.Text = "System already exists in AD with that name. Try another name"
        return
    }
.....
}

在“主”表单上,我有 FindInAD 函数

function FindInAd($nameOfSystem)
{
    Write-Host "seeking system" $nameOfSystem
    ([adsisearcher]"(CN=$nameOfSystem)").FindAll()
}

FindInAd() 失败,因为无论出于何种原因,$nameOfSystem 设置为 1,如果我没有将其显式转换为字符串,它会隐式转换为 Int32(显然)

我尝试了以下方法:

通过标注它所属的表单来完全限定文本框输入 ($adObjectModifier)

 $buttonPerformAction_Click={
        if (FindInAD($adObjectModifier.$nameOfDeviceInput.Text).Count -gt 0)
        {
            $buttonPerformAction.BackColor = 'Red'
            $buttonPerformAction.Text = "System already exists in AD with that name. Try another name"
            return
        }
    .....
    }

将 $nameOfSystem 参数显式转换为 [string] 类型

function FindInAd([string]$nameOfSystem)
{
    Write-Host "seeking system" $nameOfSystem
    ([adsisearcher]"(CN=$nameOfSystem)").FindAll()
}

将原始字符串从 AdObjectModifier 表单传递到 FindInAD。

.... 

if (FindInAD("Test").Count -gt 0)

....

在方法调用之间,当时输出管道上没有其他内容(至少不是来自我)。它是 EventHandler > 带字符串参数的函数调用

为什么我传递的字符串变成了数字???

编辑:我认为我传递的参数正在以某种方式自动替换为结果布尔值,但这对我没有任何意义....

【问题讨论】:

    标签: powershell method-invocation method-parameters


    【解决方案1】:

    您有一个语法问题

    FindInAD($nameOfDeviceInput.Text).Count # WRONG
    

    注意:错误在此上下文中意味着:语法正式有效,但没有达到您的预期 -见底部。

    应该是:

    (FindInAD $nameOfDeviceInput.Text).Count
    

    PowerShell 命令 - 函数、cmdlet、脚本和外部程序 - 像 shell 命令一样调用 - foo arg1 arg2 - 和 不像 像 C# 方法 - foo('arg1', 'arg2')

    即:

    • 不要不要(...)放在参数列表周围。

      • 但是,如果您希望命令调用参与表达式,您确实需要(...)整个调用,如上所示,可以访问属性 .Count - 有关更多信息,请参阅 this answer
    • 使用 空格(至少一个空格)分隔参数,彼此之间以及命令名称 - 不要使用,

      • , 参数之间的功能不同:它构造一个 array 作为 single 参数传递 - 见下文。
    • 您可以将简单的字符串(既不包含空格也不包含 PowerShell 元字符,例如 ;&)作为 barewords 传递;也就是说,引用它们是可选的;例如,您可以调用 foo bar 而不是 foo 'bar' - 请参阅 this answer 了解 PowerShell 如何解析未引用的命令参数。

    • 此外,如果目标函数或脚本具有 explicitly declared parametersbinary cmdlet 总是这样做),例如 -bar-baz,您可以将值传递为 named 参数,即在它们前面加上目标参数名称;这样做是脚本中的好习惯:foo -bar arg1 -baz arg2

    相比之下,调用对象方法使用与常规编程语言类似的语法,例如C#( $obj.foo('arg1', 'arg2'))

    这个区别与两个PowerShell's two fundamental parsing modes有关,在this answer中有详细解释:

    • 命令参数模式中被解析——就像在shell中一样。

    • 方法调用和基于运算符的表达式以表达式模式进行解析 - 就像在常规编程语言中一样。

    这些模式是允许 PowerShell 服务双重职责所必需的:一方面作为 shell,另一方面作为 脚本(编程)语言 另一方面。


    PowerShell 可以帮助您避免这种语法问题

    请注意,问题不在于使用 method 语法来调用 commandinvalid 语法,而是它 没有无法按预期工作,这可能难以诊断。

    简而言之:当您将命令 foo 调用为foo('foo', 'bar') 时,('foo', 'bar') 是一个 2 元素数组,然后将其传递给foo 作为单个参数

    为了防止出现问题,您可以Set-StrictMode 设置为-Version 2 或更高版本,如果您不小心使用方法语法,PowerShell 会报告错误调用命令时:

    # Turn on the check for accidental method syntax.
    # Note: This also turns on ADDITIONAL checks - see below.
    Set-StrictMode -Version 2
    
    # This call now produces an ERROR, because the proper syntax would be:
    #    foo 'a' 'b'
    foo('a', 'b')
    

    注意事项

    • Set-StrictMode -Version 2 包含附加严格性检查,您还必须遵守这些检查,特别是:

      • 您不得引用不存在的变量
      • 您不得引用不存在的属性;请参阅GitHub issue #2798,了解与 PowerShell 统一处理标量和集合有关的相关缺陷。
    • 仅对带有多个参数的伪方法调用(例如,
      foo('bar', 'baz'))报告错误,而不是仅一个;例如,foo('bar') 被接受,因为单参数情况通常仍然(意外地)有效。

    • 违反严格性报告的错误是语句-终止错误:也就是说,它们只终止手头的语句,但默认情况下脚本继续;为了确保整体执行中止 - 对于任何类型的错误 - 您必须在代码开头设置
      $ErrorActionPreference = 'Stop'。请参阅this answer 了解更多信息。


    至于你尝试了什么

    FindInAD($nameOfDeviceInput.Text).Count

    等同于:

    FindInAD ($nameOfDeviceInput.Text).Count

    也就是说,表达式 ($nameOfDeviceInput.Text).Count 的结果作为参数传递给函数FindInAD

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 2014-01-01
      相关资源
      最近更新 更多