PowerShell not executing script from SQL server - sql-server

I have a powershell script that I need to execute from a SQL server on a schedule. I don't think it matters, but in case it does, the script is sending the command to refresh a powerbi.com dataset. I can run it successfully locally and on one of my servers, but when I try it on the other one, I get the error below.
For clarification, the powershell versions match as well as the execution policies. I have listed them here:
PS Version 5.1.14409.1005
Execution Policy
Scope ExecutionPolicy
Machine Policy Undefined
User Policy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine RemoteSigned
The error I get on one of the servers is below:
Connect-PowerBIServiceAccount : Failed to populate environments in settings
At
DatasetRefresh_NoParam_Simple.ps1:9
char:1
+ Connect-PowerBIServiceAccount -Credential $myCred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (Microsoft.Power...IServiceAccount:ConnectPowerBIServiceAccount) [Connect-Po
werBIServiceAccount], Exception
+ FullyQualifiedErrorId : Failed to populate environments in settings,Microsoft.PowerBI.Commands.Profile.ConnectPo
werBIServiceAccount
Invoke-PowerBIRestMethod : Login first with Login-PowerBIServiceAccount
At
DatasetRefresh_NoParam_Simple.ps1:19
char:1
+ Invoke-PowerBIRestMethod -Url $RefreshDSURL -Method Post -Body $MailF ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-PowerBIRestMethod], Exception
+ FullyQualifiedErrorId : System.Exception,Microsoft.PowerBI.Commands.Profile.InvokePowerBIRestMethod
I feel like there is a server setting that I'm missing, but I'm logged into both with admin credentials and running powershell as administrator. Any help would be appreciated.
EDIT
I tried to run a different script to also show the other error I am getting.
\\BlahServerName\WorkSpace\PowerBI\BDW_Resources\Admin\DatasetRefresh_PS\DatasetRefresh_Param_onlyDataset.ps1 : File
\\BlahServerName\WorkSpace\PowerBI\BDW_Resources\Admin\DatasetRefresh_PS\DatasetRefresh_Param_onlyDataset.ps1 cannot be
loaded. The file
\\BlahServerName\WorkSpace\PowerBI\BDW_Resources\Admin\DatasetRefresh_PS\DatasetRefresh_Param_onlyDataset.ps1 is not
digitally signed. You cannot run this script on the current system. For more information about running scripts and
setting execution policy, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ \\BlahServerName\WorkSpace\PowerBI\BDW_Resources\Admin\DatasetRefresh_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
This is the one I got too that led me to look at the execution policies, but if both of my servers have the same policy settings, why am I able to execute on one and not the other? (Again, I'm logged in as myself on both with admin privileges)

If you are executing it from SQL Server - I assume that is using SQL Agent?
Be aware of the context that SQL Server Agent executes in - have you created a SQL Agent Proxy using a defined credential to ensure the context?

Related

error while trying to install yarn in vscode when i had earlier been using npm

This is the error message
yarn : File C:\Users\Admin\AppData\Roaming\npm\yarn.ps1 cannot be loaded
because running scripts is disabled on this system. For more
information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ yarn
+ ~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
For those who are not aware of how to solve this error using Windows PowerShell
Open PowerShell (Run As Administrator)
Check the current execution policy using this command
Get-ExecutionPolicy
# You should get 'Restricted'
Run this command to make it 'Unrestricted'
Set-ExecutionPolicy Unrestricted
Check again whether execution policy changed by running this command
Get-ExecutionPolicy
You should get 'Unrestricted'
Now try to run nodemon on your project
nodemon 'filename.js'
Hope this would be helpful

PowerShell Import-Module SqlServer Could not load PSSnapins.dll

Trying to Import SQL Server module to use Invoke-Sqlcmd and whether in raw PowerShell ISE (as Administrator and non-Admin) or via SSMS I get the same error and wondering if someone could help please?
The version I am using is an unzipped nupkg of sqlserver.21.1.18230 (the latest release) and the DLL's are in the right location (C:\Program Files\WindowsPowerShell\Modules). The DLL's are not showing as Blocked in the Properties. Security is showing "Full Control" on the DLL in the Properties.
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { $_.PSChildName -match '^(?!S)\p{L}'} |
Select PSChildName, Version, Release
##Get PowerShell version
$PSVersionTable
$Env:PSModulePath
#Get-PSRepository
Get-ExecutionPolicy -List
[Environment]::Is64BitProcess
Import-Module SqlServer
The Import-Module is failing with the following results (after the error a raft of other DLL's from the SqlServer.psm1 script are listed also erroring)
PSChildName Version Release
----------- ------- -------
v2.0.50727 2.0.50727.4927
v3.0 3.0.30729.4926
Windows Communication Foundation 3.0.4506.4926
Windows Presentation Foundation 3.0.6920.4902
v3.5 3.5.30729.4926
Client 4.8.03752 528040
Full 4.8.03752 528040
Client 4.0.0.0
C:\Users\app-itsme\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\windows\system32\WindowsPowerShell\v1.0\Modules
Import-Module : Could not load file or assembly 'file:///C:\Program Files\WindowsPowerShell\Modules\SqlServer\Microsoft.SqlServer.Management.PSSnapins.dll' or one of its dependencies. Operation is not
supported. (Exception from HRESULT: 0x80131515)
At C:\Program Files\WindowsPowerShell\Modules\SqlServer\SqlServer.psm1:61 char:25
+ ... $binaryModule = Import-Module -Name $binaryModulePath -PassThru
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Import-Module], FileLoadException
+ FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand
The variable '$binaryModule' cannot be retrieved because it has not been set.
At C:\Program Files\WindowsPowerShell\Modules\SqlServer\SqlServer.psm1:63 char:29
+ $importedModules += $binaryModule
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (binaryModule:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
Any help appreciated
Thank you
The issue was still around permissions and installation. Installation seemed to have to be at non-admin and then Install-Module at non-admin. It was then available as admin.
Also the Powershell admin doesnt have internet access something that Powershell assumes always exists which added to the woes of other solutions being attempted. Had to set proxy firewall etc
Thanks

Otcopus deploy - SQL - Deploy DACPAC "Could not connect to database server."

Trying out Octopus deploy for the first time. Trying to deploy a dacpac to a machine and it keeps on failing. I keep on getting the following error:
Exception calling "Extract" with "4" argument(s): "Could not connect to database server."
At C:\Octopus\Work\20191023152506-102-81\Script.ps1:394 char:13
+ $dacServices.Extract($dbDacPacFilepath, $TargetDatabase, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DacServicesException
The remote script failed with exit code 1
The action SQL - Deploy DACPAC on Staging failed
I am currently using SQL server 2017 and have the dacframework installed for SQL server 2016. for the connection string i tried using ., localhost, and the name of the server given in sql management studio. I am not passing any credentials, I am using integrated security. I am also passing the database name as well.
I followed this youtube video also, just without using the project variables.
In my previous experience I just used the SqlPackage.exe to deploy a dacpac. Helps for manually testing and polishing out permissions, or other issues.
For example:
#example usage:Generate-DBUpdate-Script -server $dbServer -db $dbName -user $dbUser -psw $dbPassword -dacpacFilePath $dacpacFile -publishProfilePath ".\Publish\$dbPublishProfile" -outputFile $SqlUpgradeArtifactName
function Generate-DBUpdate-Script($server, $db, $user, $psw, $dacpacFilePath, $publishProfilePath, $outputFile)
{
#generate an update script
& 'C:\Program Files (x86)\Microsoft SQL Server\110WorkingDAC\DAC\bin\SqlPackage.exe' /Action:Script /OutputPath:$outputFile /SourceFile:$dacpacFilePath /Profile:$publishProfilePath /TargetServerName:$server /TargetDatabaseName:$db /TargetUser:$user /TargetPassword:$psw
#save generated script as deployment artifact
New-OctopusArtifact $outputFile
}
Can change the action to publish to avoid generating the script and just deploy straight away.
Hope that helps.

SQL PowerShell Snapin Problems

I am currently trying to execute the command
add-sqlsnappin
which give me error message
SQL Server Provider for Windows PowerShell is not installed.
At C:\deploy\SIF.Sitecore.Commerce.1.1.4\Modules\DeployCommerceDatabase\DeployCommerceDatabase.psm1:215 char:9
+ throw "SQL Server Provider for Windows PowerShell is not inst ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (SQL Server Prov... not installed.:String) [], RuntimeException
+ FullyQualifiedErrorId : SQL Server Provider for Windows PowerShell is not installed.
Which cannot be true since
if (Get-Module -ListAvailable -Name SqlServer) {
>> Write-Host "Module exists"
>> } else {
>> Write-Host "Module does not exist"
>> }
Module exists
so why can i not add-sqlsnapin?
The sqlserver module and SQL Server snap-in aren't the same thing. So, the module existing doesn't mean the snap-in is registered in-order for you to load it. Give the Running SQL Server PowerShell on MSDN a look. If you want the SqlServer module, try the following:
Import-Module sqlserver

Deploy SSIS task in VSTS

I'm trying to use a "Deploy SSIS" task in VSTS. I get this error:
Task_InternalError Exception calling ".ctor" with "1" argument(s): "Failed to connect to server xyz
It is asking for the following information below. How do I go about finding the information below needed for this task? The path to the .ispac file is easy since it is uploaded. I also think the name of the server is correct.
-Display name
-Path to .ispac file
-Name of the SSIS project in the .ispac file
-Name of the SQL Server hosting the SSIS catalog database
-Name of the SSIS catalog
-Name of the SSIS catalog folder
-Description of the SSIS catalog folder
Edit:
(detailed error log)
2017-09-20T12:55:05.6438379Z ##[debug]Env:INPUT_PROJECTFILEPATH: 'd:\a\3\s\HR Data.ispac'
2017-09-20T12:55:05.6468345Z ##[debug]Env:INPUT_PROJECTNAME: 'xyz'
2017-09-20T12:55:05.6488487Z ##[debug]Env:INPUT_SERVERNAME: 'xyz'
2017-09-20T12:55:05.6538355Z ##[debug]Env:INPUT_CATALOGNAME: 'xyz'
2017-09-20T12:55:05.6558353Z ##[debug]Env:INPUT_FOLDERNAME: 'xyz'
2017-09-20T12:55:05.6568351Z ##[debug]Env:INPUT_FOLDERDESCRIPTION (empty)
2017-09-20T12:55:05.6618490Z ##[debug]Env:INPUT_PROJECTPARAMETERS (empty)
2017-09-20T12:55:05.6628342Z SQL Connection String: Data Source=xyz;Initial Catalog=master;Integrated Security=SSPI;
2017-09-20T12:55:05.6638360Z Project file path: d:\a\3\s\xyz.ispac
2017-09-20T12:55:22.8057049Z ##[debug]Leaving D:\a_tasks\DeploySsis_582498ad-81d6-48b7-b9bc-fd0e5dec2e2b\0.1.0\DeploySSISTask.ps1.
2017-09-20T12:55:22.8388650Z ##[debug]Caught exception from task script.
2017-09-20T12:55:22.8418679Z ##[debug]Error record:
2017-09-20T12:55:22.8948708Z ##[debug]D:\a_tasks\DeploySsis_582498ad-81d6-48b7-b9bc-fd0e5dec2e2b\0.1.0\DeploySSISTask.ps1 : Task_InternalError Exception calling ".ctor" with "1" argument(s): "Failed to connect to server xyz."
2017-09-20T12:55:22.8968708Z ##[debug]At line:1 char:1
2017-09-20T12:55:22.8978718Z ##[debug]+ . 'd:\a_tasks\DeploySsis_582498ad-81d6-48b7-b9bc-fd0e5dec2e2b\0.1.0\ ...
2017-09-20T12:55:22.8988697Z ##[debug]+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-09-20T12:55:22.8998697Z ##[debug] + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
2017-09-20T12:55:22.9008861Z ##[debug] + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,DeploySSISTask.ps1
2017-09-20T12:55:22.9018704Z ##[debug]
2017-09-20T12:55:22.9048688Z ##[debug]Script stack trace:
2017-09-20T12:55:22.9078699Z ##[debug]at ,
D:\a_tasks\DeploySsis_582498ad-81d6-48b7-b9bc-fd0e5dec2e2b\0.1.0\DeploySSISTask.ps1: line 97
2017-09-20T12:55:22.9098691Z ##[debug]at , : line 1
2017-09-20T12:55:22.9108747Z ##[debug]at , : line 22
2017-09-20T12:55:22.9118883Z ##[debug]at , : line 18
2017-09-20T12:55:22.9128882Z ##[debug]at , : line 1
2017-09-20T12:55:22.9148698Z ##[debug]Exception:
2017-09-20T12:55:22.9188799Z ##[debug]Microsoft.PowerShell.Commands.WriteErrorException: Task_InternalError Exception calling ".ctor" with "1" argument(s): "Failed to connect to server xyz."
2017-09-20T12:55:22.9478706Z ##[error]Task_InternalError Exception calling ".ctor" with "1" argument(s): "Failed to connect to server xyz."
2017-09-20T12:55:22.9478706Z ##[debug]Processed: ##vso[task.logissue type=error]Task_InternalError Exception calling ".ctor" with "1" argument(s): "Failed to connect to server xyz."
2017-09-20T12:55:22.9508700Z ##[debug]Processed: ##vso[task.complete result=Failed]
Regarding Deploy SSIS task, it connects to the server with windows authentication (Integrated Security=SSPI), but the Hosted agent uses service account that can’t connect to the server.
You can set up a private build agent (running as your account) and queue build with that build agent, or you can refer to the source code to build the PowerShell script to deploy SSIS project with SQL Server authentication (User ID=XXX; Password=XX)
A related thread: Build and Deploy SSIS packages using VSTS

Resources