2014-09-29 72 views
3

說我有兩大塊這樣的:如何根據以前的資源執行情況有條件地執行Powershell DSC中的資源?

Configuration TestConfig 
{ 
    Node ('localhost') { 
     File foo { 
      DestinationPath = 'C:\foo.txt' 
      Contents = 'Bar' 
     } 
     Script dothing { 
      SetScript = {'Baz' | set-content C:\foo.txt} 
      TestScript = {return $false} 
      GetScript = { 
       return @{ 
        GetScript = '' 
        SetScript = '' 
        TestScript = '' 
        Credential = $Credential 
        Result = (Invoke-Expression -Command $TestScript) 
       } 
      } 
      DependsOn = '[File]foo' 
     } 
    } 
} 

我建了一個試驗,做這樣的事情,但它似乎像服務資源獲取文件資源的測試輸出,無論執行的 - 也就是說,如果它實際上對文件做了任何事情。

VERBOSE: [MACHINENAME]: LCM: [ Start Set  ] 
VERBOSE: [MACHINENAME]: LCM: [ Start Resource ] [[File]foo] 
VERBOSE: [MACHINENAME]: LCM: [ Start Test  ] [[File]foo] 
VERBOSE: [MACHINENAME]:       [[File]foo] The destination object was found and no action is required. 
VERBOSE: [MACHINENAME]: LCM: [ End Test  ] [[File]foo] in 0.0040 seconds. 
VERBOSE: [MACHINENAME]: LCM: [ Skip Set  ] [[File]foo] 
VERBOSE: [MACHINENAME]: LCM: [ End Resource ] [[File]foo] 
VERBOSE: [MACHINENAME]: LCM: [ Start Resource ] [[Script]dothing] 
VERBOSE: [MACHINENAME]: LCM: [ Start Test  ] [[Script]dothing] 
VERBOSE: [MACHINENAME]: LCM: [ End Test  ] [[Script]dothing] in 0.0050 seconds. 
VERBOSE: [MACHINENAME]: LCM: [ Start Set  ] [[Script]dothing] 
VERBOSE: [MACHINENAME]: LCM: [ End Set  ] [[Script]dothing] in 0.0060 seconds. 
VERBOSE: [MACHINENAME]: LCM: [ End Resource ] [[Script]dothing] 
VERBOSE: [MACHINENAME]: LCM: [ End Set  ] 
VERBOSE: [MACHINENAME]: LCM: [ End Set  ] in 0.0390 seconds. 

人爲的例子不談,我怎麼能只擁有腳本資源執行只有當文件資源實際上做了什麼?

我在這裏的用例是檢查一個文件是否在遠程位置發生了變化,如果是,將它複製到本地機器,然後重新啓動服務。如果文件沒有改變,我顯然不想重新啓動服務,但是我看不到一個好的冪等方法來做到這一點,因爲文件正在使用散列測試 - 我必須有我的服務重新啓動步驟執行相同的文件散列檢查。

回答

3

我不認爲DSC中有任何本地內置的東西,它可以讓一個資源檢測是否在相同配置運行中爲不同資源實際執行了「Set-TargetResource」函數。

但是,以下腳本資源將按照您的要求進行操作,但它們使用全局腳本級別的變量進行通信,因此如果您有多個服務在單個DSC中進行配置,它們會中斷。 ConfigureService腳本資源更新本地配置文件並設置RestartService資源讀取的標誌以檢查是否進行了更改。這有點髒,但它有效。

Script ConfigureService { 
    GetScript = { return $null; } 
    TestScript = { 
     # compare remote and local file contents and return 
     # $true if they match, or $false if they're different 
     $source = "D:\temp\dsctest\source.txt"; 
     $target = "D:\temp\dsctest\target.txt"; 
     $isMatch = [System.IO.File]::ReadAllText($target) -eq [System.IO.File]::ReadAllText($source); 
     # set a flag for the RestartService resource to read 
     $script:serviceChanged = -not $isMatch; 
     write-verbose "isMatch = $isMatch"; 
     return $isMatch; 
    } 
    SetScript = { 
     # overwrite the local file 
     $source = "D:\temp\dsctest\source.txt"; 
     $target = "D:\temp\dsctest\target.txt"; 
     $content = [System.IO.File]::ReadAllText($source); 
     write-verbose "overwriting config"; 
     [System.IO.File]::WriteAllText($target, $content); 
    } 
} 

Script RestartService { 
    DependsOn = "[Script]ConfigureService" 
    GetScript = { return $null; } 
    TestScript = { 
     write-verbose "serviceChanged = $($script:serviceChanged)"; 
     return -not $script:serviceChanged; 
    } 
    SetScript = { 
     # restart the service 
     write-verbose "restarting service"; 
    } 
} 

或者只是把它們全部放到一個資源中。如果您希望將其重複用於多個服務,則可以將其移動到一個完整的ServiceConfiguration定製資源中。

Script ConfigureService { 
    GetScript = { return $null; } 
    TestScript = { 
     # compare remote and local file contents and return 
     # $true if they match, or $false if they're different 
     $source = "D:\temp\dsctest\source.txt"; 
     $target = "D:\temp\dsctest\target.txt"; 
     $isMatch = [System.IO.File]::ReadAllText($target) -eq [System.IO.File]::ReadAllText($source); 
     write-verbose "isMatch = $isMatch"; 
     return $isMatch; 
    } 
    SetScript = { 
     # overwrite the local file 
     $source = "D:\temp\dsctest\source.txt"; 
     $target = "D:\temp\dsctest\target.txt"; 
     $content = [System.IO.File]::ReadAllText($source); 
     write-verbose "overwriting config"; 
     [System.IO.File]::WriteAllText($target, $content); 
     # restart the service 
     write-verbose "restarting service"; 
    } 
} 
+0

同意;沒有本地的方式來有條件地執行基於另一資源的資源。 – briantist 2014-09-29 22:15:39