PowerShell
PowerShell - 使用另一个脚本文件中的param调用和执行函数(PowerShell - Calling and Executing a Function with param from another Script file)我无法让脚本从另一个文件加载和执行函数。 现在函数正在使用一个“虚拟”字符串变量,该变量在主脚本中被赋予一个值(因此函数本身中的参数只是一个空的占位符字符串,在主脚本中给定值。提前感谢!我解释更多代码如下:
主脚本:遍历实例列表($ InstanceList),并尝试连接每个$实例,然后使用参数$ 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 stuff)($ 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逐行自上而下执行所以我不确定它是否是我设置主脚本的方式,或者我如何设置该功能。
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
#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 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. 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.
最满意答案对不起,我评论过而不是回答,所以你可能已经看到了删除的评论。
加载函数的行不应该在结尾处使用该分号,并且它不应该在路径之前的那个句点之前。 尝试:
. C:\Scripts\CMS_Functions\Environment_Functions.ps1看看是否没有为您加载该功能,并解决问题。 或者只是将函数放在脚本的开头而不是将其分解到它自己的文件中以避免一起出现问题。
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.ps1See 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.