在 OSX 中,我打开 bash 终端并进入 PowerShell 控制台。在我的 PowerShell 脚本中,我想打开另一个 PowerShell 控制台并在那里执行 PowerShell 脚本。
在Windows下,我会做
Invoke-Expression ('cmd /c start powershell -Command test.ps1')
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 OSX 中做同样的事情?
PowerShell 7 引入了一个非常需要的功能来并行运行管道输入。
PowerShell 7的文档没有提供有关如何实现的任何详细信息。
已经利用PoshRSJob与Invoke-Parallel之前的模块,我知道,运行空间在传统上认为是在运行PowerShell的工作在PowerShell中并行操作的更有效的方法。我已经阅读了一些混合内容,表明这是现在使用线程而不是运行空间,但找不到任何其他特定内容。
我真的很感激对以下方面的一些技术见解:
Powershell 6具有Unix风格/etc/issue,提到了文档的链接.
PowerShell v6.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
Run Code Online (Sandbox Code Playgroud)
这很好,但是:
如何删除部分或全部消息?IIRC Powershell 5仍然有版权信息,所以也许我不能删除它,但摆脱最后3行会有帮助吗?
标准用户有几个选项可以以管理员(或任何其他用户)身份运行,但是,即使以管理员身份登录,某些功能也需要“提升”运行。
在 Windows gui 上,只需右键单击 a.exe并选择run as Administrator甚至提升“cmd”或“powershell”。
如何在 Windows 核心上获得提升的权限?
随着 PowerShell Core 的发布,应用程序在使用托管自动化库 (system.management.automation) 时如何选择调用哪个版本的 Powershell(Powershell 5.x 或 PowerShell Core)?关于应该创建的运行空间的一些事情?或者也许是连接信息?
这个自我回答的问题侧重于 Windows [1],解决了以下几个方面:
现在有两个PowerShell 版本- 传统的、仅限Windows 的 Windows PowerShell和跨平台的 PowerShell Core,两者都可以安装在给定的 Windows 机器上:
如何判断哪个 PowerShell 版本将执行远程命令,例如 via Invoke-Command -ComputerName?
我如何通过配置针对特定版本,包括临时的和持久的?
笔记:
对于要在给定机器上通过远程处理可定位的版本,必须将其设置为远程处理:
只有Windows PowerShell中被自动设置为远程处理,但只能在服务器上运行Windows Server 2012或更高版本。
从 v7 开始,PowerShell Core还没有随 Windows 一起提供;如果您使用的是官方安装程序,则可以选择在安装过程中启用远程处理。
在任何情况下,您都可以根据Enable-PSRemoting需要(重新)启用 PowerShell 远程处理,其中:
必须从相应的版本运行。
必须以管理权限运行
[1] 也就是说,问题集中在基于WinRM的远程处理上(WinRM 是 DTMF …
此命令打开一个新的 powershell 窗口,运行命令,然后退出:
Start-Process powershell { echo "hello"; sleep 1; echo "two"; sleep 1; echo "goodbye" }
Run Code Online (Sandbox Code Playgroud)
如果我改为启动 Powershell Core,它会打开一个新窗口,但新窗口会立即退出:
Start-Process pwsh { echo "hello"; sleep 1; echo "two"; sleep 1; echo "goodbye" }
Run Code Online (Sandbox Code Playgroud)
使用 pwsh 进行此类调用的正确方法是什么?
我有以下代码:
$html = New-Object -ComObject "HTMLFile"
$source = Get-Content -Path $FilePath -Raw
try
{
$html.IHTMLDocument2_write($source) 2> $null
}
catch
{
$encoded = [Text.Encoding]::Unicode.GetBytes($source)
$html.write($encoded)
}
$t = $html.getElementsByTagName("table") | Where-Object {
$cells = $_.tBodies[0].rows[0].cells
$cells[0].innerText -eq "Name" -and
$cells[1].innerText -eq "Description" -and
$cells[2].innerText -eq "Default Value" -and
$cells[3].innerText -eq "Release"
}
Run Code Online (Sandbox Code Playgroud)
该代码在 Windows Powershell 5.1 上运行良好,但在 Powershell Core 7 上$_.tBodies[0].rows返回 null。
那么,如何在 PS 7 中访问 HTML 表格的行?
我在 WindowsPowerShell 和 PowerShell 中都有相同的 profile.ps1。它包括调用 Windows Text-To-Speech 的命令,但是,这些命令在 PowerShell 7 中运行时会失败。
当我尝试使用我使用以下代码创建的 $PomrptTTS 对象时会发生错误:
Add-Type -AssemblyName System.speech
$PromptTTS = New-Object System.Speech.Synthesis.SpeechSynthesizer
Run Code Online (Sandbox Code Playgroud)
在 PowerShell 7 中,任何访问或使用我的 $PormptTTS 对象的尝试都会产生以下结果:
SetValueInvocationException: ....\profile.ps1:82
Line |
82 | $PromptTTS.Rate = 0 ; $PromptTTS.Speak("Time for the $((Get-Date).DayofWeek) shuffle")
| ~~~~~~~~~~~~~~~~~~~
| Exception setting "Rate": "Object reference not set to an instance of an object."
MethodInvocationException: ....\profile.ps1:82
Line |
82 | … e = 0 ; $PromptTTS.Speak("Time for the $((Get-Date).DayofWeek) shuffle")
| ~~~~~~~~~~~~~~~~~~~~
| Exception calling …Run Code Online (Sandbox Code Playgroud) 最近我发现了一些关于在 Powershell 中将一些类似版本的值分配给变量的令人讨厌的行为(至少在 7.2.5 中)。
起初我尝试过:
> $version = 1.2.3
> echo $version
Run Code Online (Sandbox Code Playgroud)
我很快发现我必须用引号转义 thr 值才能显式地使其成为字符串。这个效果很好:
> $version = "1.2.3"
> echo $version
1.2.3
Run Code Online (Sandbox Code Playgroud)
问题是:为什么会这样?为什么pwsh在第一个示例中不抛出某种错误而只是将其转换为$null?就像其他情况一样:
> 12a
12a: The term '12a' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
> [int]12a
ParserError:
Line |
1 | …Run Code Online (Sandbox Code Playgroud) powershell ×10
powershell-core ×10
windows ×2
cmd ×1
html ×1
macos ×1
runspace ×1
terminal ×1
winrm ×1
wsman ×1