如何使用PowerShell将一个函数变量数据传递到另一个函数?

时间:2023-02-04 20:55:59

I've written function mirroring that will call one of a number of other functions. To save re-writing function 'A', I'd like to pass the function to be called as a parameter of function 'A'. For example:

我编写了函数镜像,它将调用许多其他函数之一。为了保存重写函数'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()
}

My second function is

我的第二个功能是

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

My question is how can I use my $ServerStatus1 in my function InsertServerStatus. What is it I'm doing wrong?

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

2 个解决方案

#1


0  

Aft is right, you have to call the function with all the parameters. You should call your function like this:

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

InsertServerStatus $ServerName $InstanceName $ServerStatus1 $RemActonToBeTaken

What you are doing is this:

你在做什么是这样的:

InsertServerStatus $ServerStatus1

So the $ServerName parameter is getting the values of the ServerStatus1 variable

因此$ ServerName参数获取ServerStatus1变量的值

#2


0  

Dear All Thank for reply i got the answer is like that

亲爱的所有感谢回复我得到了答案就是这样

Pass variable name in to parent function

将变量名称传递给父函数

$script:ServerStatus1 = "" 

into the child function

进入孩子的功能

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

) 
{
(Parent Function) mirroring 

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

#1


0  

Aft is right, you have to call the function with all the parameters. You should call your function like this:

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

InsertServerStatus $ServerName $InstanceName $ServerStatus1 $RemActonToBeTaken

What you are doing is this:

你在做什么是这样的:

InsertServerStatus $ServerStatus1

So the $ServerName parameter is getting the values of the ServerStatus1 variable

因此$ ServerName参数获取ServerStatus1变量的值

#2


0  

Dear All Thank for reply i got the answer is like that

亲爱的所有感谢回复我得到了答案就是这样

Pass variable name in to parent function

将变量名称传递给父函数

$script:ServerStatus1 = "" 

into the child function

进入孩子的功能

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

) 
{
(Parent Function) mirroring 

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