Para quem leu meu post anterior de Gerar DACPAC usando Powershell percebeu que é um código muito simples, passível de erros como:
- digitar o nome do banco errado,
- símbolos nos nomes dos bancos,
- não escolher um local ou o nome do servidor ele gera erro no script…
coisas bem primárias…
Dessa vez fiz algumas melhorias,,, como:
- Tela mais simpática
- conecta no servidor de banco e lista as bases
- multi seleção de bases
- não deixa você continuar se não selecionar um destino e colocar o nome do servidor
- Não fecha sozinho
- deixa na tela do posh o código para, caso precise, você copie e execute novamente
- coloca o mouse como ampulheta
- abre o diretório de destino quando acaba a tarefa
- e localiza o executável do gerador de dacpac,,, desde que esteja no C:\
Uma coisa muito importante, o código é livre para modificação, sinta-se a vontade de fazer qualquer modificação que queira,, se achar que sua versão ficou melhor, ou achou algum problema e corrigiu, mande o código, vamos melhorar,,,,
Existe um detalhe para rodar esta versão sem problemas,,, você precisa ter o Windows atualizado além do Windows Management Framework 4.0 e do .Net Frame Work 4.5.2.
cls $data = get-date -format "_yyyyMMdd" $WindowTitle = "Gerador de DACPAC" $LabelPath = "Caminho:" $LabelServer = "Servidor:" $aplicacao = (Get-ChildItem -Path "C:\Program Files (x86)\Microsoft SQL Server" -Include "SqlPackage.exe" -Recurse | % { $_.FullName } | Select-Object -First 1) $x = @() [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $objForm = New-Object System.Windows.Forms.Form $objForm.Text = $WindowTitle $objForm.Size = New-Object System.Drawing.Size(290,270) $objForm.StartPosition = "CenterScreen" $objForm.FormBorderStyle = 'FixedSingle' $objForm.KeyPreview = $True $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}}) $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(75,215) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" $OKButton.Add_Click( {$i=0 if ( $textbox2.Text -eq '' ) { } else { foreach ($objItem in $objListbox.SelectedItems) { #[System.Windows.Forms.Application]::UseWaitCursor=$true $objForm.Cursor=[System.Windows.Forms.Cursors]::WaitCursor $local = $textbox2.Text $servidor = $textBoxServerName.Text Write-Progress -Activity "Gerando DACPAC" -status "Gerando DACPAC para $objItem" -percentComplete ($i++ / ($objListbox.SelectedItems).Count*100) $exec = ("'$aplicacao'" + " /a:Extract /ssn:"+$servidor +" /sdn:"+$objItem +" /tf:"+"'$local'"+"\"+$objItem+$data+".dacpac") #$exec = (C:\"Program Files (x86)"\"Microsoft SQL Server"\110\DAC\bin\SqlPackage.exe /a:Extract /ssn:$servidor /sdn:$objItem /tf:$local\$objItem$data+.dacpac) write-host $exec Invoke-expression "& $exec" #[System.Windows.Forms.Application]::UseWaitCursor=$false $objForm.Cursor=[System.Windows.Forms.Cursors]::Default } #Invoke-Item "$local" } if ( $checkbox1.Checked -eq $true -and $textbox2.Text -ne '') { Invoke-Item "$local" } else { } #$objForm.Close() }) $objForm.Controls.Add($OKButton) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(150,215) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Fechar" $CancelButton.Add_Click({$objForm.Close()}) $objForm.Controls.Add($CancelButton) # create your checkbox $checkbox1 = new-object System.Windows.Forms.checkbox $checkbox1.Location = new-object System.Drawing.Size(260,66) $checkbox1.Size = new-object System.Drawing.Size(250,50) $checkbox1.Checked = $true $objForm.Controls.Add($checkbox1) $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(10,120) $objLabel.Size = New-Object System.Drawing.Size(280,20) $objLabel.Text = "Selecione a base que deseja extrair o DACPAC:" $objForm.Controls.Add($objLabel) $objListbox = New-Object System.Windows.Forms.Listbox $objListbox.Location = New-Object System.Drawing.Size(10,140) $objListbox.Size = New-Object System.Drawing.Size(260,20) $objLabel2 = New-Object System.Windows.Forms.Label $objLabel2.Location = New-Object System.Drawing.Size(10,10) $objLabel2.Size = New-Object System.Drawing.Size(280,20) $objLabel2.Text = "Servidor:" $objForm.Controls.Add($objLabel2) # Create textbox for the Server name $textBoxServerName = New-Object System.Windows.Forms.TextBox $textBoxServerName.DataBindings.DefaultDataSourceUpdateMode = 0 $textBoxServerName.Location = New-Object System.Drawing.Size(10,30) $textBoxServerName.Name = "ServerName" $textBoxServerName.Size = New-Object System.Drawing.Size(100,10) $textBoxServerName.TabIndex = 0 $objForm.Controls.Add($textBoxServerName) $objLabel2 = New-Object System.Windows.Forms.Label $objLabel2.Location = New-Object System.Drawing.Size(10,60) $objLabel2.Size = New-Object System.Drawing.Size(280,20) $objLabel2.Text = "Destino:" $objForm.Controls.Add($objLabel2) # Create textbox used to file destination $textBox2 = New-Object System.Windows.Forms.TextBox $textBox2.DataBindings.DefaultDataSourceUpdateMode = 0 $textBox2.Location = New-Object System.Drawing.Size(10,80) $textBox2.Name = "textBox2" $textBox2.Size = New-Object System.Drawing.Size(150,10) $textBox2.TabIndex = 0 $objForm.Controls.Add($textBox2) $app = New-Object -ComObject Shell.Application $destino_OnClick= { #TODO: Place custom script here try { $browseForFolderOptions = 0 if ($NoNewFolderButton) { $browseForFolderOptions += 512 } $folder = $app.BrowseForFolder(0, $Message, $browseForFolderOptions, $InitialDirectory) #if ($folder) { $selectedDirectory = $folder.Self.Path } else { $selectedDirectory = '' } #[System.Runtime.Interopservices.Marshal]::ReleaseComObject($app) > $null $textbox2.Text = $folder.Self.Path #$selectedDirectory #.FileName.ToString() } catch { [System.Windows.Forms.MessageBox]::Show( $_.Exception.Message, "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error ) } } $conectar_OnClick= { #TODO: Place custom script here if ( $textBoxServerName.Text -eq '' ) { } else { $objForm.Cursor=[System.Windows.Forms.Cursors]::WaitCursor $dataSource = $textBoxServerName.Text $database = "master" $connectionString = "Server=$dataSource;Database=$database;Integrated Security=True;" $query = "select name from sys.databases where database_id > 4" $connection = New-Object System.Data.SqlClient.SqlConnection $connection.ConnectionString = $connectionString $connection.Open() $command = $connection.CreateCommand() $command.CommandText = $query $result = $command.ExecuteReader() $table = new-object “System.Data.DataTable” $table.Load($result) #$table $objListbox.datasource=$table.Item(0) $connection.Close() $objForm.Cursor=[System.Windows.Forms.Cursors]::Default } } $conectar = New-Object System.Windows.Forms.Button $conectar.Location = New-Object System.Drawing.Size(200,30) $conectar.Size = New-Object System.Drawing.Size(60,20) #$button2.TabIndex = 1 $conectar.Text = "Conectar" $conectar.UseVisualStyleBackColor = $True $conectar.add_Click($conectar_OnClick) $objForm.Controls.Add($conectar) $destino = New-Object System.Windows.Forms.Button $destino.Location = New-Object System.Drawing.Size(200,80) $destino.Size = New-Object System.Drawing.Size(50,20) #$button2.TabIndex = 1 $destino.Text = "..." $destino.UseVisualStyleBackColor = $True $destino.add_Click($destino_OnClick) $objForm.Controls.Add($destino) $objListbox.SelectionMode = "MultiExtended" $objListbox.Height = 70 $objForm.Controls.Add($objListbox) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog() $x
[polldaddy poll=8777809]