我不熟悉posting,也不熟悉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_UserScripts\MyServerData.psd1 `
发布于 2018-06-07 01:56:18
您的配置中有一个名为$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_UserScripts\MyServerData.psd1 -ServiceAccountCredential $Credential
发布于 2018-06-07 03:50:08
谢谢@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_UserScripts\MyServerData.psd1 -PackagePath "C:\PowerShell_UserScripts"
https://stackoverflow.com/questions/50724175
复制相似问题