将凭据传递给函数Azure Powershell的问题

时间:2017-03-12 20:12:03

标签: powershell azure azure-powershell

大家好我试图将服务器登录凭据传递给我的' createSQLServer函数但是仍然遇到错误'Cannot process argument transformation on parameter 'creds'.userName''我尝试过很多不同的问题,甚至尝试使用& #39; param block'但是卡住了。欢迎向正确的方向发展,欢呼。

###### SQL SERVER LOGIN CREDENTIALS
$userName = "aaron"
$password = "Password_1234" 
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $securePassword

### SQL server names with resource group name appeneded
$server1 = "sqlserver1" + $resGroup
$serVerion = "12.0"

function createSQLserver ([string]$server1,[string]$server2, [string]$server3, [System.Management.Automation.PSCredential]$creds,[string]$server1Location, [string]$server2Location, [string]$server3Location, [string]$resGroup, [string]$serVerion, [string]$userName, [string]$password, [SecureString]$securePassword)
  {
       #Create server 1(Server A)
    <#check to see if server exists - It exists, $continue is created and passed to
    if statement to append two random characters to name#>
    Write-Host "Creating First SQL Server"

      $sqlServer = New-AzureRmSqlServer -ServerName $server1 -SqlAdministratorCredentials $creds -Location $server1Location -ResourceGroupName $resGroup -ServerVersion $serVerion -ErrorVariable continue -ErrorAction SilentlyContinue

    if ($continue)
    {
     do {
         $server1 = $server1 + (rand)
       $sqlServer = New-AzureRmSqlServer -ServerName $server1 `
     -SqlAdministratorCredentials $creds -Location $server1Location `
     -ResourceGroupName $resGroup -ServerVersion "12.0" -ErrorVariable continue -ErrorAction SilentlyContinue
      }
      until(!$continue)

     Write-Host 'exists creating new' $server1 'Created'
    }else{
    Write-Host $server1 ' Created'
    }
      Start-Sleep -s 2
  }


  createSQLserver $server1 $username $password $securePassword $creds $server1Location $resGroup $serVerion

1 个答案:

答案 0 :(得分:2)

您需要使用命名参数!

这是您的前几个参数的片段:

...
[string]$server1
,
[string]$server2
,
[string]$server3
,
[System.Management.Automation.PSCredential]$creds
...

然后是你传入函数调用的那些

createSQLserver $server1 $username $password $securePassword ...

因为您没有使用参数的 名称 ,所以他们使用的是相对序号,即

param    | value
---------+----------------
$server1 | $server1
$server2 | $username
$server3 | $password
$creds   | $securePassword

那我们学到了什么?

始终使用命名参数!

createSQLserver -server1 $server1 -username $username -password $password -securePassword $securePassword

这应该让你排除:-)