将 PSCredential Class 作为参数传递到 SqlServerDsc 模块中



我是发帖新手,一般对powershell DSC和powershell不熟悉。我正在 SqlServerDsc 模块中测试 SqlServiceAccount 资源,但无法弄清楚如何使用 PSCredential 类。我的目标是在变量中拥有凭据,但无法弄清楚如何正确执行此操作。我查看了该示例并阅读了其 github 上资源的 psm1,但仍然丢失了。 这是我用来测试它的代码,密码以及其他信息位于该脚本底部调用的另一个脚本中。弹出提示我输入凭据的窗口,但我希望将我的凭据放在变量中以填充此窗口。

Configuration SQLInstall
{param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]
$PackagePath,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$ServiceAccountCredential
)
Import-DscResource –ModuleName PSDesiredStateConfiguration
Import-DSCResource -ModuleName ComputerManagementDsc
Import-DSCResource -ModuleName SqlServerDsc
Node $AllNodes.where{ $_.Role.Contains("SQLENGINE") }.NodeName
{
Log ParamLog
{
Message = "Running SQLInstall. PackagePath = $PackagePath"
}
# Password info here
$password = $using:Node.Service4SQLPassword | ConvertTo-SecureString -asPlainText -Force 
$username = $using:Node.Service4SQLAccount 
$Credential = New-Object System.Management.Automation.PSCredential($username,$password)
SqlServiceAccount SetServiceAccount_DatabaseEngine
{
ServerName     = $Node.NodeName
InstanceName   = 'MSSQLSERVER'
ServiceType    = 'DatabaseEngine'
ServiceAccount = $ServiceAccountCredential
RestartService = $true
Force          = $true
DependsOn      = "[Script]InstallSQLServer"
}
SqlServiceAccount SetServiceAccount_SQLServerAgent
{
ServerName     = $Node.NodeName
InstanceName   = 'MSSQLSERVER'
ServiceType    = 'SQLServerAgent'
ServiceAccount = $ServiceAccountCredential
RestartService = $true
Force          = $true
DependsOn      = "[Script]InstallSQLServer"
}
SqlServiceAccount SetServiceAccount_IntegrationServices
{
ServerName     = $Node.NodeName
InstanceName   = 'MSSQLSERVER'
ServiceType    = 'IntegrationServices'
ServiceAccount = $ServiceAccountCredential
RestartService = $true
Force          = $true
DependsOn      = "[Script]InstallSQLServer"
}               
}}
SQLInstall -ConfigurationData C:PowerShell_UserScriptsMyServerData.psd1 `

配置中有一个名为$ServiceAccountCredential的必需参数,用于配置。

但是在中间的某个地方,你填充了一个名为$Credential的变量,但你不会在任何地方使用它。将其移出Configuration定义(它不属于那里(,然后将其作为参数传递:

# Password info here
$password = $using:Node.Service4SQLPassword | ConvertTo-SecureString -asPlainText -Force 
$username = $using:Node.Service4SQLAccount 
$Credential = New-Object System.Management.Automation.PSCredential($username,$password)
Configuration SQLInstall    
{
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]
$PackagePath,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$ServiceAccountCredential
)
<# ... #>
}
SQLInstall -ConfigurationData C:PowerShell_UserScriptsMyServerData.psd1 -ServiceAccountCredential $Credential

谢谢你@briantist!经过几次调整后,它现在可以工作了,这是解决方案

Configuration SQLInstall                                               
{param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]
$PackagePath
)
Import-DscResource –ModuleName PSDesiredStateConfiguration
Import-DSCResource -ModuleName ComputerManagementDsc
Import-DSCResource -ModuleName SqlServerDsc

Node $AllNodes.where{ $_.Role.Contains("SQLENGINE") }.NodeName
{
Log ParamLog
{
Message = "Running SQLInstall. PackagePath = $PackagePath"
}

$password = $Node.Service4SQLPassword | ConvertTo-SecureString -asPlainText -Force 
$username = $Node.Service4SQLAccount 
$ServiceAccountCredential = New-Object System.Management.Automation.PSCredential($username,$password)
SqlServiceAccount SetServiceAccount_DatabaseEngine
{
ServerName     = $Node.NodeName
InstanceName   = 'MSSQLSERVER'
ServiceType    = 'DatabaseEngine'
ServiceAccount = $ServiceAccountCredential
RestartService = $true
Force          = $true
#DependsOn     = "[Script]InstallSQLServer"
}
SqlServiceAccount SetServiceAccount_SQLServerAgent
{
ServerName     = $Node.NodeName
InstanceName   = 'MSSQLSERVER'
ServiceType    = 'SQLServerAgent'
ServiceAccount = $ServiceAccountCredential
RestartService = $true
Force          = $true
#DependsOn     = "[Script]InstallSQLServer"
}
SqlServiceAccount SetServiceAccount_IntegrationServices
{
ServerName     = $Node.NodeName
InstanceName   = 'MSSQLSERVER'
ServiceType    = 'IntegrationServices'
ServiceAccount = $ServiceAccountCredential
RestartService = $true
Force          = $true
#DependsOn     = "[Script]InstallSQLServer"
}

}                                                                 
}SQLInstall -ConfigurationData C:PowerShell_UserScriptsMyServerData.psd1 -PackagePath "C:PowerShell_UserScripts" 

最新更新