簡體   English   中英

Windows PowerShell調用帶有參數的功能

[英]Windows PowerShell invoking function with 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 "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
....

問題是如何使Log_Start函數使用在腳本開始處分配的變量,或者[CmdletBinding()]的聲明和函數本身無法實現。 如果我嘗試以編碼方式運行它,則提示我輸入路徑和日志名,我認為它應該使用已經定義的名稱。 顯然我缺少這個概念。 我當然可以只在函數的param聲明中分配我需要的值,但是我打算使用更多的函數,例如log-write和log-finish,並且不想重復相同的值。 我想念什么?

您已在腳本頂部定義了自定義參數,現在必須通過更改將它們傳遞給函數。

Log-Start

讀行

Log-Start $LogPath $LogName

盡管最好以不同的方式命名參數以避免混淆。 除非您計划在函數中使用-Verbose或-Debug等通用參數,否則您實際上不需要CmdletBinding()聲明,因此可以擺脫以下兩行:

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

並且您的腳本仍然可以使用。

如果要包括配置文件中的設置,則一種方法是哈希表。 您的配置文件如下所示:

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

然后您的腳本將有一個額外的var指向配置文件和一個導入它的函數:

$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")  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM