Unable to run a dtsx package through powershell - sql-server

I am trying to run a bunch of stored procedures and in the end run a dtsx package which is already set up in the SQL server through powershell
I get the below as the location of the dtsx while initiating the package from SQL mgmt studio using powershell. However my PS ISE is not talking to this location.
PS SQLSERVER:\SSIS\SM024591\DEFAULT\Catalogs\SSISDB\Folders\006_EXPORTDATA\Projects\EXPORT-P-USU-MULTI-DSDB\Packages\EXPORT-P-USU-MULTI-DSDB%2Edtsx
I tried to find the real path of PS SQLSERVER:\SSIS\SM024591\DEFAULT\Catalogs\SSISDB\Folders\006_EXPORTDATA\Projects\EXPORT-P-USU-MULTI-DSDB\Packages\EXPORT-P-USU-MULTI-DSDB%2Edtsx which seems to be in a DB server engine
and it seems like mapping is only for PS instance and only valid once the PS session running
Any guidance would be much appreciated so that I can run the below with the correct path or any other suggestions are welcome too
(Get-ChildItem Microsoft.SqlServer.Management.PSProvider\SqlServer::SQLSERVER:\SSIS\SM024761\DEFAULT\Catalogs\SSISDB\Folders\006_EXPORTDATA\projects | WHERE { $_.Name -eq 'EXPORT-P-USU-MULTI-DSDB.dtsx' }).Execute("false", 0)

Related

Unable to Process Partition in Power BI Premium Model Using SQL Server Agent Powershell CmdExec Step

I am trying to use Analysis Services Cmdlets to process partitions on a Power BI Premium Model. My PowerShell script works fine when run from ISE, the command line, and when scheduled using windows task scheduler; however, when I try to schedule the PowerShell script using a SQL Server 2019 Agent job using a step type of
Operating System (CmdExec)" the following error message is encountered.
Message Executed as user: MyDomain\MyUser. Invoke-ProcessPartition : The
connection string is not valid. At
C:\Users\MyUser\Desktop\PS1\SSAS\wtf.ps1:15 char:11 + $asResult =
Invoke-ProcessPartition -Credential $UserCredential -Server...+
CategoryInfo : NotSpecified: (:) [Invoke-ProcessPartition],
ConnectionException + FullyQualifiedErrorId :
Microsoft.AnalysisServices.ConnectionException,Microsoft.AnalysisServices.PowerShell.Cmd
lets.ProcessPartition.
I have followed the steps in this blog article to setup the job. The same windows user is used in all three run scenarios. The SQL server is my local development SQL server of which the windows user is SA on the SQL Server and Windows Admin. The same machine the SQL Instance is on is being used for successfully executing the other three ways of running the PS scripts, (ISE, Command Line, & Windows Task Scheduler)
If I run the following from the command line on the same machine as the SQL server, my local host, the PowerShell script runs successfully.
PowerShell -File "C\Users\MyUser\Desktop\PS1\SSAS\wtf.ps1"
Below is my PowerShell script modified to be as small as possible to demonstrate issue and of course sensitive information redacted. Thanks in advance for any help, I'm at a loss at how to proceed. I really need this to work from the SQL agent so I don't have to guess when steps that the processing depends on have completed.
$ErrorActionPreference=”Stop”
Import-Module "SqlServer"
$User = "MyUser#MyDomain.com"
$PWord = ConvertTo-SecureString -String "MyPassword" -AsPlainText -Force
$UserCredential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord
$server = "powerbi://api.powerbi.com/v1.0/myorg/MyWorkspace"
$db = "MyModel"
$asResult = Invoke-ProcessPartition -Credential $UserCredential -Server $server -PartitionName "DimDate" -TableName "DimDate" -Database $db -RefreshType "Full"
The step code in my job is:
powershell -File "C:\Users\MyUser\Desktop\PS1\SSAS\wtf.ps1"
UPDATE:
I have discovered that if I target an on-prem SSAS server, the exact same code, with the exception of the server variable, does work using the SQL Agent method, but of course switching to on-prem is not what I desire.
UPDATE 2:
When I right click on a job within SSMS, and click "Start PowerShell" this opens up a SQL Server PowerShell Window. If I try executing the script this way I get the below error message. The account being used does not have MFA enabled.
Unable to obtain authentication token using the credentials provided.
If your Active Directory tenant administrator has configured
Multi-Factor Authentication or if your account is a Microsoft Account,
please remove the user name and password from the connection string,
and then retry. You should then be prompted to enter your credentials.
If I don't pass a credential to the Invoke-ProcessPartition command using the SQL Server PowerShell window, I get prompted for credentials and the call works. Of course I can't use this as a workaround because I need it to run unattended.
I also tried opening PowerShell ISE as the account being used for authentication to the Workspace in the PS script and it also gives the same error as the SQL Server Agent Job does.
The connection string is not valid.
I have found a solution to the issue. The resolution was twofold.
First problem was that when PowerShell was being run from the SQL Server Agent, the Version of the SqlServer module was an older outdated version. I found this out by executing a ps1 file from the SQL Server Agent Job using the following code and looking in the job history results.
Get-Command -module sqlserver invoke*
I tried running
Install-Module sqlserver -AllowClobber -Scope AllUsers
as an admin but it did not update the SqlServer module the SQL agent was running. Instead I created a ps1 file that simply runs
Install-Module sqlserver -AllowClobber -Scope CurrentUser -Force
and called the script using a SQL Agent CMD task and this updated the SqlServer module version. After this I started getting a more useful error message:
Unable to obtain authentication token using the credentials provided.
If your Active Directory tenant administrator has configured
Multi-Factor Authentication or if your account is a Microsoft Account,
please remove the user name and password from the connection string,
and then retry. You should then be prompted to enter your
credentials
With this new error message, I decided to try an alternate way of supplying credentials by utilizing Azure Service Principals. This new method resulted in success from all PowerShell methods including SQL Server Agent Job. The steps implemented are outlined in this Power BI Documentation.
The final PS code looks like this.
$ErrorActionPreference=”Stop”
Import-Module "SqlServer"
$AppId = "AAD_App_Registration_Application_Client_Id"
$TenantId = "AAD_App_Registration_Directory_Tenant_Id"
$AppSecret = "AAD_App_Registration_CertificatesAndSecrets_ClientSecret"
$PWord = ConvertTo-SecureString -String $AppSecret -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $AppId, $PWord
Invoke-ProcessPartition -Server "powerbi://api.powerbi.com/v1.0/myorg/MyModel" -PartitionName "DimDate" -TableName "DimDate" -Database "MyModel" -RefreshType "Full" -ServicePrincipal -ApplicationId $AppId -TenantId $TenantId -Credential $Credential

