我最近给自己买了一台新的 Windows 11 笔记本电脑,并开始探索 powershell。以前从未在 Windows 上使用过它,但我是 Mac 的频繁用户,zsh所以并不是全新的。
我将 Powershell 升级到7.2.1,但注意到我习惯使用 Oh My Zsh 的自动建议类型功能丢失了。经过一番搜索后,我安装了PSReadLine,并使用以下命令设置了我的个人资料:
Import-Module PSReadLine\n\nSet-PSReadLineOption -PredictionSource History\n\nSet-PSReadLineOption -HistorySearchCursorMovesToEnd\nSet-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward\nSet-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward\n\nSet-PSReadLineKeyHandler -Key Tab -Function Complete\nRun Code Online (Sandbox Code Playgroud)\n这比以前好多了,但我注意到我不能部分接受自动完成。
\n例如,这是我的历史的一部分:
\nNew-item \xe2\x80\x93type file \xe2\x80\x93force $profile\nRun Code Online (Sandbox Code Playgroud)\n如果我现在尝试创建一个文件test-file,然后开始输入New-Item,我确实得到了建议。我想接受直到 结束-force,或者可能直到file。无论我尝试什么组合键,这种情况都不会发生(徒劳地尝试了Ctrl+ \xe2\x86\x92、Shift+ \xe2\x86\x92、Alt+ \xe2\x86\x92、Fn+ )。\xe2\x86\x92
当然,我可以接受完整的建议并删除我不需要的内容。虽然对于这个例子来说,这确实无关紧要,但对于仅在初始部分出现匹配的长命令来说,这会很烦人。所以,我的问题是部分接受建议的组合是什么,或者如果需要单独启用,我该怎么做? …
我有一个递归函数,执行了大约 750 次 - 迭代 XML 文件并进行处理。代码正在运行使用Start-Job
下面的例子:
$job = Start-Job -ScriptBlock {
function Test-Function {
Param
(
$count
)
Write-Host "Count is: $count"
$count++
Test-Function -count $count
}
Test-Function -count 1
}
Run Code Online (Sandbox Code Playgroud)
输出:
$job | Receive-Job
Count is: 224
Count is: 225
Count is: 226
Count is: 227
The script failed due to call depth overflow.
Run Code Online (Sandbox Code Playgroud)
在我的机器上,深度溢出始终发生在 227。如果我删除Start-Job,我可以达到 750~(甚至更高)。我正在使用作业进行批处理。
使用时有没有办法配置深度溢出值Start-Job?
这是 PowerShell 作业的限制吗?
希望有人能指出这一点,真的很烦人。
\n我试图在 Powershell 7.1.3 中使用 Markdig 查找 MarkdownDocument 中的所有链接
\n它是为 .NET 设计的,具有一组继承类,用于表示各种类型的 Markdown 文档。我正在使用一个简单的辅助 PS 包装器,它有助于使用通用方法,但我不认为这是一个原因。
\n让我用一个简单的重现向您展示
\ninstall-module psmarkdig # provides the Get-MdElement helper\n$doc = [Markdig.Parsers.MarkdownParser]::Parse('# blah`n`nHello [this is a link](linkpath.md)')\n$links = @(Get-MdElement $doc 'Markdig.Syntax.Inlines.LinkInline') # force even a single match to be an array\nRun Code Online (Sandbox Code Playgroud)\n好的,所以这包含一个链接
\n> $links.Count\n1\nRun Code Online (Sandbox Code Playgroud)\n并且元素是我们期望的类型
\n> $links[0].GetType()\nIsPublic IsSerial Name BaseType\n-------- -------- ---- --------\nTrue False LinkInline Markdig.Syntax.Inlines.ContainerInline\nRun Code Online (Sandbox Code Playgroud)\n我可以看到有一个带有 Get-Member 的 IsImage 属性
\n> Get-Member -InputObject $links[0]\n TypeName: …Run Code Online (Sandbox Code Playgroud) 最近切换到新的 Windows 终端,经过几个小时的互联网搜索后,我找不到任何有用的东西,我想要的就是在新的 Windows 终端中设置 cmd 以显示 git 分支,就像 powershell 可以实现的那样。
我对 cmd 非常满意,尤其是它能够使用额外的 linux 命令,并且不想仅仅因为 git 分支的良好显示而切换到 powershell。这是一个源代码,其中对 powershell 的所有内容都有很好的解释,我只想对 CMD 做同样的事情。
提前致谢
在 PowerShell 中,我想编写一个函数,它接受不同的选项作为参数。如果它接收多个参数是可以的,但它必须至少接收一个参数。我想通过参数定义来强制执行它,而不是通过之后的代码。我可以使用以下代码让它工作:
function Set-Option {
Param(
[Parameter(Mandatory, ParameterSetName="AtLeastOption1")]
[Parameter(Mandatory=$false, ParameterSetName="AtLeastOption2")]
[Parameter(Mandatory=$false, ParameterSetName="AtLeastOption3")]
$Option1,
[Parameter(Mandatory=$false, ParameterSetName="AtLeastOption1")]
[Parameter(Mandatory, ParameterSetName="AtLeastOption2")]
[Parameter(Mandatory=$false, ParameterSetName="AtLeastOption3")]
$Option2,
[Parameter(Mandatory=$false, ParameterSetName="AtLeastOption1")]
[Parameter(Mandatory=$false, ParameterSetName="AtLeastOption2")]
[Parameter(Mandatory, ParameterSetName="AtLeastOption3")]
$Option3
)
# Do stuff, but don't evaluate the plausibility of the given parameters here
}
Run Code Online (Sandbox Code Playgroud)
但正如您所看到的,它的扩展性很差。对于每个附加选项,我必须向所有其他选项添加一行。能否以更高效且更可维护的方式来完成此操作?
正如我已经说过的,我不想检查代码中的参数,例如通过评估$PSBoundParameters. 出于自动文档的原因,我希望它发生在参数定义中。
如果您需要一个真实世界的示例,请查看Set-DhcpServerv4OptionValue它接受许多不同的选项(-DnsDomain、-DnsServer、-Router、 ...),其中可以包含所有选项,但没有任何选项是没有意义的。
注意:在提供了几个答案之后,我刚刚意识到,如果您提供多个选项,我的代码实际上不起作用。
我正在尝试绘制蓝牙耳机电池放电图表。为此,我需要读取所连接设备的电池百分比。我可以在设备的 GUI 上看到电源信息。有没有办法使用 PowerShell 获取连接的蓝牙设备的电池百分比信息?(比如使用 wmi 或其他任何东西)
我正在尝试部署支持 Heroku 的 Nestjs,每当它运行命令时prebuild: rimraf dist都会package.json遇到此错误。我不知道 glob 是什么,但我假设我不需要它......disableGlob = true在 cli/scripts 中设置的语法是什么?
当前错误:
/tmp/build_ceb6e44a/node_modules/rimraf/rimraf.js:42
throw Error('glob dependency not found, set `options.disableGlob = true` if intentional')
^
Error: glob dependency not found, set `options.disableGlob = true` if intentional
at defaults (/tmp/build_ceb6e44a/node_modules/rimraf/rimraf.js:42:11)
at rimraf (/tmp/build_ceb6e44a/node_modules/rimraf/rimraf.js:60:3)
at go (/tmp/build_ceb6e44a/node_modules/rimraf/bin.js:44:3)
at Object.<anonymous> (/tmp/build_ceb6e44a/node_modules/rimraf/bin.js:68:3)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
Node.js v17.9.0
Run Code Online (Sandbox Code Playgroud)
注意:Heroku 正在node 17.9.0 …
我有一个 XML 文件,我正在尝试使用 PowerShell 读取该文件。但是,当我阅读它时,某些 XML 对象的输出中包含以下字符: \xc3\xa2\xe2\x82\xac\xe2\x80\xb9
\n我只是从第三方下载了所需的 XML 文件,该文件在 Excel 中打开。然后我抓取所需的列并将它们粘贴到新的 Excel 工作簿中。然后,我使用 XML 架构映射字段,然后将其导出为 XML 文件,然后将其用于编写脚本。
\n在 Excel 电子表格中,我的数据看起来很干净,但当我导出它并运行 PS 脚本时,这些奇怪的字符出现在输出中。这些字符甚至在导出后出现在实际的 XML 文件中。我究竟做错了什么?
\n我尝试使用-Encoding UTF8,但我对 PowerShell 比较陌生,并且不确定如何将其正确应用到我的脚本中。感谢任何帮助!
电源外壳
\n$xmlpath = 'Path\\To\\The\\File.xml'\n\n[xml]$xmldata = (Get-Content $xmlpath)\n\n$xmldata.applications.application.name\nRun Code Online (Sandbox Code Playgroud)\n输出示例
\n\xc3\xa2\xe2\x82\xac\xe2\x80\xb9ABC_DEF_GHI\xc3\xa2\xe2\x82\xac\xe2\x80\xb9.com\xc3\xa2\xe2\x82\xac\xe2\x80\xb9\xc3\xa2\xe2\x82\xac\xe2\x80\xb9\n\xc3\xa2\xe2\x82\xac\xe2\x80\xb9JKL_MNO_PQRS\xc3\xa2\xe2\x82\xac\xe2\x80\xb9.com\xc3\xa2\xe2\x82\xac\xe2\x80\xb9\nTUV_WXY_Z.com\nAB_CD_EF_GH\xc3\xa2\xe2\x82\xac\xe2\x80\xb9.com\nRun Code Online (Sandbox Code Playgroud)\n 当我尝试运行时:
mv test.txt test2.txt test-files
Run Code Online (Sandbox Code Playgroud)
返回错误:
Move-Item : A positional parameter cannot be found that accepts argument '.\test-files\'.
At line:1 char:1
+ mv .\test.txt .\test2.txt .\test-files\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Move-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.MoveItemCommand
Run Code Online (Sandbox Code Playgroud)
但是,如果我只是一次移动一个文件
mv test.txt test-files
mv test2.txt test-files
Run Code Online (Sandbox Code Playgroud)
然后它会移动文件,但如果我有两个以上的文件,这似乎很乏味。
有没有可能的方法将多个文件一次移动到特定位置?
我尝试过 mv t*.txt test-files ,但我想要实际移动的文件是index.html和style.css
我正在编写一个将从 AzDo Pipeline 运行以禁用 F5 WebServer 的脚本。下面的脚本在可视代码中运行良好,并且确实按预期禁用了服务器。但是当从终端或 PS 窗口运行时失败并出现以下错误。有人可以帮忙吗?
\n$ServerInput = 'server1.abc.com'\n$BIGIPBaseURL = "https://ser-f5-1.prod.abc.com"\n$usr = "nilesh"\n$SecurePassword='P@assword'\nWrite-Host "Starting the Script..." \n# Initialize variables\n[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12\n$BIGIPToken = $null\nWrite-Host -ForegroundColor Green " done!"\n$DisableWebServers = $true\n# Initialize functions\nWrite-Host "Initializing functions..." -NoNewline\n$PSVersionTable\n\nfunction Disable-BIGIPNode([string]$NodeName) {\n # servers should use the Disable-BIGIPTelcoNode() function\n Write-Host "In the Disable function"\n if ($NodeName -match "(?i).*telco.*") {\n Write-Host -ForegroundColor Yellow "WARNING: `"$($NodeName.ToUpper().Split('.')[0])`" is in the wrong list. telcoo hosts should be added to the TelcoServers list in your input file."\n …Run Code Online (Sandbox Code Playgroud) powershell ×10
windows ×3
automation ×1
azure-devops ×1
battery ×1
bluetooth ×1
cmd ×1
excel ×1
f5 ×1
git-branch ×1
glob ×1
heroku ×1
nestjs ×1
node.js ×1
parameters ×1
psreadline ×1
windows-11 ×1
wmi ×1
xml ×1