如何检查哪个版本的Powershell用于编写脚本

时间:2016-03-23 15:08:00

标签: powershell

这可能吗?

我问,因为我的脚本可以在我的电脑上运行而不能在客户机上运行,​​如果因为脚本使用了错误的版本而无法正常工作,那将是浪费时间。

由于

我尝试运行的脚本(从登录脚本的顶部):

try {
    . "\\dc1\netlogon\PSSubs\Logging_Functions.ps1"
} 
catch { [System.Windows.Forms.MessageBox]::Show($Error[0]) }

try {
    Log-Start -LogPath "\\DC1\NETLOGON\PSSubs" -LogName "Test_Log.log" -ScriptVersion "1.0"
} 
catch { [System.Windows.Forms.MessageBox]::Show($Error[0]) }

Logging_Functions.ps来自here,我已发布第一个函数的代码,我尝试调用(Log-Start)nelow。

Function Log-Start{
  <#
  .SYNOPSIS
    Creates log file

  .DESCRIPTION
    Creates log file with path and name that is passed. Checks if log file exists, and if it does deletes it and creates a new one.
    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

  .PARAMETER ScriptVersion
    Mandatory. Version of the running script which will be written in the log. Example: 1.5

  .INPUTS
    Parameters above

  .OUTPUTS
    Log file created

  .NOTES
    Version:        1.0
    Author:         Luca Sturlese
    Creation Date:  10/05/12
    Purpose/Change: Initial function development

    Version:        1.1
    Author:         Luca Sturlese
    Creation Date:  19/05/12
    Purpose/Change: Added debug mode support

  .EXAMPLE
    Log-Start -LogPath "C:\Windows\Temp" -LogName "Test_Script.log" -ScriptVersion "1.5"
  #>

  [CmdletBinding()]

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

  Process{
    $sFullPath = $LogPath + "\" + $LogName

    #Check if file exists and delete if it does
    If((Test-Path -Path $sFullPath)){
      Remove-Item -Path $sFullPath -Force
    }

    #Create file and start logging
    New-Item -Path $sFullPath -ItemType File

    # This version displays an annoying erro
    # 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 ""
    Add-Content -Path $sFullPath -Value "Running script version [$ScriptVersion]."
    Add-Content -Path $sFullPath -Value ""
    Add-Content -Path $sFullPath -Value "***************************************************************************************************"
    Add-Content -Path $sFullPath -Value ""

    #Write to screen for debug mode
    Write-Debug "***************************************************************************************************"
    Write-Debug "Started processing at [$([DateTime]::Now)]."
    Write-Debug "***************************************************************************************************"
    Write-Debug ""
    Write-Debug "Running script version [$ScriptVersion]."
    Write-Debug ""
    Write-Debug "***************************************************************************************************"
    Write-Debug ""
  }
}

1 个答案:

答案 0 :(得分:0)

脚本指定所需版本的规范方法是使用#Requires

      #Requires -Version <N>[.<n>]
      #Requires –PSSnapin <PSSnapin-Name> [-Version <N>[.<n>]]
      #Requires -Modules { <Module-Name> | <Hashtable> } 
      #Requires –ShellId <ShellId>
      #Requires -RunAsAdministrator

有关详细信息,请参阅help about_requires

因此,在您的脚本中放置#Requires将确保只有在安装了所需的最低版本时才会运行。

要回答OP标题中的问题:如果脚本是使用#Requires编写的,只需在脚本中搜索#Requires关键字并解析-Version参数。如果未使用#Requires,请参阅对OP的评论。