简体   繁体   English

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

[英]How to pass one function variable data into another function using PowerShell?

I've written function mirroring that will call one of a number of other functions. 我已经编写了函数mirroring ,它将调用许多其他函数之一。 To save re-writing function 'A', I'd like to pass the function to be called as a parameter of function 'A'. 为了保存重写函数“ A”,我想将要调用的函数作为函数“ A”的参数传递。 For example: 例如:

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 . 我的问题是如何在我的函数InsertServerStatus使用$ServerStatus1 What is it I'm doing wrong? 我做错了什么?

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变量的值

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"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM