Deploying database using TFS Deployer and SqlPackage - sql-server

I'm trying to make PowerShell script that uses SqlPackage to deploy database through TFS Deployer service. Script is working if it is executed directly from command line but it fails when TFS Deployer tries to execute it, the same user account is used for both cases.
The service is running in test mode (TfsDeployer -d), as console application, but it also fails when runs as Windows Service.
This is log file (captured output of SqlPackage and exception caught in PowerShell script):
11/18/2012 17:51:49 | Publishing to database 'databaseName' on server 'machineName'.
11/18/2012 17:51:56 | An error occurred while the batch was being executed.
11/18/2012 17:51:56 | System.Management.Automation.RemoteException: *** Could not deploy package.
These are the only information I was able to collect. Error code (HRESULT) was not present in the caught exception.
PowerShell script goes like this:
try{
$cmd = Join-Path (Get-Item "Env:ProgramFiles(x86)").Value "Microsoft SQL Server\110\DAC\bin\SqlPackage.exe"
$src = Join-Path $source "db.dacpac"
$cfg = Join-Path $source "db.publish.xml"
&$cmd /Action:Publish /SourceFile:$src /Profile:$cfg 2>&1 | ForEach-Object -Process {
Write-Log $_
}
}
catch [Exception] {
Write-Log $_.Exception.ToString()
if($_.Exception.HResult) {
Write-Log "Code: $($_.Exception.HResult.ToString('X'))"
}
}

Related

Get Azure Active Directory export from GitHub Action

Background
I previously created a PowerShell script that accessed my company's Active Directory and exported the file as a csv:
get-aduser -filter * -properties "whenCreated","DisplayName","Department","Enabled","mobile","MobilePhone","Name","Office","Title" | export-csv -path adexport.csv
To use this command, I had to install some cmdlets with:
Get-WindowsCapability -Name RSAT.ActiveDirectory* -Online | Add-WindowsCapability -Online
Use GitHub Action
I want to automate this script with a GitHub Action. I set up the connection between GitHub and Azure following this documentation. I created the following, simplified workflow:
name: AzureLoginSample
on: push
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Log in with Azure
uses: azure/login#v1
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}'
enable-AzPSSession: true
- name: Azure PowerShell Action
uses: Azure/powershell#v1
with:
inlineScript: |
Get-AzADUser | export-csv -path adexport.csv
azPSVersion: 3.1.0
When the workflow runs, the first step (log in) works just fine, but the second step fails because:
Get-AzADUser: /home/runner/work/_temp/415ec269-1cff-4c50-8035-c1e5181e0412.ps1:2
Line |
2 | Get-AzADUser | export-csv -path adexport.csv
| ~~~~~~~~~~~~
| Insufficient privileges to complete the operation.
I feel like I have the necessary permissions on the Azure side of things; the Azure application has reader and contributor permissions. I know with my original PowerShell script, I had to run as an admin - is there a way to do this with my Azure PowerShell script?
Previous Attempt
I tried to copy and paste the original PowerShell command, but the cmdlet get-aduser could not be found. When I tried to create a separate step and install the cmdlets, I was given another " cmdlet could not be found" error.
Thank you in advance and let me know if you need any clarifications.

Azure Managed Instance - Virtual Machine

