繁体   English   中英

如何在没有Powershell的情况下创建和执行DSC

[英]How to create and execute dsc without powershell

我想使用C#作为创建和应用DSC的语言。 到目前为止,我唯一想到的解决方案是从代码中调用powershell。

有没有办法不通过powershell来做到这一点?

您可以使用MSFT_DscLocalConfigurationManager CIM类以及该类中的方法执行与PowerShell cmdlet相同的工作。 这需要以某种方式生成的MOF文件。 可以使用声明性配置脚本,也可以手工制作! :)这是将配置推送到远程系统的示例。 哦! 我在这里使用PowerShell,但是您也可以使用C#来实现!

因此,如果您不想使用PowerShell生成MOF,则只需找到一种在C#中执行此操作的方法。

Configuration DemoConfig {
    Node Test {
        File FileDemo {
            DestinationPath = "C:\Windows\System32\Drivers\Etc\Hosts.backup"
            Contents = ""
            Ensure = "Present"
        }
    }
}

$mof = DemoConfig

$configurationData = [Byte[]][System.IO.File]::ReadAllBytes((Resolve-Path $mof.FullName))
$buffSize = 0
$totalSize = [System.BitConverter]::GetBytes($configurationData.Length + 4 + $buffSize)
$configurationData = $totalSize + $configurationData

Invoke-CimMethod -ComputerName Server01 –Namespace "root/Microsoft/Windows/DesiredStateConfiguration" -ClassName "MSFT_DSCLocalConfigurationManager" -MethodName "SendConfigurationApply" -Arguments @{ConfigurationData = $configurationData; Force = $true} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM