Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command is running fine in console but not when used by Powershell

I have a PS script that should deploy a project to my SSIS server. When I run the generated command in a console it runs fine but when the command is executed from Powershell it fails because of this (windows) error :

TITLE: SQL Server Integration Services

The path format is invalid. Parameter name: DestinationPath (ISDeploymentWizard)

ADDITIONAL INFORMATION:

The path format is invalid. (Microsoft.SqlServer.IntegrationServices.Wizard.Common)

If I run the generated command from a console it runs fine:

D:\Deploy\ISDeploymentWizard.exe /Silent /ModelType:Project /SourcePath:"D:\Deploy\Receive\My_Beautiful_Project.ispac" /DestinationServer:"localhost" /DestinationPath:"/SSISDB/My Beautiful Project/My_Beautiful_Project" /ProjectPassword:"SuperSecretPassword"

The script (thanks to suggestions from Guenther Schmitz and Janne Tukaanen) :

#region script configuration
$SsisServer = "."
$ProjectFileFolder = "D:\Deploy\Receive"
$ProjectFileName = "My_Beautiful_Project.ispac"
$ProjectFilePassword = "SuperSecretPassword"
$FolderName = "My Beautiful Project"
$ProjectName = "My_Beautiful_Project"
$ISDeploymentWizard = "D:\Deploy\ISDeploymentWizard.exe"
#endregion

#region project deployment
# Create command line arguments
$DestinationPath = "/SSISDB/" + $FolderName + "/" + $ProjectName
$ProjectFilePath = $ProjectFileFolder + "\" + $ProjectFileName
$cmd = $ISDeploymentWizard
$arg1 = "/Silent"
$arg1a= "/ModelType:Project"
$arg2 = "/SourcePath:""$ProjectFilePath"""
$arg3 = "/DestinationServer:""$SsisServer"""
$arg4 = "/DestinationPath:""$DestinationPath"""
$arg5 = "/ProjectPassword:""$ProjectFilePassword"""
Write-Host "$cmd" $arg1 $arg1a $arg2 $arg3 $arg4 $arg5
& "$cmd" $arg1 $arg1a $arg2 $arg3 $arg4 $arg5


Write-Host "Done"
#endregion 
like image 599
Henrov Avatar asked Nov 29 '18 07:11

Henrov


People also ask

Why is PowerShell not recognizing commands?

Cause. This is caused by the user (or system) PATH environment variable not containing the directory where the PowerShell executable resides. It's usually located at C:\Windows\System32\WindowsPowerShell\v1.

Why is PowerShell not executing scripts?

It could be PowerShell's default security level, which (IIRC) will only run signed scripts. That will tell PowerShell to allow local (that is, on a local drive) unsigned scripts to run. Then try executing your script again. You have to run Powershell with administrator privileges, at least under Windows 8!

How do I enable PowerShell commands?

To enable Windows PowerShell Integrated Scripting Environment (ISE) Start Server Manager. Click Features and then click Add Features. In Select Features, click Windows PowerShell Integrated Scripting Environment (ISE).

Do CMD commands work in PowerShell?

In addition, most Command Prompt commands are usable in PowerShell, whether natively or through aliases.


3 Answers

There is no need to declare the following variables $arg1 $arg1a $arg2 $arg3 $arg4 $arg5, just run the following command (why declaring variables and storing their values in another variables??):

& $cmd /Silent /ModelType:Project /SourcePath:$ProjectFilePath /DestinationServer:$SsisServer /DestinationPath:$DestinationPath /ProjectPassword:$ProjectFilePassword
like image 66
Yahfoufi Avatar answered Oct 18 '22 18:10

Yahfoufi


you are missing the executable in the line below Write-Host.

change

& $arg1 $arg2 $arg3 $arg4 $arg5 

to

& $cmd $arg1 $arg2 $arg3 $arg4 $arg5 
like image 43
Guenther Schmitz Avatar answered Oct 18 '22 19:10

Guenther Schmitz


If you have troubles to start console apps in powershell (typically because of multiple arguments), you may execute it through cmd (in powershell)

cmd /c "$cmd $arg1 $arg2 $arg3 $arg4 $arg5"

There is also another option using Process class, so you don't have to use cmd:

$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo 
$ProcessInfo.FileName = "D:\Deploy\ISDeploymentWizard.exe"
$ProcessInfo.Arguments = "$arg1 $arg1a $arg2 $arg3 $arg4 $arg5"
$ProcessInfo.RedirectStandardError = $true 
$ProcessInfo.RedirectStandardOutput = $true 
$ProcessInfo.UseShellExecute = $false 
$Process = New-Object System.Diagnostics.Process 
$Process.StartInfo = $ProcessInfo 

$Process.Start() | Out-Null 
$output = $Process.StandardOutput.ReadToEnd() 
$errors = $Process.StandardError.ReadToEnd()
$Process.WaitForExit() 
Write-Host $output 
Write-Error $errors 

You can check this for some more details: PowerShell, stream Process output and errors while running external process

like image 1
Mike Twc Avatar answered Oct 18 '22 20:10

Mike Twc