简体   繁体   English

请帮助我使用重新排列路径的Power Shell脚本

[英]Please help me with a Power shell Script which rearranges Paths

I have both Sybase and MSFT SQL Servers installed. 我同时安装了Sybase和MSFT SQL Server。 There is a time when Sybase interferes with MS SQL because they have they have some overlapping commands. Sybase有时会干扰MS SQL,因为它们具有一些重叠的命令。

So, I need two scripts: 因此,我需要两个脚本:

A) When runs, script A backs up the current path, grabs all paths that contain sybase or SYBASE or SyBASE (you get the point) in them and move them all at the very end of the path, while preserving the order. A)运行时,脚本A备份当前路径,抓取其中包含sybase或SYBASE或SyBASE(您已掌握要点)的所有路径,并将它们全部移动到路径的尽头,同时保留顺序。

B) When it runs, script B restores the path from back-up. B)运行时,脚本B从备份中还原路径。

Both script a and script b should affect the path immediately. 脚本a和脚本b都应立即影响路径。 So, if a.bat that calls patha.ps1, pathb.ps1 looks like so: 因此,如果调用patha.ps1的a.bat,pathb.ps1如下所示:

@REM Old path here
call patha.ps1
@REM At this point the effective path should be different.
call pathb.ps1
@REM Effective old path again

Please let me know if this does not make sense. 如果这没有意义,请告诉我。 I am not sure if call command is the best one to use. 我不确定call命令是否是最好的命令。

I have never used PS before. 我以前从未使用过PS。 I can try to formulate the same thing in Python (I know SO users tend to ask for "What have you tried so far"). 我可以尝试在Python中表达同样的内容(我知道SO用户倾向于询问“到目前为止您尝试了什么”)。 Well, at this point I am VERY slow at writing anything in Power Shell language. 好吧,在这一点上,我用Power Shell语言编写任何东西的速度都很慢。

Please help. 请帮忙。

First of all: call will be of no use here as you are apparently writing a batch file and PowerShell scripts have no association to run them by default. 首先:在这里call将毫无用处,因为您显然正在编写一个批处理文件,并且PowerShell脚本默认没有关联来运行它们。 call is for batch files or subroutines. call用于批处理文件或子例程。

Secondly, any PowerShell script you call from a batch file cannot change environment variables of the caller's environment. 其次,您从批处理文件调用的任何PowerShell脚本都不能更改调用者环境的环境变量。 That's a fundamental property of how processes behave and since you are calling another process, this is never going to work. 这是流程行为的基本属性,并且由于您正在调用另一个流程,因此这永远都行不通。

I'm not so sure why you are even using a batch file here in the first place if you have PowerShell. 如果您使用PowerShell,我不确定您为什么还要在这里甚至使用批处理文件。 You might just as well solve this in PowerShell completely. 您可能还需要在PowerShell中完全解决此问题。

However, what I get from your problem is that the best way to resolve this is probably the following: Create two batch files that each set the PATH appropriately. 但是,我从您的问题中得到的是解决此问题的最佳方法可能是以下方法:创建两个批处理文件,每个文件都适当地设置了PATH。 You can probably leave out both the MSSQL and Sybase paths from your usual PATH and add them solely in the batch files. 您可能可以从通常的PATH中忽略MSSQL和Sybase路径,而仅将它们添加到批处理文件中。 Then create shortcuts to 然后创建快捷方式

cmd /k set_mssql_path.cmd

and

cmd /k set_sybase_path.cmd

each of which now is a shortcut to a shell to work with the appropriate database's tools. 现在,每种方法都是外壳程序的快捷方式,可与相应的数据库工具一起使用。 This is how the Visual Studio Command Prompt works and it's probably the cleanest solution you have. 这就是Visual Studio命令提示符的工作方式,它可能是您拥有的最干净的解决方案。 You can use the color and prompt commands in those batches to make the two different shells distinct so you always know what environment you have. 您可以在这些批处理中使用colorprompt命令使两个不同的shell区别开,以便您始终知道自己所处的环境。 For example the following two lines will color the console white on blue and set a prompt indicating MSSQL: 例如,以下两行将控制台颜色变为蓝色,并设置提示以指示MSSQL:

color 1f
prompt MSSQL$S$P$G

This can be quite handy, actually. 实际上,这可能非常方便。

Generally, trying to rearrange the PATH environment variable isn't exactly easy. 通常,尝试重新排列PATH环境变量并非易事。 While you could trivially split at a ; 虽然你可以在一点上分裂; this will fail for paths that itself contain a semicolon (and which need to be quoted then). 这对于本身包含分号的路径将失败(然后需要用引号引起来)。 Even in PowerShell this will take a while to get right so I think creating shortcuts specific to the tools is probably the nicest way to deal with this. 即使在PowerShell中,这也需要花费一些时间,因此我认为创建特定于工具的快捷方式可能是解决此问题的最佳方法。

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

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