我想知道如何在DSC调用的CMD中引用凭证。 这是我尝试部署的配置,但它没有收到凭据。
configuration SQLCMD
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullorEmpty()]
[PSCredential]
$Credential
)
Import-DscResource -ModuleName xSqlServer
Node localhost
{
LocalConfigurationManager
{
ConfigurationMode = 'ApplyOnly'
RebootNodeIfNeeded = $true
ActionAfterReboot = 'ContinueConfiguration'
AllowModuleOverwrite = $true
}
Script DeployDBmoveTempDB
{
SetScript = {
$SourceFile = 'C:\DatabaseTest.dacpac'
$TargetServerName = 'localhost'
$TargetDatabaseName = 'TestDB1'
$databaseSizeSQLCMD = '200MB'
$databaseLogSizeSQLCMD = '20MB'
$tempdbSizeSQLCMD = '1900MB'
$tempdbLogSizeSQLCMD = '1900MB'
trap {
Write-Error $_
Exit 1
}
$args = @('/Action:Publish'
,"/SourceFile:$SourceFile"
,"/TargetServerName:$TargetServerName"
,"/TargetUser:$Credential.UserName"
,"/TargetPassword:$Credential"
,"/TargetDatabaseName:$TargetDatabaseName"
,"/v:databaseSizeSQLCMD=$databaseSizeSQLCMD"
,"/v:databaseLogSizeSQLCMD=$databaseLogSizeSQLCMD"
,"/v:tempdbSizeSQLCMD=$databaseSizeSQLCMD"
,"/v:tempdbLogSizeSQLCMD=$databaseLogSizeSQLCMD"
,'/p:BlockOnPossibleDataLoss=false'
)
try {
& "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\SqlPackage.exe" $args
}
catch {
Write-Host $_ ;
}
}
TestScript = {
Test-Path D:\TestDB1_primary.mdf
}
GetScript = { <# This must return a hash table #> }
}
}
}
但是,以下配置可以正常工作:
configuration SQLCMD
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullorEmpty()]
[PSCredential]
$Credential
)
Import-DscResource -ModuleName xSqlServer
Node localhost
{
LocalConfigurationManager
{
ConfigurationMode = 'ApplyOnly'
RebootNodeIfNeeded = $true
ActionAfterReboot = 'ContinueConfiguration'
AllowModuleOverwrite = $true
}
Script DeployDBmoveTempDB
{
SetScript = {
$ErrorActionPreference = "Stop"
$SourceFile = 'C:\DatabaseTest.dacpac'
$TargetServerName = 'localhost'
$user_name = 'mySqlAdmin'
$user_pwd = 'blabla'
$TargetDatabaseName = 'TestDB1'
$databaseSizeSQLCMD = '200MB'
$databaseLogSizeSQLCMD = '20MB'
$tempdbSizeSQLCMD = '1900MB'
$tempdbLogSizeSQLCMD = '1900MB'
trap {
Write-Error $_
Exit 1
}
$args = @('/Action:Publish'
,"/SourceFile:$SourceFile"
,"/TargetServerName:$TargetServerName"
,"/TargetUser:$user_name"
,"/TargetPassword:$user_pwd"
,"/TargetDatabaseName:$TargetDatabaseName"
,"/v:databaseSizeSQLCMD=$databaseSizeSQLCMD"
,"/v:databaseLogSizeSQLCMD=$databaseLogSizeSQLCMD"
,"/v:tempdbSizeSQLCMD=$databaseSizeSQLCMD"
,"/v:tempdbLogSizeSQLCMD=$databaseLogSizeSQLCMD"
,'/p:BlockOnPossibleDataLoss=false'
)
try {
& "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\SqlPackage.exe" $args
}
catch {
Write-Host $_ ;
}
}
TestScript = {
Test-Path D:\TestDB1_primary.mdf
}
GetScript = { <# This must return a hash table #> }
}
}
}
答案 0 :(得分:2)
DSC配置将脚本存储为生成的mof中的字符串,并且默认情况下不会扩展变量,因为它不知道要扩展哪些以及作为脚本的一部分保留哪些。但是,通过指定$using:
- Scope,您可以包含配置中定义的变量。在mof-compilcation期间,然后在每个Get- / Set- / TestScript脚本块的开头添加变量。
例如:
configuration SQLCMD
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullorEmpty()]
[PSCredential]
$Credential
)
Import-DscResource -ModuleName xSqlServer
$user_name = $Credential.UserName
$user_pwd = $Credential.GetNetworkCredential().Password
Node localhost
{
Script DeployDBmoveTempDB
{
SetScript = {
$TargetDatabaseName = 'TestDB1'
$args = @(,"/TargetUser:$using:user_name"
,"/TargetPassword:$using:user_pwd"
,"/TargetDatabaseName:$TargetDatabaseName")
try {
& "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\SqlPackage.exe" $args
}
catch { Write-Host $_ }
}
TestScript = { Test-Path "D:\TestDB1_primary.mdf" }
GetScript = { <# This must return a hash table #> }
}
}
}
请注意,密码将以纯文本形式存储在mof文件中。例如:
SetScript = "$user_name ='User1'\n$user_pwd ='Password1'\n \n\n $TargetDatabaseName = 'TestDB1'\n $args = @(,\"/TargetUser:$user_name\"\n
,\"/TargetPassword:$user_pwd\"\n ,\"/TargetDatabaseName:$TargetDatabaseName\") \n\n try {\n & \"C:\\Program File
s (x86)\\Microsoft SQL Server\\130\\DAC\\bin\\SqlPackage.exe\" $args\n }\n catch { Write-Host $_ } \n\n ";