将PSCredential类作为参数传递给SqlServerDsc模块?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (94)

我正在测试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 `
提问于
用户回答回答于

在配置中$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
用户回答回答于

这是一个解决方案

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" 

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励