简体   繁体   English

批处理脚本:将返回的 GUID 作为桌面快捷方式的 URL 参数传递

[英]Batch script: Pass returned GUID as URL parameter for desktop shortcut

I'm trying to create desktop shortcuts to a private page we work with that will open in Edge, direct to a specific URL, and pass the GUID as a URL parameter.我正在尝试为我们使用的私有页面创建桌面快捷方式,该页面将在 Edge 中打开,指向特定的 URL,并将 GUID 作为 URL 参数传递。

I've tried the following but as you can expect, only the string "powershell" is passed on to the URL, not the returned GUID.我尝试了以下方法,但正如您所料,只有字符串“powershell”被传递给 URL,而不是返回的 GUID。

SET a=powershell -Command "[guid]::NewGuid().ToString()"
C:\Windows\System32\cmd.exe /c start msedge "https://www.website.com/page?user="%a% --no-first-run

How can I replace the %a% portion of the URL with the returned contents of the system GUID?如何将 URL 的 %a% 部分替换为系统 GUID 的返回内容?

powershell -Command "[guid]::NewGuid().ToString()"

Batch files (executed by cmd.exe ) have no concept of a what is known as command substitution in POSIX-compatible shells (a feature that PowerShell itself provides too, though it has no official name there): the ability to assign a command's output to a variable .批处理文件(由cmd.exe执行)在与 POSIX 兼容的 shell 中没有所谓的命令替换的概念(PowerShell 本身也提供了这一功能,尽管它在那里没有正式名称):分配命令的能力Z738E6221F398663到一个变量

Instead, you must use a for /f loop to capture command output in a variable (which generally loops over each output line , but in your case there is only one output line):相反,您必须使用for /f循环在变量中捕获命令 output (通常循环每个 output 行,但在您的情况下只有一个Z78E6221F6393D1356681DB398F4CE6DZ 行):

@echo off & setlocal

:: Capture the output from a PowerShell command in variable %guid%, via 
:: a for /f loop:
for /f "usebackq delims=" %%a in (`powershell -Command "[guid]::NewGuid().ToString()"`) do set "guid=%%a"

:: Note: No need for `cmd /c` from a batch file to use `start`
start "" msedge "https://www.website.com/page?user=%guid%" --no-first-run
  • Run for /? for /? in a cmd.exe session for help.cmd.exe session 中寻求帮助。

  • This answer discusses using for /f to capture command output in more detail;这个答案更详细地讨论了使用for /f来捕获命令 output ; notably:尤其:

    • usebackq isn't strictly necessary here, but is generally advisable to give you the freedom to use both ' and " quoting in the command line being invoked. usebackq在这里并不是绝对必要的,但通常建议您可以自由地在被调用的命令行中同时使用'"引用。

    • Similarly, delims= isn't strictly necessary here, since the output by definition contains no spaces , but it is generally advisable if the intent is to capture an output line in full .同样, delims=在这里不是绝对必要的,因为 output 根据定义不包含空格,但如果意图是完整地捕获 output 行,通常是可取的。

  • The "" as the first start argument isn't strictly necessary here, but in general it is useful when invoking applications whose paths must be double-quoted.作为第一个start参数的""在这里并不是绝对必要的,但通常在调用路径必须双引号的应用程序时它很有用。 Without "" as the first argument, a double-quoted application path would be interpreted as starts window-title argument (which only meaningfully applies to console applications).如果没有""作为第一个参数,双引号应用程序路径将被解释为starts窗口标题参数(仅有意义地适用于控制台应用程序)。

It is possible to do all of this directly using a PowerShell one-liner:可以使用 PowerShell 单线直接完成所有这些操作:

powershell -noprofile -command start msedge \"https://www.website.com/page?user=$(New-Guid) --no-first-run\"
  • Passing -noprofile to powershell.exe is most of the time a good idea to reduce startup time and provide a more predictable environment as no user profile will be loaded.-noprofile传递给powershell.exe在大多数情况下是减少启动时间并提供更可预测的环境的好主意,因为不会加载任何用户配置文件。
  • start is an alias for the Start-Process command. startStart-Process命令的别名。
  • Here start gets passed two positional arguments, the name of the process to start ( -FilePath parameter) and the process's arguments as a single string ( -ArgumentList parameter).这里start传递了两个位置 arguments,要启动的进程的名称( -FilePath参数)和进程的 arguments 作为单个字符串( -ArgumentList参数)。 Therefore, the 2nd argument must be quoted.因此,必须引用第二个参数。 To pass the quotes from the command processor cmd.exe through to PowerShell, they must be backslash-escaped.要将命令处理器cmd.exe中的引号传递到 PowerShell,它们必须进行反斜杠转义。
  • Within the process's parameter string, the subexpression operator $(…) is used to call the New-Guid command inline and convert it to a string (by implicitly calling the .ToString() method of the Guid object it returns).在进程的参数字符串中, 子表达式运算符$(…)用于内联调用New-Guid命令并将其转换为字符串(通过隐式调用它返回的Guid object 的.ToString()方法)。
  • If you actually need to use the GUID as a variable in other parts of your batch script (which is not clear from the question), then this helpful answer provides a solution.如果您确实需要在批处理脚本的其他部分中使用 GUID 作为变量(从问题中不清楚),那么这个有用的答案提供了一个解决方案。

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

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