I have setup an Azure VM that is linked to my Managed SQL, but I need to install SSMS and ODBC drivers but the machine tells me nothing can be installed.
Also, I did the install from this (https://learn.microsoft.com/en-us/azure/azure-sql/managed-instance/connect-vm-instance-configure) and it was meant to do the SSMS install as part of the process
Do these need to be done via PowerShell scripts? And if so, does anyone have one spare for this or another idea?
Thanks
It can be done directly, install from Browser using link SSMS and ODBC and can also be done with PowerShell Script
PowerShell Script for Downlod SSMS
#Set file and folder path for SSMS installer .exe
$folderpath="c:\windows\temp"
$filepath="$folderpath\SSMS-Setup-ENU.exe"
#If SSMS not present, download
if (!(Test-Path $filepath)){
write-host "Downloading SQL Server 2016 SSMS..."
$URL = "https://download.microsoft.com/download/3/1/D/31D734E0-BFE8-4C33-A9DE-2392808ADEE6/SSMS-Setup-ENU.exe"
$clnt = New-Object System.Net.WebClient
$clnt.DownloadFile($url,$filepath)
Write-Host "SSMS installer download complete" -ForegroundColor Green
}
else {
write-host "Located the SQL SSMS Installer binaries, moving on to install..."
}
# start the SSMS installer
write-host "Beginning SSMS 2016 install..." -nonewline
$Parms = " /Install /Quiet /Norestart /Logs log.txt"
$Prms = $Parms.Split(" ")
& "$filepath" $Prms | Out-Null
Write-Host "SSMS installation complete" -ForegroundColor Green
PowerShell Script for Downlod ODBC Driver.
$url = "https://download.microsoft.com/download/1/E/7/1E7B1181-3974-4B29-9A47-CC857B271AA2/English/X64/msodbcsql.msi"
$outpath = "c:/odbc.msi"
Invoke-WebRequest -Uri $url -OutFile $outpath
Start-Process -Filepath $outpath -ArgumentList "/qr IACCEPTMSODBCSQLLICENSETERMS=YES"
Use this command to run command above on your Azure VMs :
$vm = Get-AzVM -Name <VM name> -ResourceGroupName <resource group name>
Invoke-AzVMRunCommand -VM $vm -CommandId 'RunPowerShellScript' -ScriptPath "<install odbc or SSMS file path>"
Reference : Azure VM: Update Microsoft ODBC Driver via Powershell
https://gist.github.com/justinsoliz/34324700ea93c7b77b4ac3e132584de7

TFS Run batch script on a remote server with admin permission

I currently have Server A which is where my TFS and Build Agent is located. I then have Server B which is when my source code site. I am trying to set up a build definition and copies file from on location in server B to another and then build the solution.
However when I run this batch file as part of a build definition it is not creating folders where it need to be. I believe due to the agent not having correct permissions.
Is there a way to run the following batch script to run with Admin permission from a build definition.
You can try below workarounds:
Convert the batch script to PowerShell script, then copy the
PowerShell script to target machine and use the PowerShell on Target
Machines task to run the script. You can enter your admin user
and password using the task. Reference below screenshot.
Add a PowerShell task and run below script to call the cmd.exe to
run the batch script with an admin user account on target machine
(Copy the batch script to target machine first, in below sample I
copied the batch script to C:\Scripts\Test.bat):
Param(
[string]$computerName = "v-tinmo-12r2",
)
$Username = "Domain\user"
$Password = ConvertTo-SecureString "password-here" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($Username,$password)
Invoke-Command -ComputerName $computerName -Credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\Scripts\Test.bat'"}

Inno script to identify SQL Server instances installed on client system and skip installing instance if already installed

I am creating a package using inno script and want to install SQL Server SQLEXPR_x64_ENU on the client's machine. I want code which determines installed SQL Server instances (MSSQL$MYSQL and MSSQLSERVER) on the target system. If the instances are already installed, then the script should skip installing the instances again.
I am using following shell script to find instances and calling it by batch file but it is not working.
$server = $env:computername
$object = Get-WmiObject win32_service -ComputerName $server | where {($_.name -like "MSSQL$*" -or $_.name -like "MSSQLSERVER" -or $_.name -like "SQL Server (*") -and $_.name -notlike "*helper*" -and $_.name -notlike "*Launcher*"}
if ($object)
{
echo " One OR More Instances running..."
$instInfo= $object |select Name,StartMode,State, Status
}
else
{
Start-Process -FilePath "C:\sql\SQLEXPR_x64_ENU.exe"
echo " No SQL Instances running..."
}
Is there any Inno script which can find number of SQL Server instances installed?

Script to stop/restart local SQL services

Does anyone have a simple script (powershell or command line) that would enable to me to shutdown local SQL Services (SQL Server, Integration services, etc ..) when I don't need them and then perhaps another script to turn them all back on when I do need them?
Currently I am going to Services and then stopping/starting them manually.
We had a need to stop and disable and then enable and start on multiple remote servers, so this is how I handled it:
Stopping:
function stopdisable ($compnam, $svc)
{
(get-service -computername $compnam -name $svc).stop()
set-service -computername $compnam -name $svc -startuptype disabled
}
stopdisable "server1" "servicename1"
stopdisable "server2" "servicename2"
Starting:
function enablestart ($compnam, $svc)
{
set-service -computername $compnam -name $svc -startuptype automatic
(get-service -computername $compnam -name $svc).start()
}
enablestart "server1" "servicename1"
enablestart "server2" "servicename2"
Of course, this is dependent upon you having the appropriate permissions on the remote servers.

Resources