Command line that opens SSMS not actually logging into database

I am trying to log in to a remote MS SQL Server 2012 by using SSMS and I'm opening SSMS via command line. When I run my command SSMS starts up and it opens my .sql file that I specify but it doesn't actually log in to my database. Here's the two versions of my command that I've run:
"C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\Ssms.exe"
-S 172.18.211.76 -U USERNAME -P PASSWORD -nosplash "full_path_to_my_.sql_file"
and
"C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\Ssms.exe"
"full_path_to_my_.sql_file" -S 172.18.211.76 -U USERNAME -P PASSWORD -nosplash
Both commands start SSMS successfully and open my .sql file, but neither commands actually logs into my database for me. What I mean by that is that after I run my command the object explorer in SSMS looks like this:
so before I can do anything in my database I have to click on the connect icon and enter my credentials so the object explorer now looks like this:
In the past I was doing this and if I remember correctly I was able to use a command prompt to open SSMS and have it log into a database all at once but I can't find the command that I used to do it previously. If someone could help me out I'd greatly appreciate it. Thanks in advance.
EDIT: here's the version of SSMS I'm using, just in case it's important:
Microsoft SQL Server Management Studio 13.0.15600.2
After looking at this further I have 2 observations:
I didn't specify the name of the database I wanted to connect to in my command using
the -d switch.
I checked and either way, using the -d switch or without, even though the object
explorer doesn't show that I have an active connection I can still run the .sql file
that I specified to be loaded.
So, I guess the problem isn't necessarily that the command I listed doesn't connect to the database, the problem is that a connection is made but it isn't reflected in the object explorer. Thanks everyone.
EDIT: as #larnu pointed out I was running an older version of SSMS. I installed v17.9.1 and tried to run the command but got the same result, after the command is run a connection is made to the database but it is not reflected in the object explorer. Not having the connection display in the object explorer is minor seeing as the connection is actually made and statements can be run in SSMS successfully after SSMS has been opened by the command.

(SQL Server 14) SQL Server WMI Provider is not available on <COMPUTER NAME>

I'm trying to enable the TCP/IP and Named Pipes Protocols using PowerShell, but I'm unable to due to the following error.
Here is the code I'm using to connect to the Server Protocols.
Is there a way to make SQL Server WMI available through PowerShell?
Here is what the $machine variable looks like after this cmdlet:
$machine = New-Object 'Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer' -ArgumentList 'STORE000013'
I think you are missing one... When running PowerShell (PowerShell.exe) instead of SQL Server PowerShell, first execute the following statements to manually load the required assemblies:
# Load the assemblies
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")
For more information, here:
https://technet.microsoft.com/en-us/library/dd206997(v=sql.105).aspx

sql server cmdlet (sqlps) and remote server

I'm using sqlps powershell module to get some data from my local database. My code is something like this
PS C:\> Import-Module sqlps -DisableNameChecking
PS SQLSERVER:\> cd "SQL\myMachineName\..."
It works correctly, but sometimes I need to connect to a remote server running SQL server, so I use Enter-PSSession. Is this the correct way or can I somehow map that server under local SQLSERVER:\SQL directory?
The PowerShell provider for SQL server can be used to connect to a remote SQL server without PowerShell remoting. For example, at the SQLSERVER: drive, you can change the location to a different SQL Server using
PS SQLSERVER:\> cd "SQL\remoteMachineName\SQLInstanceName"

PowerShell Copy on a mapped or UNC Drive (SQL Server Job Agent)

What am I doing wrong?
As a step in a Job I am trying to overwrite file by copying the drive is mapped, but I am using unc path just in case.
In PowerShell this works in the command line interface.
Copy \V240\folder\foo.txt \V240\folder\bar.txt
1
When I put it in a Job Agent in SQL Server with the PowerShell option it says successfully completed but the file has not been copied.
The Powershell SQL Agent job step starts Powershell sqlps host in the SQL Server provider. I would try setting location (cd) to the filesystem provider before copying
set-location c:\
Copy \V240\folder\foo.txt \V240\folder\bar.txt

Resources