簡體   English   中英

PowerShell-從另一個腳本文件使用參數調用和執行功能

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

我無法讓腳本從另一個文件加載並執行功能時遇到麻煩。 現在,該函數正在使用一個“虛擬”字符串變量,該變量在主腳本中具有一個值(因此該函數本身的參數只是一個空的占位符字符串,在主腳本中具有該值。在此先感謝!下面的代碼后有更多內容:

主腳本:遍歷實例列表($ 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腳本 ,它基本上查詢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()
    }

我收到錯誤“ getProdInstances”未被識別為cmdlet,函數的名稱,等等等等。 指向主腳本所在的行。 我知道PS是逐行自上而下地執行的,所以我不確定這是否是我設置主腳本的方式,還是我如何設置功能。

抱歉,我沒有回答,而是發表評論,因此您可能已看到已刪除的評論。

您正在加載函數的行末尾不需要分號,並且路徑前不應該有該句號。 嘗試:

. C:\Scripts\CMS_Functions\Environment_Functions.ps1

看看是否沒有為您加載該功能,然后解決該問題。 或者只是將函數放在腳本的開頭,而不是將其分解到自己的文件中,以避免一起出現問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM