sabinio / salt

SQL Agent depLoy Tasks - Takes SQL Server Agent Jobs stored in XML and deploys them to a SQL Server Instance.
MIT License
13 stars 1 forks source link

Variables are not visible in scope #20

Open fmms opened 6 years ago

fmms commented 6 years ago

I created a script named Deploy-Job.ps1

...
$paramRunAs = "myAccount"
Unblock-File $saltlocation
Import-Module -Force $saltlocation
Add-Type -Path "C:\Program Files\Microsoft SQL Server\140\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"

$SqlConnection = Connect-SqlConnection -ConnectionString $connectionString
[xml] $_xml = [xml] (Get-Content -Path $JobManifestXmlFile)
$x = Get-Xml -XmlFile $_xml
Set-JobCategory -SqlServer $SqlConnection -root $x
Set-JobOperator -SqlServer $SqlConnection -root $x
$sqlAgentJob = Set-Job -SqlServer $SqlConnection -root $x
Set-JobSchedules -SqlServer $SqlConnection -root $x -job $SqlAgentJob
Set-JobSteps -SqlServer $SqlConnection -root $x -job $SqlAgentJob 
Disconnect-SqlConnection -SqlDisconnect $SqlConnection

However, the variable $paramRunAs was not visible to the function functions\SetJobProperties.ps1 in line 36. I had to change the calling script to

Set-Variable -Scope Global paramRunAs ""

Can the module be adapted, or should the documentation be adjusted, or did I do something wrong?

regards and thanks

RichieBzzzt commented 6 years ago

Hello Felix!

This is unusual: I’ve not seen other uses of salt have this issue. Is this runnning through PowerShell window, or VS Code, or PowerShell Console? How about through TeamCity/VSTS etc? What version of PowerShell? Is a custom profile loaded prior?

fmms commented 6 years ago

Hi @RichieBzzzt ,

we had a long talk at the sqlbits. ;)

So the PowerShell version

Name                           Value
----                           -----
PSVersion                      5.1.15063.1155
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.1155
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

All installed on Win 10.

No custom profile loaded:

# SQL Server Agent> Test-Path $profile
False

I have saved the commands from the initial post as a file called Deploy-Job.ps1 and then execute this script with .\Deploy-Job.ps1 in a PowerShell command line window.

RichieBzzzt commented 6 years ago

Hello!

Sorry for being slow to respond; a mixture of holidays and the world cup have slowed down my response time.

Also the build server needed a bit of a rebuild. But now I am up and running and free of distractions I'll go through this and the other issues you have raised.

RichieBzzzt commented 6 years ago

Hello! (again!)

Yes I recall our long chat, it was good to meet you!

At the moment parameters need to be set at a global level when running in PowerShell like you are: this is consistent with my testing project that I have up and running prior to each release.

I would love to change the Module so that these variables are in scope for the deployment - I'm not sure how VSTS/Octopus handle this as through their pipeline they manage it - but I don't know how!

I'll keep investigating how this is done, meanwhile I'll keep the issue open and update the documentation.

Thanks for all the feedback, it's been most useful!