如何在Powershell脚本中创建函数?

时间:2017-09-28 07:38:03

标签: function powershell powershell-v3.0 powershell-v4.0

如何编写函数以避免我的powershell脚本的重复代码,它基本上显示控制面板中的“添加和删除”窗口中我的机器上安装的应用程序列表。

以下是powershell脚本

Stack.ps1

Clear-Host

#$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$scriptPath = $PSScriptRoot
$logFilePath= Join-path $scriptPath "TestResults.log"

#If log file exists, then clear its contents 
if (Test-Path $logFilePath)
{
    clear-content -Path $logFilePath
} 


$log = "Date Of Testing: {0} " -f (Get-Date)
$logString = "Process Started." 
add-content -Path $logFilePath -Value $log -Force 
add-content -Path $logFilePath -Value $logString -Force 
add-content -Path $logFilePath -Value "`n" -Force


#Validate ADD/Remove Program list

add-content -Path $logFilePath -Value "`n" -Force
add-content -Path $logFilePath -Value "Add/Remove Programs :" -Force
add-content -Path $logFilePath -Value "`n" -Force

  $InstalledPrograms = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*

add-content -Path $logFilePath -Value "`n" -Force
foreach ($InstalledProgram in $InstalledPrograms )
{
   if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("Operational Control")))
    {
        $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | 
        Format-Table –AutoSize

        $logString = $InstalledProgram.DisplayName 
        $logp = $InstalledProgram.Publisher
        $logv = $InstalledProgram.DisplayVersion
        $logd= $InstalledProgram.InstallDate

        add-content -Path $logFilePath -Value "Product Name: $logString" -Force   
        add-content -Path $logFilePath -Value "Publisher: $logp" -Force  
        add-content -Path $logFilePath -Value "Version: $logv" -Force  
        add-content -Path $logFilePath -Value "InstallDate: $logd" -Force  
        add-content -Path $logFilePath -Value "`n" -Force****

    }

if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("Management Studio")))
    {
        $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | 
        Format-Table –AutoSize

        $logString = $InstalledProgram.DisplayName 
        $logp = $InstalledProgram.Publisher
        $logv = $InstalledProgram.DisplayVersion
        $logd= $InstalledProgram.InstallDate

        add-content -Path $logFilePath -Value "Product Name: $logString" -Force   
        add-content -Path $logFilePath -Value "Publisher: $logp" -Force  
        add-content -Path $logFilePath -Value "Version: $logv" -Force  
        add-content -Path $logFilePath -Value "InstallDate: $logd" -Force  
        add-content -Path $logFilePath -Value "`n" -Force

    }

    if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("System Analyzer")))
    {
        $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | 
        Format-Table –AutoSize

        $logString = $InstalledProgram.DisplayName 
        $logp = $InstalledProgram.Publisher
        $logv = $InstalledProgram.DisplayVersion
        $logd= $InstalledProgram.InstallDate

        add-content -Path $logFilePath -Value "Product Name: $logString" -Force   
        add-content -Path $logFilePath -Value "Publisher: $logp" -Force  
        add-content -Path $logFilePath -Value "Version: $logv" -Force  
        add-content -Path $logFilePath -Value "InstallDate: $logd" -Force  
        add-content -Path $logFilePath -Value "`n" -Force

    }

      if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("STeP")))
    {
        $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | 
        Format-Table –AutoSize

        $logString = $InstalledProgram.DisplayName 
        $logv = $InstalledProgram.DisplayVersion
        $logd= $InstalledProgram.InstallDate

        add-content -Path $logFilePath -Value "Product Name: $logString" -Force   
        add-content -Path $logFilePath -Value "Version: $logv" -Force  
        add-content -Path $logFilePath -Value "InstallDate: $logd" -Force  
        add-content -Path $logFilePath -Value "`n" -Force

    }   
}

我说的是每个if语句下面的代码,用于将输出复制到日志文件。我可以用函数替换该代码并在每个if语句中调用该函数。

1 个答案:

答案 0 :(得分:3)

好的,让我们这样做吧。函数是关于避免重复代码,这可以在几个级别完成。 PowerShell中的函数需要在执行之前声明,因此我倾向于在脚本的开头聚集所有函数,如下所示:

# *** FUNCTION DEFINITIONS

function Log-InstalledProgram($InstalledProgram, $LogFilePath)
{
    $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | 
    Format-Table –AutoSize

    $logString = $InstalledProgram.DisplayName 
    $logp = $InstalledProgram.Publisher
    $logv = $InstalledProgram.DisplayVersion
    $logd= $InstalledProgram.InstallDate

    add-content -Path $LogFilePath -Value "Product Name: $logString" -Force   
    add-content -Path $LogFilePath -Value "Publisher: $logp" -Force  
    add-content -Path $LogFilePath -Value "Version: $logv" -Force  
    add-content -Path $LogFilePath -Value "InstallDate: $logd" -Force  
    add-content -Path $LogFilePath -Value "`n" -Force
}

# *** BEGIN MAIN SCRIPT

Clear-Host

#$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$scriptPath = $PSScriptRoot
$logFilePath= Join-path $scriptPath "TestResults.log"

#If log file exists, then clear its contents 
if (Test-Path $logFilePath)
{
    clear-content -Path $logFilePath
} 


$log = "Date Of Testing: {0} " -f (Get-Date)
$logString = "Process Started." 
add-content -Path $logFilePath -Value $log -Force 
add-content -Path $logFilePath -Value $logString -Force 
add-content -Path $logFilePath -Value "`n" -Force


#Validate ADD/Remove Program list

add-content -Path $logFilePath -Value "`n" -Force
add-content -Path $logFilePath -Value "Add/Remove Programs :" -Force
add-content -Path $logFilePath -Value "`n" -Force

  $InstalledPrograms = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*

add-content -Path $logFilePath -Value "`n" -Force

foreach ($InstalledProgram in $InstalledPrograms )
{
   if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("Operational Control")))
    {
        Log-InstalledProgram $InstalledProgram $logFilePath
    }

    if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("Management Studio")))
    {
        Log-InstalledProgram $InstalledProgram $logFilePath
    }

    if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("System Analyzer")))
    {
        Log-InstalledProgram $InstalledProgram $logFilePath
    }

    if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("STeP")))
    {
        Log-InstalledProgram $InstalledProgram $logFilePath
    }   
}

您可以通过为所需程序添加内部循环来进一步减少代码:

foreach ($InstalledProgram in $InstalledPrograms )
{
    foreach ($displayName in "Operational Control","Management Studio", "System Analyzer", "STeP")
    {
        if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains($displayName)))
        {
            Log-InstalledProgram $InstalledProgram $logFilePath
        }
    }
}

还有其他优化,例如,您可以避免重复调用Add-Content,因为-Value参数也将数组作为参数,您可以使用哈希表来捕获您想要的对象属性在日志文件中提取其显示名称,依此类推。