我無法從其他文件加載並執行函數。現在函數使用一個「虛擬」字符串變量,在主腳本中給出一個值(所以函數本身中的參數只是一個空的佔位符字符串,在主腳本中給出了值。PowerShell - 使用另一個腳本文件的param調用和執行函數
主腳本:下面的代碼之後更遍歷實例($ InstanceList)的列表,併爲每個$例如,它會嘗試連接,然後應與參數$實例
#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
}
執行函數
功能腳本它基本上查詢一個sql表並找到實例,然後如果這些$ ins tances等於$ prodQuery數組中的第一條記錄,然後它補充說,例如一組在中央管理服務器(SQL的東西)($實例是虛擬佔位符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通過自上而下逐行執行,所以我不知道它是我設置主腳本的方式,還是如何設置函數。
我改變它到你說的話,並把功能扔到同一個文件。我現在正在獲取指向$ prodQuery = Invoke-Sqlcmd和$ prodResult行的函數中的連接錯誤,這些行聲明存在與網絡有關的錯誤。如果我把所有的東西都拿出來放入循環中,我就不會收到這個錯誤並且它會被填充,但是我需要在函數中使用這些東西,因爲它需要爲未來設置。 – Skunny11
您應該使用當前代碼和確切的錯誤更新您的原始問題(顯然,服務器/數據庫名稱可能因隱私而更改)。 – TheMadTechnician
我得到它的工作,必須將函數參數放在括號中 – Skunny11