是否可以在 PowerShell 中创建匿名递归函数?(如果是,怎么做?)
我有一个递归对象并使用递归函数向下钻取属性,例如:
$Object = ConvertFrom-Json '
{
"Name" : "Level1",
"Folder" : {
"Name" : "Level2",
"Folder" : {
Name : "Level3"
}
}
}'
Function GetPath($Object) {
$Object.Name
if ($Object.Folder) { GetPath $Object.Folder }
}
(GetPath($Object)) -Join '\'
Level1\Level2\Level3
Run Code Online (Sandbox Code Playgroud)
该函数相对较小,并且只有必需的函数,因此我想直接将其作为匿名函数调用,例如:
(&{
$Object.Name
if ($Object.Folder) { ???? $Object.Folder }
}) -Join '\'
Run Code Online (Sandbox Code Playgroud)
这在 PowerShell 中可能吗?如果是,我如何(尽可能干净地)引用当前函数?????
因此,我使用 PowerShell(特别是版本 5.1)为我的工作编写一个脚本,该脚本基本上根据用户从可用选项中选择的内容打开一些网站。
为了打开网站(对于本例,假设我要去 Google),我使用 [system.Diagnostics.Process]::Start("chrome","https://www.google. com”)。这确实会打开该网站,但我正在尝试将其完全打开到一个新窗口中。在当前状态下,我只能通过手动打开一个新窗口然后运行脚本来完成此操作。
关于任何开关或任何可能有效的东西有什么建议吗?我在其他网站上看到过一些关于 --new-window 或 -new = 1 的内容,但这些是用于其他命令的,似乎不适用于此命令。
我还专门使用 Chrome,因为我尝试打开的网站需要登录,所以我已经打开了一个 Chrome 窗口,并且它默认在同一窗口上打开我的所有新选项卡,这就是我的操作我试图避免。
任何帮助将不胜感激!
我正在做一个研究项目,涉及在 Anaconda 命令提示符中运行终端。我能够安装并使用当前版本的 Anaconda (4.12) 并在其中运行命令提示符,但我必须将我的 Anaconda 版本降级到 4.2,以便我可以使用 Python 3.5 处理我的项目。该版本的 Anaconda 无法在我的计算机上打开 Anaconda 命令提示符或 Anaconda Navigator,因此我尝试在卸载 4.2 后重新安装当前版本的 Anaconda;但是,现在我遇到了 Anaconda 命令提示符无法打开的问题(但我已经能够运行 Anaconda Navigator)。我尝试执行以下操作来查看是否可以解决此问题:
我不知道该怎么做才能解决这个问题。我已经尝试过从 Windows 和其他论坛在线阅读的许多内容,但到目前为止它们还没有起作用。
我正在编写一个 powershell 脚本来启用某些 Windows 功能。它看起来像这样:
...
Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerRole"
Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServer"
Enable-WindowsOptionalFeature -Online -FeatureName "IIS-FTPServer"
...
Run Code Online (Sandbox Code Playgroud)
在此脚本中,有多个命令可用于启用需要重新启动的 Windows 功能。我注意到在 PowerShell 中,它正在启用某些功能,然后在我启用需要重新启动的功能的最后 4 行中,它不断提示我在 powershell 中重新启动计算机。所以我必须不断地说“不”,否则它会在执行脚本中的所有命令之前重新启动计算机:
在我的脚本中,如何等到所有命令都执行完毕,然后才出现有关重新启动电脑的提示?我尝试在命令中添加“Wait-Process”和“-Wait”标签,但出现如下错误:

有谁知道我可以等待所有命令执行然后才出现重新启动提示的方法吗?
使用 Powershell,System.Management.Automation.Cmdlet.Invoke() 返回类型的对象'<Invoke>d__40'而不是指定的 OutputType。
重现:
powershell -NoProfileAdd-Type -Path .\ExampleCmdlet.cs$command = [SendGreeting.SendGreetingCommand]::new()$command.Name = 'Person'$invoke = $command.Invoke()$invoke.GetType()预期:[string]
实际:[<Invoke>d__40]
$PS版本表:
Name Value
---- -----
PSVersion 5.1.19041.1237
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1237
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Run Code Online (Sandbox Code Playgroud)
Get-Member -InputObject $invoke揭示这个来实现IEnumerator和一些玩弄.MoveNext(),.Current有时会输出预期的"Hello Person!"结果。
这是什么<Invoke>d__40类型?
为什么$command.Invoke()不直接返回预期的字符串输出?
例如
$people = @(
@('adam', '24', 'M')
@('andy', '20', 'M')
@('alex', '30', 'M')
@('ava', '25', 'F')
)
foreach($person in $people | where ($person[1] -lt '25') -and ($person[2] -eq 'M'))
Run Code Online (Sandbox Code Playgroud)
这应该选择adam并andy...
我想在 Powershell 中使用这个使用 gis 标志的正则表达式,
https://regex101.com/r/yoM4cV/1
/(?<=#start)(.+)(?=#end)/gis
Run Code Online (Sandbox Code Playgroud)
匹配 #start 和 #end 之间的句子
#start
hello powershell regex
what is the equivalent of flags gis
#end
Run Code Online (Sandbox Code Playgroud)
我找不到任何有关 Powershell 的 gis 标志支持的信息。Powershell 中缺少这个吗?如果是,有什么替代方案?
更新:我问是因为这返回 false
$test=@'
#start
hello powershell regex
what is the equivalent of flags gis
#end
'@
$test -match (?<=#start)(.+)(?=#end)
Run Code Online (Sandbox Code Playgroud) 我查看了 VSCode 设置,我无法清楚地看到“集成”是什么意思,这就是我所拥有的,但它加载了外部 powershell 配置文件。我想阻止它这样做,可以吗?
PS:我没有也不想要 powershell 扩展,我应该有这个选项吗?
更新:按照建议,我在用户和工作空间设置中都取消选中了“powershell.enableProfileLoading”,重新启动了 vscode,但在终端中仍然出现“加载个人和系统配置文件”,奇怪的是它仍然对我不起作用?
这可能是 VSCode 设置错误吗?
function Main {
$result1 = DoWork1
$result1.GetType()
$result2 = DoWork2
$result2.GetType()
}
function DoWork1 {
$result1 = Invoke-Sqlcmd -Query "select top 1 * from customer" -ServerInstance "(localdb)\MSSQLLocalDB" -Database "Database1" -OutputAs DataTables
#assign to variable then return
return $result1
}
function DoWork2 {
#return results without assigning to variable
return Invoke-Sqlcmd -Query "select top 1 * from customer" -ServerInstance "(localdb)\MSSQLLocalDB" -Database "Database1" -OutputAs DataTables
}
Main
Run Code Online (Sandbox Code Playgroud)
这是意外的输出:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False DataRow System.Object
True True …Run Code Online (Sandbox Code Playgroud) 我有这个 JSON:
'{
"fibo": {
"2": { "hi": "there" },
"8": { "and": "you?" }
"5": { "well": "doing fine" },
"3": { "what": "is hap?" },
}
}'
Run Code Online (Sandbox Code Playgroud)
例如,我想以{ "what": "is hap?" }最简单的方式提取最后一个元素(对象)(最好是带管道的单行,而不用foreach-ing 它)。
我试过:
'{
"fibo": {
"2": { "hi": "there" },
"8": { "and": "you?" }
"5": { "well": "doing fine" },
"3": { "what": "is hap?" },
}
}' | ConvertFrom-Json | select { $_.fibo } | select -Last 1
Run Code Online (Sandbox Code Playgroud)
但select …
powershell ×10
anaconda ×1
c# ×1
command-line ×1
function ×1
python ×1
recursion ×1
regex ×1
windows ×1