[13]Windows PowerShell DSC学习系列---直接调用DSC的资源方法

在本章节中,将会讲述如何直接调用DSC的资源方法。翻译来源:

https://msdn.microsoft.com/en-us/powershell/dsc/directcallresource

在PowerShell DSC 5.x中,我们能够使用Invoke-DscResource 这个DSC的命令去直接调用DSC 资源文件中定义的方法或者函数(如果是基于MOF的资源的话,其里面包含Get-TargetResourceSet-TargetResource, 和Test-TargetResource,如果是基于类描述的DSC资源文件,其里面包含的方法是GetSet, 和Test方法)。 当我们需要使用第三方应用程序直接调用DSC资源的方法的时候,或者在开发DSC的资源的时候,Invoke-DscResource特别的有用。

Invoke-DscResource的一个典型的应用场景就是和refreshMode = 'Disabled'配合使用(通过Get-DscLocalConfigurationManag

er/Set-DscLocalConfigurationManager命令操作).

当我们调用 Invoke-DscResource命令的时候,我们能指定Method的参数,同时我们也能传入一个哈希表指定Property的参数。下面是一些直接调用DSC资源的例子。

确认一个文件是否存在$result = Invoke-DscResource -Name File -Method Set -Property @{DestinationPath = "$env:SystemDrive\\DirectAccess.txt";Contents = 'This file is create by Invoke-DscResource'} -Verbose$result | fl测试一个文件是否存在$result = Invoke-DscResource -Name File -Method Test -Property @{DestinationPath="$env:SystemDrive\\DirectAccess.txt";Contents='This file is create by Invoke-DscResource'} -Verbose$result | fl获取文件的内容$result = Invoke-DscResource -Name File -Method Get -Property @{DestinationPath="$env:SystemDrive\\DirectAccess.txt";Contents='This file is create by Invoke-DscResource'} -Verbose$result.ItemValue | fl

注意:  Invoke-DscResource不支持直接调用组合资源中的方法。相应的,我们需要调用用来组合资源的方法。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值