【问题标题】:Passing PSCredential Class as parameter in SqlServerDsc module在 SqlServerDsc 模块中将 PSCredential 类作为参数传递
【发布时间】:2018-06-06 15:28:27
【问题描述】:

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

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 `

【问题讨论】:

    标签: sql-server powershell dsc


    【解决方案1】:

    您的配置中有一个名为$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
    

    【讨论】:

    • 谢谢!我收到此错误消息。我不认为它正在获取我的变量: ConvertTo-SecureString :无法将参数绑定到参数“String”,因为它为空。在 line:3 char:41 + ... Node.Service4SQLPassword | ConvertTo-SecureString -asPlainText -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
    • @JulesSQL 啊我明白了,这是我没有仔细阅读的错。我现在看到您正在从配置数据中获取密码。在那种情况下,我根本不知道您为什么要使用$Using:$Node 应该没问题。但同样在这种情况下,您的配置中不应包含该强制参数。它应该是可选的或完全省略,然后您将您的引用从$ServiceAccountCredential 更改为$Credential(或相反地更改您在转换阶段的分配)。
    • 是的!我现在可以用了,谢谢!我会发布解决方案。
    【解决方案2】:

    谢谢@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" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-17
      • 2013-11-08
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      相关资源
      最近更新 更多