Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

Powershell Script help for Scripting

$
0
0
Hello , Please assists me if any one has idead about this . I have script in powershell which script out SP with specific condition . Want to achieve is drop those SP once the script generation is completed . My script works only for one SP at time and I want it loop properly .if I add $proc.drop() and throws error : An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute..At E:\RD\LVer.ps1:18 char:40+ foreach <<<< ($proc in $procs) + CategoryInfo : InvalidOperation: (Microsoft.SqlSe...dListEnumerator:SmoSortedListEnumerator) [], Runtim eException + FullyQualifiedErrorId : BadEnumeration[[b]u]Actual Script :[/u][/b]$rootDrive = "<Location for scripts>"[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null$MyScripter=new-object ("Microsoft.SqlServer.Management.Smo.Scripter")$srv=New-Object "Microsoft.SqlServer.Management.Smo.Server" "<ServerName>" $dbName = $srv.databases["<Database Name>"] $procs = $dbName.StoredProcedures $MyScripter.Server=$srv #STORED PROCEDURES if($procs -ne $null) { foreach ($proc in $procs) { #Assuming that all non-system stored procs have proper naming convention and don't use prefixes like "sp_" if ( $proc.Name.IndexOf("<any condition>") -eq 0) { $fileName = $proc.name.Replace("\", "") "Scripting SP $fileName" $scriptfile = "$rootDrive\$filename.sql" $MyScripter.Options.FileName = $scriptfile #AppendTofile has to be 'true' in order that all the procs' scripts will be appended at the end $MyScripter.Options.AppendToFile = "False" $MyScripter.Script($proc)|out-null $proc.Drop() } } }

Viewing all articles
Browse latest Browse all 467

Trending Articles