2016-03-23 1 views
0

Bu mümkün mü?Bir Komut Dosyası Yazmak İçin Hangi Powershell Sürümünün Kullanıldığı Nasıl Kontrol Edilir

Bilgisayarımın üzerinde çalışan ancak istemci bilgisayarda çalışan bir komut dosyası kullandığım için ve komut dosyasının yanlış sürümü kullandığı için işe yaramazsa hata ayıklamaya çalışırken zaman kaybı olacak diye soruyorum.

Teşekkür


I (oturum açma komut üstünden) çalıştırmak çalışıyorum komut dosyası:

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.pshere olduğunu ve ben için kod gönderdiniz İlk işlevini aramayı denedim (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 "" 
    } 
} 
+0

Tek yapmanız gereken anahtar kelimeleri analiz etmek ve kodun çalışması için gereken MINIMUM sürümünü belirlemektir. – Jimbo

+7

Komut dosyaları * bir sürümle * yazılmadı. Bir sürüm için * yazılmıştır. Yani, özellikleri, sözdizimi, vb. Kullanırlar ve hangi powershell ile uyumlu olduklarını belirler. ** Onlar için betik "çalışmıyor" nasıl? Hiç çalışmıyor mu? Bir hata mı veriyor? –

+4

Sanırım "PowerShell'in hangi sürümünü bir komut dosyası için kullanabileceğini kontrol et". Komut dosyası yazarı, bir komut dosyasını çalıştırmak için gereken minimum bir gerekli PowerShell sürümünü dikte etmek için '# require' yönergesini kullanmalıdır; aksi halde, komut dosyası hata oluşturabilir. –

cevap

0

bir senaryo için hangi sürümü gerekli belirtmek için kanonik yolu #Requires kullanır:

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

fazla bilgi için help about_requires bakınız.

Komut dosyanızda #Requires yerleştirmek, yalnızca gereken minimum sürüm yüklendiğinde çalışacağından emin olacaktır.

OP başlıktaki soruyu cevaplamak için: komut #Requires kullanılarak yazılmıştır eğer sadece #Requires anahtar kelime için komut dosyası arama ve -Version argüman için ayrıştırmak. #Requires kullanılmadıysa, OP'ye yapılan yorumlara bakın.