Function 为什么可以';我在PowerShell中看不到脚本的参数吗?

Function 为什么可以';我在PowerShell中看不到脚本的参数吗?,function,powershell,parameters,Function,Powershell,Parameters,我正在编写一个脚本,它将有效地占用时间和我提供的任何输入,并将其输出到现有的文本文件中 我创建的参数是$Input和$logPath$输入应该能够接受管道输入或使用“-Input”,而$logPath参数需要“-logPath” 我不确定我做错了什么,因为在ISE中使用auto complete时,我看不到这些开关。如果我尝试管道输入,我会得到: “输出日志文件:无法将输入对象绑定到的任何参数。” 由于该命令不接受管道输入或 输入及其属性与所输入的任何参数都不匹配 获取管道输入。第1行字符:10

我正在编写一个脚本,它将有效地占用时间和我提供的任何输入,并将其输出到现有的文本文件中

我创建的参数是$Input和$logPath$输入应该能够接受管道输入或使用“-Input”,而$logPath参数需要“-logPath”

我不确定我做错了什么,因为在ISE中使用auto complete时,我看不到这些开关。如果我尝试管道输入,我会得到:

“输出日志文件:无法将输入对象绑定到的任何参数。” 由于该命令不接受管道输入或 输入及其属性与所输入的任何参数都不匹配 获取管道输入。第1行字符:10“

如果我试图坚持开关而不是管道输入,我可以使用一个,但一旦我输入了一个,我就不能使用第二个开关

这是我的职责,感谢您的帮助:

function Global:Out-LogFile {
    #Planned function for outputting to a specified log file.
     <#
    .SYNOPSIS
    Adds to log files.

    .DESCRIPTION
    Updates a specified log file with a new line. The log file must already exist. See: Get-help New-LogFile

    .PARAMETER logPath
    Specifies a path for your new log (including file name).

    .EXAMPLE
    Get-ChildItem C:\ | Out-LogFile
    Piped input.

    #>

    [CmdletBinding()]

    param(
        [Parameter(Mandatory=$True,
                  ValueFromPipeline=$True,
                  ValueFromPipelineByPropertyName=$True,
                  HelpMessage="input to append to logFile",
                  ParameterSetName='Input')]
        [string[]]$Input,


        [Parameter(Mandatory=$True,
                  HelpMessage="Please enter the path to your log file",
                  ParameterSetName='logPath')]
        [string]$logPath
    )

    #Check to see that $logpath physically exists as a log file.
    Try {
        Write-Verbose Checking for existing log file.
        gi -Path $logPath -ErrorVariable $logPathError | Out-Null
    }

    Catch {
        throw "quitting! logPath does not exist, did you use the -logpath parameter? see: $logPathError"
    }

    #Create new time object, give it properties of Hours, minutes, seconds, milliseconds.
    Try {
        Write-Verbose "Creating Time object for recording log timestamps"
        $time = New-Object PSObject -ErrorVariable $timeError
        $time | Add-Member -MemberType NoteProperty -Name Hours -Value (Get-Date).Hour
        $time | Add-Member -MemberType NoteProperty -Name Minutes -Value (Get-Date).Minute
        $time | Add-Member -MemberType NoteProperty -Name Seconds -Value (Get-Date).Second
        $time | Add-Member -MemberType NoteProperty -Name Milliseconds -Value (Get-Date).Millisecond
        #declare "currentTime" variable used to place timestamps in log files. 
        [string]$currentTime = $time.Hours.ToString() + ":" + $time.Minutes.ToString() + ":" + $time.Seconds.ToString() + ":" + $time.Milliseconds.ToString() + "   "

    }

    Catch {
        throw "Can't create PSObject: time. See: $timeError"
    }

    Try {
        #Append data to log file
        Write-Verbose "Writing line to log file"
        [string]$currentTime + [string]$Input >> $logPath

    }
    Catch {
        throw "Quitting! Can't write to log file"
    }


}
函数全局:输出日志文件{
#用于输出到指定日志文件的计划函数。
[CmdletBinding()]
param(
[参数(必需=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage=“要附加到日志文件的输入”,
参数setName='Input')]
[string[]]$Input,
[参数(必需=$True,
HelpMessage=“请输入日志文件的路径”,
参数setName='logPath')]
[字符串]$logPath
)
#检查$logpath是否作为日志文件实际存在。
试一试{
对现有日志文件进行详细写入检查。
gi-路径$logPath-错误变量$logPathError | Out Null
}
抓住{
抛出“退出!logPath不存在,是否使用了-logPath参数?请参阅:$logPathError”
}
#创建新的时间对象,将其属性设置为小时、分钟、秒、毫秒。
试一试{
写详细的“创建记录日志时间戳的时间对象”
$time=新对象PSObject-ErrorVariable$TIMERROR
$time |添加成员-成员类型NoteProperty-名称小时数-值(获取日期).Hour
$time |添加成员-MemberType NoteProperty-Name Minutes-Value(获取日期).Minute
$time |添加成员-成员类型NoteProperty-名称秒-值(获取日期)。秒
$time |添加成员-成员类型NoteProperty-名称毫秒-值(获取日期).毫秒
#声明用于在日志文件中放置时间戳的“currentTime”变量。
[string]$currentTime=$time.Hours.ToString()+”:“+$time.Minutes.ToString()+”:“+$time.Seconds.ToString()+”:“+$time.Milses.ToString()+”
}
抓住{
抛出“无法创建PSObject:time。请参阅:$timeError”
}
试一试{
#将数据附加到日志文件
写详细的“将行写入日志文件”
[string]$currentTime+[string]$Input>>$logPath
}
抓住{
抛出“退出!无法写入日志文件”
}
}

