【问题标题】:How to pass one function variable data into another function using PowerShell?如何使用 PowerShell 将一个函数变量数据传递给另一个函数?
【发布时间】:2017-02-08 17:47:27
【问题描述】:

我编写了函数mirroring,它将调用许多其他函数之一。为了节省重写函数'A',我想将要调用的函数作为函数'A'的参数传递。例如:

function mirroring (
    [string] $svr="xxxx",
    [string] $inst="MSSQLSERVER2",
    [string] $datastore,
    [string] $datastore1,
    [string] $datastore2,
    [string] $datastore3,
    [string] $datastore4,
    [string] $datastore5,
    [string] $datastore6,
    [string] $datastore7,
    [string] $datastore8,
    [string] $datastore9,
    [string] $ServerStatus1
)
{
    Set-StrictMode -Version 2
    [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
    [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
    [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")

    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection "Server=$svr\$inst;Database=master;Integrated Security=True;"  
    $SqlConnection.Open()
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = " SELECT  db_name(sd.[database_id])AS [Database Name]
            ,sd.mirroring_state                  AS [Mirror State Number]
            ,sd.mirroring_state_desc             AS [Mirror State] 
            ,sd.mirroring_partner_name           AS [Partner Name]
            ,sd.mirroring_role_desc              AS [Mirror Role]  
            ,sd.mirroring_safety_level_desc      AS [Safety Level]
            ,sd.mirroring_witness_name           AS [Witness]
            ,sd.mirroring_connection_timeout AS [Timeout(sec)]
        FROM sys.database_mirroring AS sd
        WHERE mirroring_guid IS NOT null
        ORDER BY [Database Name];" 
    $SqlCmd.Connection = $SqlConnection
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $DataSet.Tables[0]
    $datastore = $DataSet.Tables[0].Rows[0][0]
    $datastore1 = $DataSet.Tables[0].Rows[0][1] 
    $datastore2 = $DataSet.Tables[0].Rows[0][2]  
    $datastore3 = $DataSet.Tables[0].Rows[0][3]
    $datastore4 = $DataSet.Tables[0].Rows[0][4]
    $datastore5 = $DataSet.Tables[0].Rows[0][5]
    $datastore6 = $DataSet.Tables[0].Rows[0][6]
    $datastore7 = $DataSet.Tables[0].Rows[0][7]

    if ($datastore2 -eq "Disconnected")
    {
        DisconnectedREMEDIATION
    }
    elseif ($datastore2 -eq "SYNCHRONIZED")
    {
        #$ServerStatus="The Instance Is in Synchronized State"
        #$RemActonToBeTaken=1
        SYNCHRONIZEDREMEDIATION
    }

    $ServerStatus1 = "DataBase Name:"+ $datastore+",Mirror State Number"+$datastore1+",Mirror State"+$datastore2+",Partner Name"+$datastore3+",Mirror Role"+$datastore4+",Safety Level"+$datastore5+",Witness"+$datastore6+",Timeout(In Sec)"+$datastore7
    InsertServerStatus $ServerStatus1
    return $ServerStatus1
    $SqlConnection.Close()
}

我的第二个功能是

function InsertServerStatus(
    $ServerName="LAPTOP6\MSSQLSERVER2",
    $InstanceName, 
    $ServerStatus1, 
    $RemActonToBeTaken
)
{
    Write-Host $ServerStatus1
}

我的问题是如何在我的函数InsertServerStatus 中使用我的$ServerStatus1。我做错了什么?

【问题讨论】:

  • 调用时,您需要传递与函数中使用的完全相同的参数。所以你的函数调用应该是-InsertServerStatus $ServerName $InstanceName $ServerStatus1 $RemActonToBeTaken

标签: sql-server-2008 powershell window


【解决方案1】:

Aft 是对的,你必须调用带有所有参数的函数。 你应该这样调用你的函数:

InsertServerStatus $ServerName $InstanceName $ServerStatus1 $RemActonToBeTaken

你正在做的是这样的:

InsertServerStatus $ServerStatus1

所以 $ServerName 参数正在获取 ServerStatus1 变量的值

【讨论】:

  • $ServerStatus1 中的串联 = "DataBase Name:"+ $datastore+",Mirror State Number"+$datastore1+",Mirror State"+$datastore2+",Partner Name"+$datastore3+",Mirror角色"+$datastore4+",安全级别"+$datastore5+",见证"+$datastore6+",超时(In Sec)"+$datastore7
【解决方案2】:

亲爱的,谢谢你的回复,我得到的答案是这样的

将变量名传递给父函数

$script:ServerStatus1 = "" 

进入子函数

function InsertServerStatus(
 $ServerName="LAPTOP6\MSSQLSERVER2",
 $InstanceName, 
 $RemActonToBeTaken

) 
{
(Parent Function) mirroring 

 $Script:ServerStatus= $ServerStatus1
"You Code"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-08
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多