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?
当您运行脚本时,它将在默认操作系统 Powershell 主机中运行,并且在 Windows 上,这将是 Windows PowerShell 的默认操作系统版本,除非您另行明确告知。
\n如果您想使用 PSCore 运行脚本,请输入...
\npwsh [UNC to your script here.]\n
Run Code Online (Sandbox Code Playgroud)\n...您将 VSCode 配置为默认 PS 版本的内容对操作系统将使用的内容没有影响。
\n确保代码仅尝试在 PSCore 中运行的另一项措施是在所有脚本顶部使用 #requires 语句。
\n\n这并不意味着您可以在 PSCore 中自动运行,它会抛出错误,告诉用户必须使用定义的 PS 版本。因此,他们可以通过启动或输入正确的版本来使用正确的版本运行。
\n示例脚本中的代码
\n#Requires -Version 7.0\n"Running $PSVersionTable"\n
Run Code Online (Sandbox Code Playgroud)\n运行脚本
\nPS 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另一种选择是修复代码以检查主机版本和分支到正确的版本。例如,我有一个函数,用于根据我想要使用的版本运行代码块。我将其保存在通过我的个人资料导入的模块中,以便它始终可用。
\nFunction 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因此,要使用操作系统默认值运行代码......
\nStart-ConsoleCommand -ConsoleCommand \'some command string\'\n
Run Code Online (Sandbox Code Playgroud)\n...在 PSCore 中运行代码...
\nStart-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)