Running a build script after calling vcvarsall.bat from powershell - c

I am attempting to run the Visual Studio (developer cmd prompt) environmental variable setup batch file followed by a build script from within a Powershell script as follows:
cmd /v:on/k "(""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" amd64_x86 && C:\buildscript.cmd --build-options)"
It appears however that the environmental settings established by vcvarsall.bat are not retained for the build script call. i.e. no default compiler setup, etc.
Is the /v:on combined with the /k switch not actually utilizing the same cmd session and properly delaying environmental variable expansion? Perhaps the approach is wrong ...

The problem is that when you run cmd.exe to run a batch file, the variables are set in that instance of cmd.exe, but they disappear after that instance terminates.
To work around this problem, you can use the Invoke-CmdScript function in this article:
Windows IT Pro: Take Charge of Environment Variables in PowerShell
The function is as follows:
# Invokes a Cmd.exe shell script and updates the environment.
function Invoke-CmdScript {
param(
[String] $scriptName
)
$cmdLine = """$scriptName"" $args & set"
& $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
select-string '^([^=]*)=(.*)$' | foreach-object {
$varName = $_.Matches[0].Groups[1].Value
$varValue = $_.Matches[0].Groups[2].Value
set-item Env:$varName $varValue
}
}
You could add this function to your PowerShell profile or use it as a script file.
Once you have defined the function, you can run your commands:
Invoke-CmdScript "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64_x86
C:\buildscript.cmd --build-options
...or whatever you need.
The article also presents a couple of functions that let you easily save and restore environment variables.

Related

TFS Build providing prebuild auto increment assembly information by 1 and post publish zip the files

I am trying to implement Build automation using TFS (Version 12.0.31101.0)
This is the settings I am using, It build properly and publishes to the mentioned drop location:
For PreBuild I am trying to use the following batch script and this is not incrementing:
$path = "E:\Dev\ABC\My Project\AssemblyInfo.vb"
$pattern = '\<Assembly: AssemblyVersion\(.*\)\>'
(Get-Content $path) | ForEach-Object{
if($_ -match $pattern){
# We have found the matching line
# Edit the version number and put back.
$fileVersion = [version]$matches[1]
Write-Output "Major is $Matches[0] Minor is $Matches[1] Build is $Matches[2] Revision is [version]$matches[3]"
$newVersion = "{0}.{1}.{2}.{3}" -f $fileVersion.Major, $fileVersion.Minor, $fileVersion.Build, ($fileVersion.Revision + 1)
'<Assembly: AssemblyVersion("{0}")>' -f $newVersion
} else {
# Output line as is
$_
}
} | Set-Content $path
For 'post build script path' i want to zip the contents and put it into another folder, I am using the following script for this.
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('\$(TF_BUILD_DROPLOCATION)\MySolution\_PublishedWebsites\ABC', 'ABC_Deploy.zip'); }"
On Executon it throws the following error:
Exception calling "CreateFromDirectory" with "2" argument(s): "Could not find
a part of the path 'C:\$TF_BUILD_DROPLOCATION\MySolution\_PublishedWebsites\ABC
At line:1 char:53
+ & { Add-Type -A 'System.IO.Compression.FileSystem';
[IO.Compression.ZipFile]::Cr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DirectoryNotFoundException
What changes should i make for both prebuild script and post build script to get this working?
For Prebuild:
Create a C# project(i used console project) which updates the assembly information..
Add RunScript to TFS build template before MSBuild.
Call the exe which is generated from c# project from your batch file, and pass the parameter as where your source code location.
Eg: (START "C:\UpdateAssemblyTest.exe" "\TFS-Server\TFSMapPath\ABC\")
PostBuild: I found a workaround for getting the TF_BUILD_DROPLOCATION. I modified the TFS Build Template and added a 'RunScript' tool and called my batch file from there and passed the drop location as an argument.
For 'Post Build Script' If build drop location is not accessible via the variable you are using; try using COPY command and copy the dropped folders to a known location on TFS Build server and then give that path in the ZIP Powershell command. (Though it is workaround for now. :) )
For 'Pre build Script', I will check and come back to you.
For 'post build script path', according to the Exception message it cannot identify the variable $TF_BUILD_DROPLOCATION.
As it's environment variable, So,please try to access the variables using $env:. The drop location would be
$env:TF_BUILD_DROPLOCATION for example.
For PreBuild, if you want to version the assemblies, you can try to put all custom Versioning work into a custom Version.proj MsBuild script and call it in TFS build definition before the .sln. The script injects Version into source code (SharedAssemblyInfo.cs, Wix code, readme.txt), and then solution build builds that source code. Please reference this thread :Versioning .NET builds
You can also reference this article :
https://www.stevefenton.co.uk/2012/11/automatically-updating-your-assemblyinfo-with-a-batch-file/

How to set FromFile location in Powershell?

I am preparing a script, which needs to use some images from same folder as the script. The images are to be shown on WinForms GUI.
$imgred = [System.Drawing.Image]::FromFile("red.png")
When I run the ps1 script manually from the folder just by clicking, it loads images and shows them. Unfortuantely I do not remember exactly how I set up this, but as far as I can, it was just the default program to use for ps1 files.
When I run the script from a cmd file (to hide the cmd window), it also loads them.
But when I open with Powershell IDE and run it, I get errors and no icons are shown on my GUI.
When I open with Powershell it also fails to load them.
The only difference between those run modes I can find is with:
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$scriptPath #always same, the location of script
(Get-Location).Path #scriptlocation when icons loaded, system32 folder when unsuccessful load
Same behavior when doing cd $scriptPath, so the current folder is most likely not the guilty one.
I know I can write $scriptPath/red.png in each file read line (FromFile), but what I want is to define it once - the default location for FromFile - and then just have simple filename work regardless of the way I run it.
What is to be changed so the default file reading path is same as my scripts location?
Modifying the default location stack in PowerShell ($PWD) doesn't affect the working directory of the host application.
To see this in action:
PS C:\Users\Mathias> $PWD.Path
C:\Users\Mathias
PS C:\Users\Mathias> [System.IO.Directory]::GetCurrentDirectory()
C:\Users\Mathias
now change location:
PS C:\Users\Mathias> cd C:\
PS C:\> $PWD.Path
C:\
PS C:\> [System.IO.Directory]::GetCurrentDirectory()
C:\Users\Mathias
When you invoke a .NET method that takes a file path argument, like Image.FromFile(), the path is resolved relative to the latter, not $PWD.
If you want to pass a file path relative to $PWD, do:
$pngPath = Join-Path $PWD "red.png"
[System.Drawing.Image]::FromFile($pngPath)
or
[System.Drawing.Image]::FromFile("$PWD\red.png")
If you require a path relative to the executing script, in PowerShell 3.0 and newer you can use the $PSScriptRoot automatic variable:
$pngPath = Join-Path $PSScriptRoot "red.png"
If you need to support v2.0 as well, you could put something like the following at the top of your script:
if(-not(Get-Variable -Name PSScriptRoot)){
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Definition -Parent
}
When using PowerShell in interactive mode, you could configure the prompt function to have .NET "follow you around" like so:
$function:prompt = {
if($ExecutionContext.SessionState.Drive.Current.Provider.Name -eq "FileSystem"){
[System.IO.Directory]::SetCurrentDirectory($PWD.Path)
}
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}
but I would recommend against that, just get into the habit of providing fully qualified paths instead.

PowerShell Output

So, I am fairly new to PowerShell and need to create a script to rename the computers in our office. That portion of the script works. The part I am having trouble with is the output.
I have this set in task scheduler, but when it runs I do not see if the rename was successful. Below is my script and below that is what goes into the text file.
start-transcript -path C:\Users\abhagwandin.SENECA\Desktop\RenameResults.txt
$CSV = Import-Csv "C:\Users\abhagwandin.SENECA\Desktop\Computer Desktop Names Test.csv" -Header OldName, NewName
Foreach ($name in $CSV)
{
write-output $name
netdom renamecomputer $name.OldName /newname: $name.NewName /userd: admin /passwordd: pass /usero: admin /passwordo: pass /reboot /force
}
stop-transcript
-------------------------------------------------------------------------------
**********************
Windows PowerShell Transcript Start
Start time: 20150520154216
Username :
Machine : (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
Transcript started, output file is C:\Users\abhagwandin.SENECA\Desktop\RenameRe
sults.txt
OldName NewName
------- -------
JFLAHNYCD1 JFLAHERTY
**********************
Windows PowerShell Transcript End
End time: 20150520154218
**********************
You know that renaming a computer through a cmdline command in powershell instead of using the built in cmdlets can give problems with the output if you don't parse the output (and preferably create a new object for it)?
Why don't you use rename-computer or the rename() method of the win32_computersystem wmi class? Both can be used remotely so you don't even have to schedule tasks that way.
Just create an input file with the current name and the desired names and use a loop to process them.

Run command on local build, but not TFS build

My post-build event is:
"$(DevEnvDir)TF.exe" checkout "$(TargetPath)"
This checks out the target build assembly file. This is what I want, and it works great in VS.
On the build server, $(DevEnvDir) is equal to *Undefined*. So, I modified my post-build event to the following:
IF NOT "$(DevEnvDir)" == '*Undefined*' "$(DevEnvDir)TF.exe" checkout "$(TargetPath)"
The problem is, it still evaluates to see if $(DevEnvDir)TF.exe is an executable, and on the build server it evaluates to *Undefined*TF.exe, which throws this error:
'"*Undefined*TF.exe"' is not recognized as an internal or external command, operable program or batch file.
How can I conditionally execute this statement without it evaluating if the executable exists first?
To run a command conditionally like this, use PowerShell:
if ('$(DevEnvDir)' -ne '*Undefined*') { [System.Diagnostics.Process]::Start('$(DevEnvDir)TF.exe', 'checkout "$(TargetPath)"') }
And wrap it in a clean PS environment:
powershell -windowstyle hidden -nologo -noprofile if ('$(DevEnvDir)' -ne '*Undefined*') { [System.Diagnostics.Process]::Start('$(DevEnvDir)TF.exe', 'checkout "$(TargetPath)"') }

How to convert this sqlcmd script to an Invoke-Sqlcmd script?

I have a SQL script designed to be executed by sqlcmd, and a Command script that executes sqlcmd with the correct parameters.
I want to convert the Command script to a PowerShell script that uses Invoke-Sqlcmd instead of sqlcmd.
The SQL script, the Command script, and the new PowerShell script all live in the directory C:\Users\iain.CORP\SqlcmdQuestion.
SQL Script
The SQL script is called ExampleQuery.sql. It selects a string literal. The value of the string literal is set by sqlcmd at runtime to the value of the ComputerName sqlcmd scripting variable. The code looks like this:
SELECT '$(ComputerName)';
Command Script
The command script is called ExecQuery.cmd. It calls sqlcmd to execute ExampleQuery.sql and sets the value of the scripting variable ComputerName to the value of the environment variable COMPUTERNAME. The code looks like this:
sqlcmd -i ExampleQuery.sql -v ComputerName = %COMPUTERNAME%
When I open a command prompt, the default working directory is C:\Users\iain.CORP. I change the to the directory containing the files, and run the Command script:
cd C:\Users\iain.CORP\SqlcmdQuestion
ExecQuery.cmd
I see this output:
---------
SKYPC0083
(1 rows affected)
The script successfully selects a string literal set by sqlcmd.
PowerShell Script
The PowerShell script is called ExecQuery.ps1. It is supposed to do the same as the command script, using Invoke-Sqlcmd instead of sqlcmd. The code looks like this:
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
Invoke-Sqlcmd -InputFile 'ExampleQuery.sql' -Variable "ComputerName = $Env:COMPUTERNAME"
When I open a PowerShell prompt, the default working directory is Z:\. I change to the directory containing the files, and run the PowerShell script:
cd C:\Users\iain.CORP\SqlcmdQuestion
.\ExecQuery.ps1
I see this output:
Invoke-Sqlcmd : Could not find file 'Z:\ExampleQuery.sql'.
At C:\Users\iain.CORP\SqlcmdQuestion\ExecQuery.ps1:4 char:14
+ Invoke-Sqlcmd <<<< -InputFile 'ExampleQuery.sql' -Variable "ComputerName = $Env:COMPUTERNAME"
+ CategoryInfo : InvalidResult: (:) [Invoke-Sqlcmd], FileNotFoundException
+ FullyQualifiedErrorId : ExecutionFailed,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
The PowerShell script raises an error because Invoke-Sqlcmd can't find the the input file in the Z:\ directory, which happens to be the default working directory.
The Command script found the script in the current working directory.
How do I make Invoke-Sqlcmd use the current working directory instead of the default working directory?
For this answer, assume that the directory C:\Users\iain.CORP\SqlcmdQuestion exists and that executing dir at that location produces the following output, as implied by the question:
Directory: C:\Users\iain.Corp\SqlcmdQuestion
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 26/09/2012 15:30 27 ExampleQuery.sql
-a--- 26/09/2012 15:30 61 ExecQuery.cmd
-a--- 26/09/2012 15:34 172 ExecQuery.ps1
PowerShell ignores the working directory by design
My question has a false premise:
How do I make Invoke-Sqlcmd use the current working directory instead of the default working directory?
The cmdlet does use the current working directory. The problem is that I didn't change the working directory at all in my PowerShell session.
In PowerShell, cd is an alias for the Set-Location cmdlet. You can prove this using the Get-Alias cmdlet:
Get-Alias cd
Output:
CommandType Name Definition
----------- ---- ----------
Alias cd Set-Location
Alex Angelopoulos explains:
[A]lthough PowerShell's location is analogous to the working directory, the location is not the same thing as the working directory. In fact, PowerShell doesn't touch the working directory.
Set-Location does not set the working directory. It sets the working location, which is a similar but distinct concept in PowerShell.
You can prove this by inspecting the working directory using the .NET property Environment.CurrentDirectory after setting the working location using cd as in the question:
cd C:\Users\iain.CORP\SqlcmdQuestion
Environment::CurrentDirectory
Output:
Z:\
I would guess this design decision was made to be consistent. The working directory would be undefined when, for example, the working location were set to a registry hive.
Invoke-Sqlcmd violates this design principle
Invoke-Sqlcmd violates PowerShell's general design principle to use the working location rather than the working directory. Most cmdlets use the working location to resolve relative paths, but Invoke-Sqlcmd is an exception.
Using the ILSpy disassembler and a little intuition to inspect the containing assembly Microsoft.SqlServer.Management.PSSnapins, I believe I have found the reason for the error.
I believe that the cmdlet's parameter -InputFile is implemented by the method IncludeFileName. ILSpy's disassembly of the method looks like this:
// Microsoft.SqlServer.Management.PowerShell.ExecutionProcessor
public ParserAction IncludeFileName(string fileName, ref IBatchSource pIBatchSource)
{
if (!File.Exists(fileName))
{
ExecutionProcessor.sqlCmdCmdLet.TerminateCmdLet(new FileNotFoundException(PowerShellStrings.CannotFindPath(fileName), fileName), "ExecutionFailureException", ErrorCategory.ParserError);
return ParserAction.Abort;
}
BatchSourceFile batchSourceFile = new BatchSourceFile(fileName);
pIBatchSource = batchSourceFile;
return ParserAction.Continue;
}
Invoke-Sqlcmd uses the .NET method File.Exists to check whether the specified input file exists. The method's documentation remarks that relative paths are resolved using the working directory:
The path parameter is permitted to specify relative or absolute path
information. Relative path information is interpreted as relative to
the current working directory. To obtain the current working
directory, see GetCurrentDirectory.
This suggests that File.Exists would return false in this case, which would cause the error message seen in the question. You can prove this by executing the method directly from the prompt:
cd C:\Users\iain.CORP\SqlcmdQuestion
[IO.File]::Exists('ExecQuery.sql')
Output:
False
The method returns false, so the cmdlet terminates with a 'file not found' error.
You can work around the unusual behavior
There are two workarounds for Invoke-Sqlcmd using the working directory instead of the working location to resolve relative paths:
Always use an absolute path as the value of the -InputFile parameter. CandiedCode's answer shows how to do this.
Set the working directory and use a relative path.
I solved the problem without side-effects by modifying ExecQuery.ps1 like this:
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
$RestoreValue = [Environment]::CurrentDirectory
[Environment]::CurrentDirectory = Get-Location
Invoke-Sqlcmd -InputFile 'ExampleQuery.sql' -Variable "ComputerName = $Env:COMPUTERNAME"
[Environment]::CurrentDirectory = $RestoreValue
I see this output:
Column1
-------
SKYPC0083
Success!
The new script sets the working directory to match the working location before executing Invoke-Sqlcmd. To avoid unintended side-effects of changing the working directory, the scrtipt restores the working directory value before completing.
Setting the current directory is described in this Channel 9 thread. The example there uses the Directory.SetCurrentDirectory method, but I find it simpler to set the property directly.
You could fully qualify the Inputfile location:
Invoke-Sqlcmd -InputFile 'C:\Users\iain.CORP\SqlcmdQuestion\ExampleQuery.sql' -Variable "ComputerName = $Env:COMPUTERNAME"
And use a variable to drive the script location:
$FileLocation = 'C:\Users\iain.CORP\SqlcmdQuestion\'

Resources