Function Windows PowerShell使用参数调用函数

Function Windows PowerShell使用参数调用函数,function,powershell,parameters,Function,Powershell,Parameters,我对PowerShell完全陌生,正在尝试编写一个简单的脚本来生成日志文件。我搜索了论坛,找不到我问题的答案。 我在网上找到了我认为有用的示例,并将其应用到我的脚本中: ## Get current date and time. In return, you’ll get back something similar to this: Sat January 25 10:07:25 2014 $curDateTime = Get-Date $logDate = Get-Date -format

我对PowerShell完全陌生,正在尝试编写一个简单的脚本来生成日志文件。我搜索了论坛,找不到我问题的答案。 我在网上找到了我认为有用的示例,并将其应用到我的脚本中:

## Get current date and time. In return, you’ll get back something similar to this: Sat January 25 10:07:25 2014 $curDateTime = Get-Date $logDate = Get-Date -format "MM-dd-yyyy HH:mm:ss" $LogPath = "C:\Temp\Log" $LogName = "log_file_" + $logDate + ".log" $sFullPath = $LogPath + "\" + $LogName <# param( ## The path to individual location files $Path, ## The target path of the merged file $Destination, ## Log path $LogPath, ## Log name $LogName ## Full LogFile Path ## $sFullPath = $LogPath + "\" + $LogName ) #> Function Log-Start { <# .SYNOPSIS Creates log file .DESCRIPTION Creates log file with path and name that is passed. Once created, writes initial logging data .PARAMETER LogPath Mandatory. Path of where log is to be created. Example: C:\Windows\Temp .PARAMETER LogName Mandatory. Name of log file to be created. Example: Test_Script.log .INPUTS Parameters above .OUTPUTS Log file created #> [CmdletBinding()] Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$true)][string]$LogName) Process { ## $sFullPath = $LogPath + "\" + $LogName # Create file and start logging New-Item -Path $LogPath -Value $LogName –ItemType File Add-Content -Path $sFullPath -Value "***************************************************************************************************" Add-Content -Path $sFullPath -Value "Started processing at [$([DateTime]::Now)]." Add-Content -Path $sFullPath -Value "***************************************************************************************************" Add-Content -Path $sFullPath -Value "" } } Set-StrictMode -Version "Latest" Log-Start .... ##获取当前日期和时间。作为回报,你会得到类似的结果:2014年1月25日星期六10:07:25 $curDateTime=获取日期 $logDate=获取日期-格式为“MM dd yyyy HH:MM:ss” $LogPath=“C:\Temp\Log” $LogName=“log\u文件”+$logDate+“.log” $sFullPath=$LogPath+“\”+$LogName 函数日志启动{ [CmdletBinding()] Param([Parameter(Mandatory=$true)][string]$LogPath,[Parameter(Mandatory=$true)][string]$LogName) 进程{ ##$sFullPath=$LogPath+“\”+$LogName #创建文件并开始日志记录 新项目-路径$LogPath-值$LogName–项目类型文件 添加内容-路径$sFullPath-值“********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* 添加内容-Path$sFullPath-Value“在[$([DateTime]::Now)]开始处理。” 添加内容-路径$sFullPath-值“********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* 添加内容-路径$sFullPath-值“” } } 设置StrictMode-版本为“最新” 日志启动 .... 问题是如何使Log_Start函数使用我在脚本开头指定的变量,或者使用[CmdletBinding()]声明和函数本身是不可能的。 如果我尝试按编码方式运行它,它会提示我输入路径和日志名,我认为它应该使用我已经定义的内容。很明显,我没有理解这个概念。我当然可以在函数的param声明中直接指定所需的值,但我计划使用更多的函数,如log write和log finish,并且不希望重复相同的值。
我遗漏了什么?

您在脚本顶部定义了自定义参数,现在必须通过更改将它们传递给函数


日志启动

行读


日志开始$LogPath$LogName

尽管您最好以不同的方式命名参数,以避免混淆。除非您计划在函数中使用-Verbose或-Debug等常用参数,这样您就不需要CmdletBinding()声明了,这样就可以省去以下两行代码:


[CmdletBinding()]
Param([Parameter(Mandatory=$true)][string]$LogPath,[Parameter(Mandatory=$true)][string]$LogName)


而且您的脚本仍然可以工作。

如果您希望包含配置文件中的设置,一种方法是哈希表。您的配置文件如下所示:


logpath=c:\somepath\
server=server.domain

然后,脚本将有一个指向配置文件的额外变量和一个导入该文件的函数:

$configFile = "c:\some.config";  
function GetConfig(){  
    $tempConfig = @{};  
    $configLines = cat $configFile -ErrorAction Stop;  
    foreach($line in $configLines){         
        $lineArray = $line -split "=";          
        $tempConfig.Add($lineArray[0].Trim(), $lineArray[1].Trim());        
    }  
    return $tempConfig;
}  
$config = GetConfig  
然后,可以将配置值分配给变量:

$LogPath = $conifg.Item("logpath")  
$server = $conifg.Item("server")  
或者直接使用它们访问

$conifg.Item("server")  

难道不是这样吗?Log Start-LogPath$LogPath-LogName$LogName不仅仅是$LogPath和$LogName?它不再要求输入,而是给出以下错误:新项:不支持给定路径的格式。在zzz.ps1:82 char:17+处,您正试图创建一个文件,其中包含“:”更改$logDate=Get Date-格式为“MM-dd-yyyy-HH-MM-ss”。至于输入,它是位置性的,所以只要参数按正确的顺序传递,就不必使用-LogPath和-LogName。叶,完全忘记了Windows不喜欢文件名中的“:”,更改了格式,它就工作了。但我再问你一个问题。如果我想在两个不同的环境中运行脚本,并且想创建包含所有参数的文件,在脚本中执行某种“include”操作并从中提取所有parm,那么最好使用什么命令?感谢您的建议。我完全按照您的建议定义了configfile,将函数复制到代码中,所有函数定义的第一行都是:$config=GetConfig$configfile。我收到的错误是[DBG]>>>不能对空值表达式调用方法。在xxx.ps1:73 char:87+$tempConfig.Add($lineArray[0].Trim(),$lineArray[1].Trim(),$lineArray[2].Trim您一定没有像错误一样使用我的代码:$tempConfig.Add($lineArray[0].Trim(),$lineArray[1].Trim(),$lineArray[2]。Trim显示您添加了$lineArray[2].Trim我假定您修改了代码,以说明配置文件中超过2个键/值对,但该代码行的功能是在=字符上拆分每个配置行。只需将更多键/值对添加到配置文件中,它们就会被导入(ok;)它们没有。在我的配置文件中有另一个名称/值对,这给了我一个错误。当我只使用两个变量时,效果很好。我猜我做错了什么。我不确定我是否理解你的答案,你能更具体一点吗?上述代码无需更改,无论配置中有多少个键/值对,只要kEY是唯一的(没有空行)。您附加的错误显示正在执行额外的代码$lineArray[2]。导致问题的原因是修剪。是的,我不必再添加另一个$lineArray项目2。感谢您的帮助