嵌入Powershell脚本问题 [英] Embedding in the Powershell script issue

查看:67
本文介绍了嵌入Powershell脚本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据另一个用户的建议创建此问题,这是

Creating this question based on the advice of another users advice, it is a follow-up of this.

这种情况是,我当前正在尝试创建一个Powershell脚本,首次运行的用户可以运行该脚本,该脚本将安装一堆程序并完成一些配置更改.配置更改之一是创建一个包含两个shell命令的文件(在下面发布),每次加载GitBash时将执行该文件.

The scenario is that I am currently trying to create a Powershell script, that a first-time user can run, which will install a bunch of programs and complete several configuration changes. One of the configuration changes is to create a file which has two shell commands (posted below), which will then be executed everytime GitBash is loaded.

我要输入到.sh文件中的命令是..(除去了和")

The commands I'm trying to input into the .sh file are.. (With " and ' removed)

alias proxyon=source $HOMEPATH/.proxy/proxy-switch.sh on

alias proxyoff=source $HOMEPATH/.proxy/proxy-switch.sh off

我正在尝试使用以下命令进行操作

I'm trying to do this using the following command

add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source "$HOMEPATH/.proxy/proxy-switch.sh on"'" 

但是,执行ps1脚本会引发错误,最上面一行是

But, the execution of the ps1 script throws an error, with the top line being

Add-Content : A positional parameter cannot be found that accepts argument '/.proxy/proxy-switch.sh'.

现在,另一个用户告诉我,如果我要使用double,则需要对内引号进行转义.据我所知,Powershell中的转义字符是`,因此我尝试了以下内容.

Now, another user told me that if i was going to use double " I would need to escape the inner quotes. The escape char in Powershell from what I can tell is ` , so I have tried the following.

add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source `"$HOMEPATH/.proxy/proxy-switch.sh on"'" 

以下哪个

以以下字符串开头:C:\ Chef \ windowsdevbox-master \ test.ps1:2 字符:113 +添加内容"$ {env:homepath} .proxy \ TempProxy.bat""alias proxyon ='source`" $ HOMEPATH/.proxy/proxy-switch.sh on<<<<' 缺少终止符:.

The string starting: At C:\Chef\windowsdevbox-master\test.ps1:2 char:113 + add-content "${env:homepath}.proxy\TempProxy.bat" "alias proxyon='source `"$HOMEPATH/.proxy/proxy-switch.sh on" <<<< '" is missing the terminator: '.

我也尝试过

add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source `"$HOMEPATH/.proxy/proxy-switch.sh on`"'" 

这不会出错,但是它不会添加$ HOMEPATH的内容,而只会添加以下内容

Which doesn't error but, it doesn't add the contents of $HOMEPATH and only adds the following

alias proxyon='source "/.proxy/proxy-switch.sh on"'

对此的任何建议都将被采纳.

Any suggestions on this would be greatly appriciated.

推荐答案

我认为问题是,您希望输出文件中的文字$homepath是为了让GitBash处理它.有很多尝试逃避和处理此问题的方法,但是可以让您与上一次所做的几乎准备就绪的方法一起使用.

I think the issue is that you want the literal $homepath in your output file in order to let GitBash deal with it. Many ways to try and escape and deal with this but lets work with you last one you made that was almost ready.

add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source `"`$HOMEPATH/.proxy/proxy-switch.sh on`"'" 

唯一不同的是$HOMEPATH前面的反引号.由于PowerShell使用$来声明和引用变量,因此您需要对其进行转义(就像对引号所做的那样),以防止PowerShell对其进行插值.

The only thing that is different here is the backtick in front of $HOMEPATH. Since PowerShell uses the $ to declare and reference variables you need to escape it, like you have done with your quotes, to prevent PowerShell from interpeting it.

由于您没有名为$HOMEPATH的变量,因此该变量将在那时创建,其值为null.这就是为什么上一个示例的输出看起来像以前一样.

Since you do not have a variable called $HOMEPATH it is created at that time with the value of null. That is why your output from your last example looked the way it did.

使用格式运算符

另一种可能更容易出现的选择是使用格式运算符.如果您想将其临时存储在变量中,则可以使用它.

Another option that might have been easier on the eyes is to use the format operator. If you wanted to store it in a variable temporarily you could have this.

$content = "alias proxyon='{0}'" -f 'source "$HOMEPATH/.proxy/proxy-switch.sh on"'
add-content "${env:homepath}\.proxy\TempProxy.bat" $content

否则将是

add-content "${env:homepath}\.proxy\TempProxy.bat" ("alias proxyon='{0}'" -f 'source "$HOMEPATH/.proxy/proxy-switch.sh on"')

这篇关于嵌入Powershell脚本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