我可以在自定义DSC资源中的get,set,test方法之间共享变量吗?

时间:2016-11-03 11:04:48

标签: powershell dsc

我有一个简单的问题。我一直在尝试使用Powershell DSC,我创建了一个自定义资源来更改客户端计算机的CD驱动器号。

我目前的代码有效,但我想看看它是否更有效率。请参阅下面的代码:

enum Ensure 
{ 
    Absent 
   Present 
}

[DscResource()]
class ChangeCDDriveLetter {

[DscProperty(Key)]
[string]$RequiredCDDriveLetter
[DscProperty(NotConfigurable)]
[string]$CurrentCDDriveLetter
[DscProperty(NotConfigurable)]
[string]$CurrentCDVolume

[ChangeCDDriveLetter] Get() {

    $This.CurrentCDDriveLetter = $This.CheckCurrentDriveLetter()
    $This.CurrentCDVolume = $This.CheckCurrentVolume($This.CurrentCDDriveLetter)

    Return $This

}

[Void] Set(){

    $This.CurrentCDDriveLetter = $This.CheckCurrentDriveLetter()
    $This.CurrentCDVolume = $This.CheckCurrentVolume($This.CurrentCDDriveLetter)

    mountvol $This.CurrentCDDriveLetter /d
    mountvol $This.RequiredCDDriveLetter $This.CurrentCDVolume

}

[bool] Test(){

    $This.CurrentCDDriveLetter = $This.CheckCurrentDriveLetter()
    $This.CurrentCDVolume = $This.CheckCurrentVolume($This.CurrentCDDriveLetter)

    Write-Verbose ("Current CD drive letter is: " + $This.CurrentCDDriveLetter)

    if ($This.CurrentCDDriveLetter -ne $This.RequiredCDDriveLetter){

        Write-Verbose ("The CD drive letter should be: " + $This.RequiredCDDriveLetter)
        Write-Verbose ("Setting the CD drive letter to: " + $This.RequiredCDDriveLetter)
        Return $False
    }
    else { 

        Write-Verbose "This is the correct drive letter."
        Return $True 

    }     
}

[string] CheckCurrentDriveLetter(){

    $CurrentDriveLetter = (Get-WmiObject win32_logicaldisk -filter 'DriveType=5').DeviceID
    return $CurrentDriveLetter

}

[string] CheckCurrentVolume($DriveLetter){

    $Volume = (mountvol $DriveLetter /l).Trim()
    Return $Volume

}

}

我的问题是关于以下重复的行:

$This.CurrentCDDriveLetter = $This.CheckCurrentDriveLetter()

如您所见,此信息在Set,Get和Test方法中收集。

是否可以收集此信息一次并在其他方法中共享此信息?

所以我会在Test方法中收集信息,并将此信息传递给Set方法?我想这样做不会对当前资源产生太大影响,但我仍然想知道它是否可行,以便我可以在将来的资源中使用它来使它们尽可能高效。

提前致谢!

亲切的问候, Bob Smienk

2 个答案:

答案 0 :(得分:0)

每次DSC运行资源时,您都不应该假设您将拥有相同的实例。因此,在执行之间保持实例字段或实例属性存在没有好方法

如果该值对于机器是静态的,您可以尝试将其存储在静态字段中,但同样不应该假设调用这些方法的顺序。

答案 1 :(得分:0)

我所做的和将要做的是基于OOP。在Test()和Set()方法的开头调用Get(),从而设置属性:

[DscResource()]
class ChangeCDDriveLetter {

[DscProperty(Key)]
[string]$RequiredCDDriveLetter
[DscProperty(NotConfigurable)]
[string]$CurrentCDDriveLetter
[DscProperty(NotConfigurable)]
[string]$CurrentCDVolume

[ChangeCDDriveLetter] Get() {

    $This.CurrentCDDriveLetter = $This.CheckCurrentDriveLetter()
    $This.CurrentCDVolume = $This.CheckCurrentVolume($This.CurrentCDDriveLetter)

    Return $This

}

[Void] Set(){

    $This.Get()

    # Replaced by $This.Get()
    # $This.CurrentCDDriveLetter = $This.CheckCurrentDriveLetter()
    # $This.CurrentCDVolume = $This.CheckCurrentVolume($This.CurrentCDDriveLetter)

    mountvol $This.CurrentCDDriveLetter /d
    mountvol $This.RequiredCDDriveLetter $This.CurrentCDVolume

}

[bool] Test(){

    $This.Get()

    # Replaced by $This.Get()
    # $This.CurrentCDDriveLetter = $This.CheckCurrentDriveLetter()
    # $This.CurrentCDVolume = $This.CheckCurrentVolume($This.CurrentCDDriveLetter)

    Write-Verbose ("Current CD drive letter is: " + $This.CurrentCDDriveLetter)

    if ($This.CurrentCDDriveLetter -ne $This.RequiredCDDriveLetter){

        Write-Verbose ("The CD drive letter should be: " + $This.RequiredCDDriveLetter)
        Write-Verbose ("Setting the CD drive letter to: " + $This.RequiredCDDriveLetter)
        Return $False
    }
    else { 

        Write-Verbose "This is the correct drive letter."
        Return $True 

    }     
}
}