0
我想在我的函數中創建一個服務器smo對象,然後使用它在傳入的scriptblock中做一些有用的事情。之後,服務器將被刪除。我想設計我的功能類似於模板設計模式實現。我的代碼列在下面,我不確定是否在腳本塊中使用$ server變量。任何人都可以幫忙?謝謝。如何在Powershell中使用模板設計模式?
function test{ [CmdletBinding()] param ( [Parameter(Mandatory = $true, Position = 0)] [object] $instance, [Parameter(Mandatory = $true, Position = 1)] [scriptblock] $script ) [Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') $instance # do something with $script Remove-Variable -Name $server }