如何从命令提示符执行 Powershell Script AS Powershell 7

Bee*_*ems 3 powershell command-line

我有一个脚本需要运行 AS Powershell 7,因为只有 Powershell 7 有我需要的命令。

当我$PSVersionTable从 VSCode运行.PS1 脚本时,我得到:

Name                           Value
----                           -----
PSVersion                      7.0.1
PSEdition                      Core
Run Code Online (Sandbox Code Playgroud)

当我使用从命令提示符运行相同的 .PS1 脚本时powershell testscript_writefile.ps1,我得到:

Name                           Value
----                           -----
PSVersion                      5.1.14393.3471
PSEdition                      Desktop
Run Code Online (Sandbox Code Playgroud)

如何执行 Powershell 脚本AS Powershell 7 而不是 Powershell 5?

pos*_*ote 8

当您运行脚本时,它将在默认操作系统 Powershell 主机中运行,并且在 Windows 上,这将是 Windows PowerShell 的默认操作系统版本,除非您另行明确告知。

\n

如果您想使用 PSCore 运行脚本,请输入...

\n
pwsh [UNC to your script here.]\n
Run Code Online (Sandbox Code Playgroud)\n

...您将 VSCode 配置为默认 PS 版本的内容对操作系统将使用的内容没有影响。

\n

确保代码仅尝试在 PSCore 中运行的另一项措施是在所有脚本顶部使用 #requires 语句。

\n

about_Requires - PowerShell | about_Requires - PowerShell | about_Requires - PowerShell | about_Requires 微软文档

\n

这并不意味着您可以在 PSCore 中自动运行,它会抛出错误,告诉用户必须使用定义的 PS 版本。因此,他们可以通过启动或输入正确的版本来使用正确的版本运行。

\n

示例脚本中的代码

\n
#Requires -Version 7.0\n"Running $PSVersionTable"\n
Run Code Online (Sandbox Code Playgroud)\n

运行脚本

\n
PS C:\\> $PSVersionTable\n<#\n#Results\n                                                                                                \nName                           Value\n----                           -----\nPSVersion                      5.1.18362.752\nPSEdition                      Desktop\nPSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}\nBuildVersion                   10.0.18362.752\nCLRVersion                     4.0.30319.42000\nWSManStackVersion              3.0\nPSRemotingProtocolVersion      2.3\nSerializationVersion           1.1.0.1\n<#\n\nPS C:\\> D:\\Scripts\\TestForHostVersion.ps1   \n<#\n# Results\n                                                                        D:\\Scripts\\TestForHostVersion.ps1 : The script \'TestForHostVersion.ps1\' cannot be run because it contained a\n"#requires" statement for Windows PowerShell 7.0. The version of Windows PowerShell that is required by the script\ndoes not match the currently running version of Windows PowerShell 5.1.18362.752.\nAt line:1 char:1\n+ D:\\Scripts\\TestForHostVersion.ps1\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n    + CategoryInfo          : ResourceUnavailable: (TestForHostVersion.ps1:String) [], ScriptRequiresException\n    + FullyQualifiedErrorId : ScriptRequiresUnmatchedPSVersion\n#>\n\nPS C:\\> pwsh -noprofile   \n<#\n# Results\n          \n\n                                                                                PowerShell 7.0.1\nCopyright (c) Microsoft Corporation. All rights reserved.\n\nhttps://aka.ms/powershell\nType \'help\' to get help.\n#>\n\nPS C:\\> D:\\scripts\\TestForHostVersion.ps1\n<#\n# Results\n\nRunning System.Management.Automation.PSVersionHashTable\n#>\n\nPS C:\\> $PSVersionTable\n<#\n# Results\n\nName                           Value\n----                           -----\nPSVersion                      7.0.1\nPSEdition                      Core\nGitCommitId                    7.0.1\nOS                             Microsoft Windows 10.0.18363\nPlatform                       Win32NT\nPSCompatibleVersions           {1.0, 2.0, 3.0, 4.0\xe2\x80\xa6}\nPSRemotingProtocolVersion      2.3\nSerializationVersion           1.1.0.1\nWSManStackVersion              3.0\n\nPS C:\\> exit\n#>\n
Run Code Online (Sandbox Code Playgroud)\n

另一种选择是修复代码以检查主机版本和分支到正确的版本。例如,我有一个函数,用于根据我想要使用的版本运行代码块。我将其保存在通过我的个人资料导入的模块中,以便它始终可用。

\n
Function Start-ConsoleCommand\n{\n    [CmdletBinding(SupportsShouldProcess)]\n\n    [Alias(\'scc\')]\n\n    Param  \n    ( \n        [string]$ConsoleCommand,\n        [switch]$PoSHCore\n    )\n\n    If ($PoSHCore)\n    {Start-Process pwsh -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -Wait}\n    Else\n    {Start-Process powershell -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -Wait}\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

因此,要使用操作系统默认值运行代码......

\n
Start-ConsoleCommand -ConsoleCommand \'some command string\'\n
Run Code Online (Sandbox Code Playgroud)\n

...在 PSCore 中运行代码...

\n
Start-ConsoleCommand -ConsoleCommand \'some command string\' -PoshCore\n
Run Code Online (Sandbox Code Playgroud)\n

你可以做类似的事情。

\n


小智 6

赶紧跑

pwsh testscript_writefile.ps1
Run Code Online (Sandbox Code Playgroud)

代替

powershell testscript_writefile.ps1
Run Code Online (Sandbox Code Playgroud)

  • 为了使这个绝对正确的答案更好,请解释为什么这是正确答案 (2认同)
  • @SimonS 我尝试了[更详细的编辑](https://superuser.com/review/suggested-edits/1087313),但被拒绝了,所以超级快:检查[Mark Kraus的博客](https://get -powershellblog.blogspot.com/2017/10/why-pwsh-was-chosen-for-powershell-core.html),解释了 `pwsh.exe` 是 v6.0+、.NET Core 和跨平台,而 `powershell. exe` 仅限 .NET Framework 和 Windows。 Kraus 引用了 [来自 Steve Lee 的 GitHub 评论](https://github.com/PowerShell/PowerShell/issues/4214#issuecomment-335989245),PowerShell Mgr:_好处是在 Windows 上明确无歧义。使用_ (2认同)