Windows PowerShell Desired State Configuration (DSC) 新手在这里。MOF 文件生成失败并显示:
PSDesiredStateConfiguration\Node : The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At line:13 char:5 + Node localhost + ~~~~ + CategoryInfo : MetadataError: (:) [PSDesiredStateConfiguration\node], ParentContainsErrorRecordException + FullyQualifiedErrorId : ArgumentIsNull,PSDesiredStateConfiguration\node Compilation errors occurred while processing configuration 'SQLConfig'. Please review the errors reported in error stream and modify your configuration code appropriately. At C:\windows\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:3917 char:5 + throw $ErrorRecord + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (SQLConfig:String) [], InvalidOperationException + FullyQualifiedErrorId : FailToProcessConfiguration
脚本
Configuration SQLConfig
{
param(
[Parameter(Mandatory =$true)][string[]]$serviceConfig,
[Parameter(Mandatory =$true)][string]$DataDrive,
[Parameter(Mandatory =$true)][string]$LogDrive
)
Import-DscResource -ModuleName SqlServerDsc
Import-DscResource -ModuleName PSDesiredStateConfiguration
Node localhost
{
WindowsFeature Net35
{
Name = 'NET-Framework-Core'
Ensure = 'Present'
}
WindowsFeature Net45
{
Name = 'NET-Framework-45-Core'
Ensure = 'Present'
}
WindowsFeature Cluster
{
Name = 'RSAT-Clustering'
Ensure = 'Present'
}
File datadrive
{
Type = 'Directory'
DestinationPath = $DataDrive
Ensure ='Present'
}
File logdrive
{
Type = 'Directory'
DestinationPath = $LogDrive
Ensure ='Present'
}
SqlDatabaseDefaultLocation dataPath
{
InstanceName = 'MSSQLSERVER'
Path = $DataDrive
ServerName = 'localhost'
Type = 'Data'
DependsOn = '[File]datadrive'
}
SqlDatabaseDefaultLocation logPath
{
InstanceName = 'MSSQLSERVER'
Path = $LogDrive
ServerName = 'localhost'
Type = 'Log'
DependsOn = '[File]logdrive'
}
foreach ($service in $serviceConfig) {
ServiceSet $service
{
Name = $service.ServiceName
State = $service.State
StartupType = $service.StartupType
Ensure = $service.Ensure
}
}
}
}
#region create configuration data
$serviceConfig=(
@{ServiceName='MSSQLSERVER';State='Running';StartupType='Automatic';Ensure='Present'},
@{ServiceName='SQLSERVERAGENT';State='Running';StartupType='Automatic';Ensure='Present'},
@{ServiceName='SQLBrowser';State='Ignore';StartupType='Disabled';Ensure='Present'}
)
SQLConfig -serviceConfig $serviceConfig -DataDrive "F:\Data" -LogDrive "H:\Log" -OutputPath "C:\dump"
您的配置函数中必须更正的几件事:
serviceConfig
必须是类型[object[]]
才能接受哈希表。使用[string[]]
PowerShell 无法正确处理对该对象的迭代,因为它是一个文字对象,而不仅仅是一个字符串数组。ServiceSet
资源的声明只是传入$service
;[System.Collections.Hashtable]
在 PowerShell 术语中,它每次都只是传递,因此不是唯一的。在我的系统上测试并成功运行的配置的更正版本: