简体   繁体   English

PowerShell-从另一个脚本文件使用参数调用和执行功能

[英]PowerShell - Calling and Executing a Function with param from another Script file

I am having trouble getting a Script to load and execute a function from another file. 我无法让脚本从另一个文件加载并执行功能时遇到麻烦。 Right now the Function is using a "dummy" string variable which is given a value in the main script (so the param in the function itself is just a empty placeholder string that is given value in the main script. Thanks in advance! Ill explain more after the code below: 现在,该函数正在使用一个“虚拟”字符串变量,该变量在主脚本中具有一个值(因此该函数本身的参数只是一个空的占位符字符串,在主脚本中具有该值。在此先感谢!下面的代码后有更多内容:

MAIN Script: iterates through a list of instances ($InstanceList) and for each $instance it tries to connect and then should execute the function with the parameter $InstanceName 主脚本:遍历实例列表($ InstanceList),并为每个$ instance尝试连接,然后应使用参数$ InstanceName执行函数

   #Loads Server Management Libraries
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Management.RegisteredServers') | Out-Null

    #Adds SQL Snapins
    Add-PSSnapin SqlServerCmdletSnapin100
    Add-PSSnapin SqlServerProviderSnapin100

    #Loads CMS Function File
    . .C:\Scripts\CMS_Functions\Environment_Functions.ps1;

    #Variable for CMS Instance
    $CMSInstance = 'CMSInstanceName';

    #Variable for the list of sql instances and their path
    $InstanceList = Get-Content "C:\Scripts\InPutFiles\instancestest.txt";

    foreach($instance in $InstanceList)
    {               
        #Creates a Server Management Object for the given $instance
        $instanceObject = New-Object Microsoft.SqlServer.Management.Smo.Server($instance)

        #Obtains the name of the instance from the sql server object
        $InstanceName = $instanceObject.Name

        #Connects to the Central Management Registered Server Instance
        $connectionString = "Data Source=$CMSInstance;Initial Catalog=master;Integrated Security=SSPI;"
        $sqlConnection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
        $conn = New-Object System.Data.SqlClient.SqlConnection("Server=$CMSInstance;Database=master;Integrated Security=True")
        $CMSStore = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore($conn)

    #Call Function
        getProdInstances $InstanceName
    }

FUNCTION Script which basically queries a sql table and finds instances and then if those $instances equal the first record in the $prodQuery array then it adds that instance to a group in a central management server (sql stuff) ($instance is the dummy placeholder param) FUNCTION脚本 ,它基本上查询sql表并查找实例,然后,如果那些$ instances等于$ prodQuery数组中的第一条记录,则它将该实例添加到中央管理服务器(sql东西)中的组($ instance是虚拟占位符PARAM)

function getProdInstances $instance
{   
    $prodQuery = "SELECT DISTINCT INSTANCE
              FROM TABLE where INSTANCE = '$instance'
              ORDER BY INSTANCE;"

    $prodQuery = Invoke-Sqlcmd -Query $prodQuery;
    $prodResult = $prodQuery[0]

    if($prodResult -eq $instance)
    {
        #Variable for the highest level group directory
        $CMSDBStore = $CMSStore.ServerGroups["DatabaseEngineServerGroup"].ServerGroups["By Environment"].ServerGroups["PROD"]

        #Sets the Registered Server Variables
        $RegServerName = $prodResult
        $RegServerInstance = $RegServerName

        #Creates a Server Management Object for the Registered Server Group and Instance to be added to it
        $NewServer = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer($CMSDBStore, "$RegServerName")

        #Creates a secure connection to the Registered Server Instance
        $NewServer.SecureConnectionString = "server=$RegServerInstance;integrated security=true"
        $NewServer.ConnectionString = "server=$RegServerInstance;integrated security=true"
        $NewServer.ServerName = "$RegServerInstance"

        #Displays information to the command prompt that the instanec $RegServerName has been added to the $CMSInstance
        Write-Host -ForegroundColor DarkGreen "Adding $RegServerName to $CMSInstance";

        #Adds the instance to the Registered Server CMS
        $NewServer.Create()
    }

I get the error 'getProdInstances' is not recognized as the name of a cmdlet, function, blah blah blah. 我收到错误“ getProdInstances”未被识别为cmdlet,函数的名称,等等等等。 pointing to the line of the main script where I call it. 指向主脚本所在的行。 I know PS executes by top down line by line so im not sure if its a way I am setting up the main script, or how im setting the function up. 我知道PS是逐行自上而下地执行的,所以我不确定这是否是我设置主脚本的方式,还是我如何设置功能。

Sorry, I commented instead of answering, so you may have seen a deleted comment. 抱歉,我没有回答,而是发表评论,因此您可能已看到已删除的评论。

Your line that's loading the function should not need that semi-colon at the end, and it shouldn't have that period immediately before the path. 您正在加载函数的行末尾不需要分号,并且路径前不应该有该句号。 Try: 尝试:

. C:\Scripts\CMS_Functions\Environment_Functions.ps1

See if that doesn't load the function for you, and resolve the issue. 看看是否没有为您加载该功能,然后解决该问题。 Or just drop the function in the beginning of the script instead of breaking it out into it's own file to avoid the issue all together. 或者只是将函数放在脚本的开头,而不是将其分解到自己的文件中,以避免一起出现问题。

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

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