Search code examples
powershellcommand-lineparameter-passingpowershell-ise

Parameters into Powershell_ISE from Command Line


I've developed a script in Powershell_ISE to backup a SQL Server database on one server, move the file to another server and restore to the target server with a different database name. Works fine.

It uses 6 mandatory parameters. Within Powershell_ISE I can run

    C:\PowershellScripts\Servers\CRMBackup.ps1 -SrcSrv uk-sb-sql12\wss -SrcDb UAT_2016_MSCRM
     -SrcBak \\uk-sb-sql12\backup\ -TrgSrv uk-sb-sql12\wss -TrgDb preprod_mscrm66 -TrgBak 
     \\uk-sb-sql12\backup\

and it works fine.

I can't get it to run from the Command Line though. How do I achieve this?

If I run

    powershell_ise.exe "C:\PowershellScripts\Servers\CRMBackup.ps1" -SrcSrv uk-sb-sql12\wss 
    -SrcDb UAT_2016_MSCRM -SrcBak \\uk-sb-sql12\backup\ -TrgSrv uk-sb-sql12\wss -TrgDb 
    preprod_mscrm66 -TrgBak \\uk-sb-sql12\backup\

from a Command prompt I get an error:

Powershell_ISE parameter error

If I call Powershell instead of Powershell_ISE, all is fine:

    powershell.exe "C:\PowershellScripts\Servers\CRMBackup.ps1" -SrcSrv uk-sb-sql12\wss 
    -SrcDb UAT_2016_MSCRM -SrcBak \\uk-sb-sql12\backup\ -TrgSrv uk-sb-sql12\wss -TrgDb 
    preprod_mscrm66 -TrgBak \\uk-sb-sql12\backup\

completes successfully:

Successful run from Powershell

Can anyone advise how to pass command line parameters into Powershell_ISE?

Thanks


Solution

  • You cannot use parameters into Powershell_ISE from Command Line.

    Some background:

    You use PowerShell ISE to develop your scripts (or notepad or whatever) and you use powershell.exe to execute them when finished. You can execute scripts in different ways:

    • From a DOS command, calling powershell.exe as you indicate in your question.
    • From a PowerShell command, calling C:\PowershellScripts\Servers\CRMBackup.ps1
    • From Windows Task Scheduler (or similar tool...)
    • From a CI tool (Jenkins, TeamCity...)

    Of course you can execute scripts from PowerShell ISE while developing them that can be done by clicking Start (or pressing F5) and you can pass parameters as explained here. But you cannot execute an script passing parameters to powershell_ise.exe.