简体   繁体   English

从命令行参数到Powershell_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. 我在Powershell_ISE中开发了一个脚本,用于在一台服务器上备份SQL Server数据库,将文件移动到另一台服务器并使用不同的数据库名称还原到目标服务器。 Works fine. 工作良好。

It uses 6 mandatory parameters. 它使用6个必需参数。 Within Powershell_ISE I can run 在Powershell_ISE内我可以跑

    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参数错误

If I call Powershell instead of Powershell_ISE, all is fine: 如果我打电话给Powershell而不是Powershell_ISE,一切都很好:

    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: 成功完成:

从Powershell成功运行

Can anyone advise how to pass command line parameters into Powershell_ISE? 任何人都可以建议如何将命令行参数传递给Powershell_ISE?

Thanks 谢谢

You cannot use parameters into Powershell_ISE from Command Line. 您不能在命令行中将参数用于Powershell_ISE。

Some background: 一些背景:

You use PowerShell ISE to develop your scripts (or notepad or whatever) and you use powershell.exe to execute them when finished. 您使用PowerShell ISE开发脚本(或记事本或其他),并在完成后使用powershell.exe执行它们。 You can execute scripts in different ways: 您可以通过不同方式执行脚本:

  • From a DOS command , calling powershell.exe as you indicate in your question. DOS命令 ,调用powershell.exe,如您在问题中指出的那样。
  • From a PowerShell command , calling C:\\PowershellScripts\\Servers\\CRMBackup.ps1 PowerShell命令 ,调用C:\\ PowershellScripts \\ Servers \\ CRMBackup.ps1
  • From Windows Task Scheduler (or similar tool...) Windows任务计划程序 (或类似工具...)
  • From a CI tool (Jenkins, TeamCity...) 来自CI工具 (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 . 当然,您可以在开发脚本时从PowerShell ISE执行脚本,可以通过单击“开始”(或按F5)来完成,您可以按照此处的说明传递参数。 But you cannot execute an script passing parameters to powershell_ise.exe . 但是您无法执行将参数传递给powershell_ise.exe的脚本

did you try this? 你试过这个吗? (powershell.exe instead of powershell_ise.exe) (powershell.exe而不是powershell_ise.exe)

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\

Unfortunatelly you cannot start ISE from command line with your parameters. 不幸的是,您无法使用参数从命令行启动ISE。 The list of possible parameters that could only customize your session is here: 可以自定义会话的可能参数列表如下:

https://technet.microsoft.com/en-us/library/hh847883 https://technet.microsoft.com/en-us/library/hh847883

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM