gpt4 book ai didi

How can i run a command in Powershell when setting variables?(如何在PowerShell中运行设置变量时的命令?)

转载 作者:bug小助手 更新时间:2023-10-24 19:51:06 26 4
gpt4 key购买 nike



I am trying to run the following in powershell:

我正尝试在PowerShell中运行以下命令:


   $str1="c:\windows\system32\
$str2 = "notepad.exe"
$str3 = $str1$str2
&$str3


It doesnt recognize $str3. I want to set the variable and then run the command. I need $str2 as this is set via an app and i dont know what it may be , but it will always be a .exe .How can i get this to run ?

它不能识别$str3。我想设置变量,然后运行命令。我需要$str2,因为这是通过应用程序设置的,我不知道它可能是什么,但它永远是一个.exe。我如何才能让它运行?


更多回答

Use proper string concatenation. Either quote the 2 variables so it can join them as one: $str3 = "$str1$str2", or use + operator to join them together: $str3 = $str1 + $str2. You can use other methods but these are the most common

使用正确的字符串连接。或者引用这两个变量,以便它可以将它们连接为一个:$str3=“$str1$str2”,或者使用+运算符将它们连接在一起:$str3=$str1+$str2。您可以使用其他方法,但这些方法是最常见的

Ok , but then how can i run the command in the powershell window after setting. So i will set them and the run them ?

好的,但设置后如何在PowerShell窗口中运行该命令?那么我会把它们设置好并运行它们吗?

you're doing it already with & $str3.

您已经使用&$str3完成了该操作。

Now what if my pathe was something like C:\Program Files\My App , how can i put that in a string , is it double quotes ?

现在,如果我的路径类似于C:\Program Files\My App,我如何将其放在字符串中,它是双引号吗?

Would it hurt you when you just tried? Your questions are quite basic. You may use your prefered internet search engine to look for answers. That will be even faster than waiting here for answers. ¯\_(ツ)_/¯

当你刚刚尝试的时候,会不会伤害到你?你的问题很基本。你可以使用你喜欢的互联网搜索引擎来寻找答案。这将比在这里等待答案更快。\_(ツ)_/

优秀答案推荐

Look like you are missing the quote at the end of line one and when you join two variables together you have to either use + to concatenate them or put them in quotes:

看起来您缺少第一行末尾的引号,当您将两个变量连接在一起时,您必须使用+将它们连接在一起,或者将它们放在引号中:


$str1 + $str2

or


"$str1$str2"

But when joining two variables with path and/or filenames you want to use Join-Path. This way the slash at the end of the path does not cause issues if it happens to be missing:

但是,当使用路径和/或文件名连接两个变量时,您希望使用Join-Path。这样,如果路径末尾的斜杠恰好丢失,也不会造成问题:


$str1 = "c:\windows\system32\"
$str2 = "notepad.exe"
$str3 = Join-Path $str1 $str2
& $str3

The above will work correctly even if $str1 is missing the slash at the end.

即使$str1末尾没有斜杠,上面的操作也会正常工作。


更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com