我想使用 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}