您为
$Input
$logPath
指定了不同的参数集名称。这实际上意味着它们是互斥的,PowerShell不会让您同时使用两者(这就是为什么它们不会出现在ISE的自动完成中)。因为在这种情况下,您总是希望同时使用这两个参数,所以实际上根本不需要指定自定义参数集

其他需要注意的事项: 当我有你们在这里时,让我指出一些可能存在问题的其他事情:

您已将输入参数命名为
$input
,您不应该这样做,因为
$input
是PowerShell中预定义/自动生成的变量(具体而言,
$input
是提供对当前管道访问的枚举器对象)因此,赋予函数参数相同的名称可能会导致脚本以意外的方式运行

例如,如果尝试手动将值传递给参数,如下所示:

Out-LogFile -Input "string1" -logPath test.txt 首先,它总是会引发异常,因为
Write Verbose
文本不在引号中,因此不是将整个句子视为字符串,而是将单词“Checking”传递给
Write Verbose
Message
参数,然后将“for”传递给接受字符串的下一个位置参数(当然,它不存在)。因此,您将收到一个“ParameterBindingException”,它的内容大致如下:

Write-Verbose : A positional parameter cannot be found that accepts argument 'for'.
整个块是不必要的(也是获取时间字符串的一种非常迂回的方式):

它基本上做了相同的事情,只是对你和PowerShell来说都少了一些工作量



最后,实际上没有任何理由将写操作包含在try-catch块中这将是一个非终止错误,无论如何也不会被捕获。

您能告诉我们您是如何调用产生错误的函数的吗?@mjolinor“test”| Out logFile-logPath“C:\logFile.txt”实际上有两件事情失败了,即信息管道和函数调用后的开关。 Write-Verbose : A positional parameter cannot be found that accepts argument 'for'. if (!(Test-Path -Path $logPath)) { throw [System.IO.FileNotFoundException]("Could not find the file specified in logPath") } #Create new time object, give it properties of Hours, minutes, seconds, milliseconds. Try { Write-Verbose "Creating Time object for recording log timestamps" $time = New-Object PSObject -ErrorVariable $timeError $time | Add-Member -MemberType NoteProperty -Name Hours -Value (Get-Date).Hour $time | Add-Member -MemberType NoteProperty -Name Minutes -Value (Get-Date).Minute $time | Add-Member -MemberType NoteProperty -Name Seconds -Value (Get-Date).Second $time | Add-Member -MemberType NoteProperty -Name Milliseconds -Value (Get-Date).Millisecond #declare "currentTime" variable used to place timestamps in log files. [string]$currentTime = $time.Hours.ToString() + ":" + $time.Minutes.ToString() + ":" + $time.Seconds.ToString() + ":" + $time.Milliseconds.ToString() + " " } Catch { throw "Can't create PSObject: time. See: $timeError" } $currentTime = (Get-Date).ToString("hh:mm:ss:fff") + " "