I have been working on droping new database as part of my continuous deployment implementation.
For this I have needed to create couple powerhsell scripts.
Here is one example (prototype how it works)
Note: it needs tidy up.
$Instance = ".\SQLEXPRESS"
$LoginName = "Login name"
$Password = "loginPassword"
$DBName = "Database"
#Get the server object
$srv = New-Object ("Microsoft.SqlServer.Management.SMO.Server") $instance
$varDBUser = "Usern"
$varDBPassword = "password"
$srv.ConnectionContext.LoginSecure = $false
$srv.ConnectionContext.Login = $varDBUser
$srv.ConnectionContext.Password = $varDBPassword
#Get the login object if it exists
$Login = $srv.Logins.Item($LoginName)
IF (!($Login)) #check to see if login already exists
{
Write-Host " login does not exists, creating"
#it doesn't, so instantiate a new login object
$Login = New-Object ("Microsoft.SqlServer.Management.SMO.Login") ($Server, $LoginName)
#make it a SQL Login
$Login.LoginType = [Microsoft.SqlServer.Management.Smo.LoginType]::SqlLogin
#Create it on the server with the specified password
$Login.Create($Password)
}
#Get the database object
$DB = $srv.Databases[$DBName]
#Get the user object if it exists
$User = $DB.Users[$LoginName]
if (!($User)) # check to see if the user is already in the database
{
#it doesn't, so add it
$User = New-Object ("Microsoft.SqlServer.Management.SMO.User") ($DB, $LoginName)
$User.Login = $LoginName
$User.Create()
}
Wednesday, 25 September 2013
Monday, 23 September 2013
Running referenced powershell scripts from teamcity build.
I have been tasked with creating automation for build process using PowerShell and TeamCity.
I have had multiple functions that I wanted to call in multiple steps, but the one issue had problem with was error:
[15:50:43][Step 7/8] .\ExecuteScripts.ps1 : The term '.\ExecuteScripts.ps1' is not recognized
[15:50:43][Step 7/8] as the name of a cmdlet, function, script file, or operable program. Check the
[15:50:43][Step 7/8] spelling of the name, or if a path was included, verify that the path is
[15:50:43][Step 7/8] correct and try again.
[15:50:43][Step 7/8] At \Path\RunUmbracoInstallation.ps1:5 char:1
[15:50:43][Step 7/8] + .\ExecuteScripts.ps1
This is so helpfull that I had to find way around it.
I have created basic file (test.ps1 - first version) that I have been able to execute from team build process.
After this step have been executing correctly though TeamCity.
I have set up permissions on powershell for user account under which teamcity agent run to FullControl.
File: test.ps1 - first version
param(
[Parameter(Mandatory=$True)] [string] $arg1,
[Parameter(Mandatory=$True)] [string] $arg2
)
function PowerShellTest{
Write-Host -fore Magenta "Executing function powershell test with arguments:"
Write-host -fore Cyan "arg1:$arg1 arg2:$arg2"
}
PowerShellTest
[Parameter(Mandatory=$True)] [string] $arg1,
[Parameter(Mandatory=$True)] [string] $arg2
)
<# ========== Loading required files ============ #>
$commons =Join-Path (Get-ScriptDirectory)"test2.ps1"
.$commons;
<# ========== Loading required files ============ #>
function PowerShellTest{
Write-Host -fore Magenta "Executing function powershell test with arguments:"
Write-host -fore Cyan "arg1:$arg1 arg2:$arg2"
PowerShellTest2 -arg1 $arg1 -arg2 $arg2
}
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
PowerShellTest
function PowerShellTest2{
param(
[Parameter(Mandatory=$True)] [string] $arg1,
[Parameter(Mandatory=$True)] [string] $arg2
)
Write-Host -fore Magenta "Executing function powershell test2:"
Write-host -fore Cyan "arg1:$arg1 arg2:$arg2"
}
Note:
After running agent again I do get results as:
Execute PS Test script (Powershell)
[18:14:36][Step 8/8] Starting: Path\powershell.exe -NoProfile -NonInteractive -ExecutionPolicy ByPass -File PathToWorkDirectory\test.ps1 arg1="Test1" arg2="2Test2"
[18:14:36][Step 8/8] in directory: PathToWorkDirectory\
[18:14:37][Step 8/8] Executing function powershell test with arguments:
[18:14:37][Step 8/8] arg1:arg1=Test1 arg2:arg2=2Test2
[18:14:37][Step 8/8] Executing function powershell test2:
[18:14:37][Step 8/8] arg1:arg1=Test1 arg2:arg2=2Test2
[18:14:37][Step 8/8] Process exited with code 0
I have managed to do this thanks to this blog article.
http://leftlobed.wordpress.com/2008/06/04/getting-the-current-script-directory-in-powershell/
I have had multiple functions that I wanted to call in multiple steps, but the one issue had problem with was error:
[15:50:43][Step 7/8] .\ExecuteScripts.ps1 : The term '.\ExecuteScripts.ps1' is not recognized
[15:50:43][Step 7/8] as the name of a cmdlet, function, script file, or operable program. Check the
[15:50:43][Step 7/8] spelling of the name, or if a path was included, verify that the path is
[15:50:43][Step 7/8] correct and try again.
[15:50:43][Step 7/8] At \Path\RunUmbracoInstallation.ps1:5 char:1
[15:50:43][Step 7/8] + .\ExecuteScripts.ps1
This is so helpfull that I had to find way around it.
I have created basic file (test.ps1 - first version) that I have been able to execute from team build process.
After this step have been executing correctly though TeamCity.
I have set up permissions on powershell for user account under which teamcity agent run to FullControl.
File: test.ps1 - first version
param(
[Parameter(Mandatory=$True)] [string] $arg1,
[Parameter(Mandatory=$True)] [string] $arg2
)
function PowerShellTest{
Write-Host -fore Magenta "Executing function powershell test with arguments:"
Write-host -fore Cyan "arg1:$arg1 arg2:$arg2"
}
PowerShellTest
File: test.ps1 - final version
param([Parameter(Mandatory=$True)] [string] $arg1,
[Parameter(Mandatory=$True)] [string] $arg2
)
<# ========== Loading required files ============ #>
$commons =Join-Path (Get-ScriptDirectory)"test2.ps1"
.$commons;
<# ========== Loading required files ============ #>
function PowerShellTest{
Write-Host -fore Magenta "Executing function powershell test with arguments:"
Write-host -fore Cyan "arg1:$arg1 arg2:$arg2"
PowerShellTest2 -arg1 $arg1 -arg2 $arg2
}
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
PowerShellTest
File: test2.ps1
function PowerShellTest2{
param(
[Parameter(Mandatory=$True)] [string] $arg1,
[Parameter(Mandatory=$True)] [string] $arg2
)
Write-Host -fore Magenta "Executing function powershell test2:"
Write-host -fore Cyan "arg1:$arg1 arg2:$arg2"
}
Now build configuration
Note:
- that script arguments do not have staring - before and value is in quotes.
- Powershell run mode: is defined as to what version of my PS and x64 is as my server version
After running agent again I do get results as:
Execute PS Test script (Powershell)
[18:14:36][Step 8/8] Starting: Path\powershell.exe -NoProfile -NonInteractive -ExecutionPolicy ByPass -File PathToWorkDirectory\test.ps1 arg1="Test1" arg2="2Test2"
[18:14:36][Step 8/8] in directory: PathToWorkDirectory\
[18:14:37][Step 8/8] Executing function powershell test with arguments:
[18:14:37][Step 8/8] arg1:arg1=Test1 arg2:arg2=2Test2
[18:14:37][Step 8/8] Executing function powershell test2:
[18:14:37][Step 8/8] arg1:arg1=Test1 arg2:arg2=2Test2
[18:14:37][Step 8/8] Process exited with code 0
I have managed to do this thanks to this blog article.
http://leftlobed.wordpress.com/2008/06/04/getting-the-current-script-directory-in-powershell/
Thursday, 19 September 2013
PowerShell - Usefull commands
Update help definition from online.
You need to run this command under administrator rights.PS C:\Windows\system32> Update-Help
(I have read, that it gets updated almost every week)
PowerShell can run with wildcards : *
PS C:\Windows\system32> help *service*Name Category Module Synopsis
---- -------- ------ --------
Get-Service Cmdlet Microsoft.PowerShell.M... Gets the services on a local or remote computer.
New-Service Cmdlet Microsoft.PowerShell.M... Creates a new Windows service.
New-WebServiceProxy Cmdlet Microsoft.PowerShell.M... Creates a Web service proxy object that lets you use and manage the Web service in Windows PowerShell.
Restart-Service Cmdlet Microsoft.PowerShell.M... Stops and then starts one or more services.
Resume-Service Cmdlet Microsoft.PowerShell.M... Resumes one or more suspended (paused) services.
Set-Service Cmdlet Microsoft.PowerShell.M... Starts, stops, and suspends a service, and changes its properties.
Start-Service Cmdlet Microsoft.PowerShell.M... Starts one or more stopped services.
Stop-Service Cmdlet Microsoft.PowerShell.M... Stops one or more running services.
Suspend-Service Cmdlet Microsoft.PowerShell.M... Suspends (pauses) one or more running services.
Get full help information about command including examples
PS C:\Windows\system32> help Get-Service -full
What version of PowerShell do I have Installed
- $PSVersionTable
- $host.version
Get gui for command
PS C:\Windows\system32> Show-Command Start-ProcessGet members of commands:
This command shows all information what all I can get get.PS D:\Scripts> get-service | get-member
PowerShell function parameters
If you are using parameters use param definition in function instead in bracets of the function name:
function WebConfig-SetAppConfigValueForKey{
param(
[Parameter(Mandatory=$true)]
[xml]$doc,
[Parameter(Mandatory=$true)]
[string]$key,
[Parameter(Mandatory=$true)]
[string]$value
)
Executing script path
If you are executing script you will be able to get path of the script which gets executed.function Test(){
$scriptpath = $MyInvocation.MyCommand.Path
write-host $scriptpath
}
Test
If you execute the script without file you will get null
Tuesday, 10 September 2013
Could not open Source file: Could not find a part of the path Web.config; \umbraco\Xslt\Web.config'
I am using Team city as my build server, and web deploy in order to create my deploy package.
I have received error message:
My error message that is on Build server
[09:31:02]Step 1/1: Publish & Deploy (MSBuild) (47s)
[09:31:05][Step 1/1] Website.Web.UI\Website.Web.UI.csproj.teamcity: Build targets: Build;Package (45s)
[09:31:50][Website.Web.UI\Website.Web.UI.csproj.teamcity] AutoParameterizationWebConfigConnectionStringsCore
[09:31:50][AutoParameterizationWebConfigConnectionStringsCore] ParameterizeTransformXml
[09:31:50][ParameterizeTransformXml] D:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.Publishing.targets(2352, 5): Could not open Source file: Could not find a part of the path 'D:\41fe9571fcaca9a1\Website.Web.UI\umbraco\Xslt\Web.config;
\umbraco\Xslt\Web.config'.
[09:31:50][Website.Web.UI\Website.Web.UI.csproj.teamcity] Project Website.Web.UI\Website.Web.UI.csproj.teamcity failed.
[09:31:50][Step 1/1] Step Publish & Deploy (MSBuild) failed
I have found 2 cases which cause this error.
This happens because of web.deploy is running web.config transformation changes. By default the web.config is set to build as content.
This happens because of web.deploy is running web.config transformation changes. By default the web.config is set to build as content.
Solution 1 - Svn issue
Issue has been that I have had 2 sources loading data.
One:Source check out from repository
Second:Artefacts used from previous steps.
Use only one source for build and error goes away.
Find the file in your solution(project). in my case it is:"D:\41fe9571fcaca9a1\Website.Web.UI\umbraco\Xslt\Web.config".
Go to the properties of the file (Alt + Enter)
Update your configuration.
Set your "Build Action" to None or Content
This means that there will be no transform on this config, usual configuration is compile.
Use only one source for build and error goes away.
Solution 2 - Web.config transform
Find the file in your solution(project). in my case it is:"D:\41fe9571fcaca9a1\Website.Web.UI\umbraco\Xslt\Web.config".
Go to the properties of the file (Alt + Enter)
Update your configuration.
Set your "Build Action" to None or Content
This means that there will be no transform on this config, usual configuration is compile.
Saturday, 7 September 2013
AngularJS Chrome Disable same origin policy
I case you have problem loading template files in chrome:
open your console.
Make sure you have closed chrome before running following command.
Command:
Chrome.exe --allow-file-access-from-files
Now open your chrome again and your scripts should work.
Wednesday, 4 September 2013
Rendering from variable html using jquery template
I started to use template for rendering my code from json responses.
Everything work great if you do not have html in the variables.
Supposed documentation should be here: http://api.jquery.com/category/plugins/templates
I could not find it.
Modifying example from github:
I have found example how to achieve this here.
I have not been able to make this work.
Finally I have found out that the correct syntax is:
${Name} =>{{html Name}}
Note that there is no longer: $.
Source for this example:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-2.0.3.js"></script>
<script src="Scripts/jQuery.tmpl.js"></script>
<style>
table { border-collapse:collapse; margin:8px; background-color:#f8f8f8; }
table td { border:1px solid blue; padding:3px; }
</style>
<script type="text/javascript">
$(document).ready(function() {
var movies = [
{ Name: "The Red Violin<strong>test</strong>", ReleaseYear: "1998", Director: "François Girard" },
{ Name: "Eyes Wide Shut", ReleaseYear: "1999", Director: "Stanley Kubrick" },
{ Name: "The Inheritance", ReleaseYear: "1976", Director: "Mauro Bolognini" }
];
var markup = "<tr><td colspan='2'>{{html Name}}</td><td>Released: ${ReleaseYear}</td><td>Director: ${Director}</td></tr>";
/* Compile markup string as a named template */
$.template("movieTemplate", markup);
/* Render the named template */
$("#showBtn").click(function () {
$("#movieList").empty();
$.tmpl("movieTemplate", movies).appendTo("#movieList");
});
});
</script>
</head>
<body>
<button id="showBtn">Show movies</button><br/>
<table><tbody id="movieList"></tbody></table>
</body>
</html>
Everything work great if you do not have html in the variables.
Supposed documentation should be here: http://api.jquery.com/category/plugins/templates
I could not find it.
Modifying example from github:
I have found example how to achieve this here.
I have not been able to make this work.
Finally I have found out that the correct syntax is:
${Name} =>{{html Name}}
Note that there is no longer: $.
Source for this example:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-2.0.3.js"></script>
<script src="Scripts/jQuery.tmpl.js"></script>
<style>
table { border-collapse:collapse; margin:8px; background-color:#f8f8f8; }
table td { border:1px solid blue; padding:3px; }
</style>
<script type="text/javascript">
$(document).ready(function() {
var movies = [
{ Name: "The Red Violin<strong>test</strong>", ReleaseYear: "1998", Director: "François Girard" },
{ Name: "Eyes Wide Shut", ReleaseYear: "1999", Director: "Stanley Kubrick" },
{ Name: "The Inheritance", ReleaseYear: "1976", Director: "Mauro Bolognini" }
];
var markup = "<tr><td colspan='2'>{{html Name}}</td><td>Released: ${ReleaseYear}</td><td>Director: ${Director}</td></tr>";
/* Compile markup string as a named template */
$.template("movieTemplate", markup);
/* Render the named template */
$("#showBtn").click(function () {
$("#movieList").empty();
$.tmpl("movieTemplate", movies).appendTo("#movieList");
});
});
</script>
</head>
<body>
<button id="showBtn">Show movies</button><br/>
<table><tbody id="movieList"></tbody></table>
</body>
</html>
Subscribe to:
Posts (Atom)