AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-21370869

Ralf_Reddings's questions

Martin Hope
Ralf_Reddings
Asked: 2025-04-25 21:20:04 +0800 CST

如果 <key> 已被按住,则阻止触发 <modifier> 和 <key> 热键?

  • 5

我遇到了这个问题,但一直无法优雅地解决。我提供了以下代码作为可复现的示例

如果我触发shift“+”键f23,然后松开shift但仍按住“+” f23,如果我按下“+” control,就会触发。我没想到会出现这种情况,而且我也不想要,如果“+”键已经按下,我肯定不想触发。controlf23controlf23f23

<^f23::
tooltip ctrl and f23 down
KeyWait, f23
tooltip ctrl and f23 up
return

<+f23::
tooltip shift and f23 down
KeyWait, f23
tooltip shift and f23 up
return

我有很多像上述这样的热键(<^f1... <^f22,等等),所以我正在寻找一种适合所有情况的解决方案。

autohotkey
  • 1 个回答
  • 55 Views
Martin Hope
Ralf_Reddings
Asked: 2025-04-22 03:33:57 +0800 CST

“using:” 和其他方法无法与 start-threadJob 一起使用,将对象加载到作业会话中

  • 7

运行以下命令将返回错误:

$job=Start-ThreadJob -name maya6 -InitializationScript {. $using:profile} -ScriptBlock {ichild}   #this is an alias defined in the profile

错误:

InvalidOperation: A Using variable cannot be retrieved. A Using variable can be used only with Invoke-Command, Start-Job, or InlineScript in the script workflow. When it is used with Invoke-Command, the Using variable is valid only if the script block is invoked on a remote computer.
ichild: The term 'ichild' 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.

我也尝试过:

$job=start-threadJob {. $args ; ichild} -ArgumentList $profile      #ichild is an alias defined in my profile

当我使用receive-job $job它时,我的提示冻结了,并且我不断收到以下错误:

Oops, something went wrong.  
Please report this bug with ALL the details below, including both the 'Environment' and 'Exception' sections.  
Please report on GitHub: https://github.com/PowerShell/PSReadLine/issues/new?template=Bug_Report.yaml  
Thank you!  
  
### Environment  
PSReadLine: 2.3.4  
PowerShell: 7.4.6  
OS: Microsoft Windows 10.0.26100  
BufferWidth: 170  
BufferHeight: 21  
  
Last 49 Keys:

我以为using是专门针对这个命令行程序的……

我在 pwsh7.4

powershell
  • 1 个回答
  • 44 Views
Martin Hope
Ralf_Reddings
Asked: 2025-03-29 08:35:25 +0800 CST

ps1 脚本与任务计划程序的执行不一致

  • 6

假设我有一个myScript.ps1文件,有时需要运行本机命令/二进制文件。其内容是:

set-content -path "c:\temp\test.text" -value "hello world"
. 'C:\temp\myCliTool.exe'

如果我手动在任务计划程序中创建一个任务并将“操作”选项卡设置为

  • 程序/文件"C:\Program Files\PowerShell\7\pwsh.exe"
  • 论据-NoProfile -ExecutionPolicy Bypass -command "& {. 'C:\temp\myScript.ps1'}"

ps1 脚本运行良好,test.txt文件已创建。此外,它执行任务所需的本机命令也运行

但是,如果我再次通过任务计划程序但在“操作”选项卡中运行相同的脚本,请稍作更改:

  • 程序/文件"C:\Program Files\PowerShell\7\pwsh.exe"
  • 论据-NoProfile -ExecutionPolicy Bypass -file 'C:\temp\myScript.ps1'

脚本似乎没有运行。test.txt文件未创建。此外,本机命令也不会运行。

如果我尝试通过其他方式(例如 cmd)运行 pwsh,则不会出现此问题。

我认为任务调度程序在这里有问题。我花了一整天时间修复它的“功能”,例如 Path Env 变量在任务调度程序调用下不可用。试图找出 pwsh-file调用的问题已无济于事,我尝试将 PowerShell 脚本中可能发生的潜在错误重定向到文本文件,但我无法完全弄清楚。

我在使用 pwsh 7.4 和 Windows 11

powershell
  • 1 个回答
  • 69 Views
Martin Hope
Ralf_Reddings
Asked: 2025-02-09 08:30:52 +0800 CST

如何获取可变数组的字符串?

  • 5

这件事一直困扰着我,几周前我想出了一个解决办法,但我忘了:

$l=[System.Collections.Generic.List[string[]]]::new()
$l.Add("one", "two", "three")
$l.ToString()                                                  #returns ---> System.String[]
"$l"                                                           #returns ---> System.String[]           
 [string]$l                                                  #returns ---> System.String[]       
 $l -join "`n"                                              #returns ---> System.String[]       

我期望类似以下内容或由$ofs变量指定的其他内容:

one
two
three

我在 pwsh 7.4

list
  • 2 个回答
  • 38 Views
Martin Hope
Ralf_Reddings
Asked: 2024-12-20 23:17:10 +0800 CST

努力通过 $PSDefaultParameterValues 字典为“.ps1”脚本提供默认参数值

  • 7

我有一个 .ps1,它有一个-path参数,我想通过$PSDefaultParameterValues字典赋予它一个默认值,我认为这将以与 cmdlet 相同的方式工作:

$PSDefaultParameterValues=@{
    "C:\Users\user1\Documents\PowerShell\Scripts\myScript.ps1:path" = "C:\temp\db.jsonc"
}

代码 myScript.ps1如下:

[CmdletBinding()]
param (
    [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,mandatory)]
    [string]$Path
)
"path ---> $path"

因此,当我输入时,.\myScript.ps1我会得到“提供以下参数的值:”提示,但如果我运行.\myScript.ps1 -path c:\some\path输出是path ---> c:\some\path

我尝试重新编写字典,但脚本仍然没有收到-path参数的值:

$PSDefaultParameterValues=@{
    #"myScript.ps1:path" = "C:\temp\db.jsonc"  
    "myScript.:pfath" = "C:\temp\db.jsonc"     # this does not work either
    #"C:\Users\user1\Documents\PowerShell\Scripts\myScript.ps1:path"    = "C:\temp\db.jsonc"
}
powershell
  • 2 个回答
  • 61 Views
Martin Hope
Ralf_Reddings
Asked: 2024-12-20 02:34:02 +0800 CST

powershell 通配符有‘或’功能吗?

  • 7

我正在尝试弄清楚是否可以将mkvormp4与匹配get-childItem。查看 about_wildcards 页面,似乎没有具体提到“or”。无论如何我都尝试过:

get-ChildItem -path 'c:/temp' -File -Filter '[*mkv][*mp4]'
get-ChildItem -path 'c:/temp' -File -Filter '[*mkv][*mp4]?'
get-ChildItem -path 'c:/temp' -File -Filter '[*mkv]?[*mp4]?'

“temp”目录中有 mp4 和 mkv 文件,所以我希望上述操作能够返回它们......但我什么也没得到。

-match我知道这是一个类似于或的小事where-object,但我希望利用通配符,因为这意味着我可以在一次调用中完成所有事情。

我想知道是否可以使用 pwsh 通配符来实现这一点。我使用的是 pwsh 7.4

powershell
  • 1 个回答
  • 52 Views
Martin Hope
Ralf_Reddings
Asked: 2024-12-02 23:10:19 +0800 CST

这个“@()”构造有什么有意义的区别吗?或者这是一个错误?

  • 7

我有一个函数,name我想为其参数提供制表符补全,制表符补全值是在 中找到的文件名c:\temp。除了使用 中的文件c:\temp作为值之外,我还想添加其他制表符补全值。以下是函数

Function foo{
    Param(
    [ValidateSet([layoutNames], ErrorMessage = """{0}"" Is not a valid Layout name")]
    $Name
    )
    $name
}

以及layoutNames参数使用的类name:

Class layoutNames : System.Management.Automation.IValidateSetValuesGenerator{
    [string[]] GetValidValues(){
        #return @((Get-ChildItem -path 'c:\temp' -File).BaseName, "valueFoo", "valueBar")        #tab completetion only suggests "valueFoo" and "valueBar"
        #return @("valueFoo", "valueBar", (Get-ChildItem -path 'c:\temp' -File).BaseName)        #tab completetion only suggests "valueFoo" and "valueBar"
        return @(                                                                                #tab completetion suggests "valueFoo" and "valueBar" and the file names.
                    "valueFoo", "valueBar"
                    (Get-ChildItem -path 'c:\temp' -File).BaseName
                    )
    }}

对于上述内容,只有第三个return示例有效,唯一的区别是新行......我认为。

我花了很长时间试图弄清楚这一点,我最初从一个如下所示的返回语句开始:

return [string[]]("valueFoo", "valueBar", (Get-ChildItem -path 'c:\temp' -File).BaseName)

但由于没有任何效果,所以我不断地进行修改,直到最终通过使用数组运算符来实现@()。

我的问题的关键是,为什么当以以下方式声明时,该类不能按预期与函数一起工作foo,即建议同时使用valueFoo和valueBar文件名c:\temp

Class layoutNames : System.Management.Automation.IValidateSetValuesGenerator{
    [string[]] GetValidValues(){
        #return [string[]]("valueFoo", "valueBar",(Get-ChildItem -path 'C:\Users\INDESK\AppData\Roaming\GPSoftware\Directory Opus\Layouts' -File).BaseName)             # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested
        #return [string[]]("valueFoo", "valueBar",((Get-ChildItem -path 'C:\Users\INDESK\AppData\Roaming\GPSoftware\Directory Opus\Layouts' -File).BaseName))               # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested
        return [string[]](((Get-ChildItem -path 'C:\Users\INDESK\AppData\Roaming\GPSoftware\Directory Opus\Layouts' -File).BaseName),"valueFoo", "valueBar")                # no files names are suggested. only 'valueFoo' and 'valueBar' are suggested
    }}

我在 pwsh 7.4/win11 上

arrays
  • 2 个回答
  • 48 Views
Martin Hope
Ralf_Reddings
Asked: 2024-11-13 02:06:29 +0800 CST

如何在“.ps1”文件中声明一个类?

  • 9

我想为参数提供一个允许的名称列表name,以便用户可以tab输入它们。我想出了以下几点:

Param(
    [ValidateSet([foo])]
    [string]$Name
    )
    $name

Class foo : System.Management.Automation.IValidateSetValuesGenerator{
    [string[]] GetValidValues(){
    return [string[]] ("cat", "dog", "fish")
    }}
    

建议输入.\myScript.ps1 -name 然后不按任何内容。运行会返回错误:tab.\myScript.ps1

Line |
   3 |      [ValidateSet([foo])]
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Unable to find type [foo].

我可以在文件中或仅将其作为标准函数进行精确设置psm1,而且它可以正常工作。我曾考虑将类移至脚本文件的顶部,但由于param()需要将其放在第一行,因此不允许这样做。

有办法解决这个问题吗?我在用 pwsh 7.4

powershell
  • 1 个回答
  • 64 Views
Martin Hope
Ralf_Reddings
Asked: 2024-11-10 08:29:39 +0800 CST

使用'[System.Windows.Forms.Clipboard]::ContainsData',如何使用它来检查剪贴板是否具有'html'数据格式?

  • 6

我正在尝试弄清楚如何使用此类。我想我知道如何让它工作,当我想检查剪贴板是否具有格式时text。复制文本后,返回以下内容true:

[System.Windows.Forms.Clipboard]::ContainsData('text')

但是,如果我从一篇文章或网页上复制带有链接的文章,则会返回以下内容false:

[System.Windows.Forms.Clipboard]::ContainsData('html')

我确信我的剪贴板中有 html 格式,因为我可以将其粘贴到允许我这样做的软件中,例如 Obsidian。

我这样做是因为我想以 html 格式获取剪贴板内容。我尝试了以下操作:

[System.Windows.Forms.Clipboard]::GetData('text')

并返回一个对象:

CanRead      : True
CanSeek      : True
CanWrite     : True
Capacity     : 2354
Length       : 2354
Position     : 0
CanTimeout   : False
ReadTimeout  :
WriteTimeout :

但做同样的事情[System.Windows.Forms.Clipboard]::GetData('html')却没有任何返回结果,我再次确信我的剪贴板中有 html 内容。

我需要这样做,因为我只需要将剪贴板的 html 内容作为字符串获取,以便在 PowerShell 中进一步处理。我使用的是 pwsh 7.4

powershell
  • 1 个回答
  • 35 Views
Martin Hope
Ralf_Reddings
Asked: 2024-10-08 01:36:16 +0800 CST

如何获取函数的参数来自动完成命令名称(如“Get-Command”)?

  • 7

我正在编写一个函数,它将为我拥有的各种命令/函数提供帮助。我知道 PowerShell 已经尽了最大努力提供“实时”帮助,我只需要一两个这样的函数来满足我的需求。

Get-Command可以以某种方式建议作为会话一部分的命令/函数以及“未加载”的命令/函数,我希望我的函数的“名称”参数也能这样做,但我在这里不知所措,我搜索了一下,没有找到任何有用的东西。

我想到的是类似以下内容的,按下tab“名称”参数,建议命令名称:

Function Get-Help{
    Param(
    $Name 
    )

    #Do something with the provided command/function name
}

普惠制 7.4

powershell
  • 1 个回答
  • 33 Views
Martin Hope
Ralf_Reddings
Asked: 2024-10-06 04:09:32 +0800 CST

使用类动态获取列表时,如何向“ValidateSet”添加附加值?

  • 6

我有一个函数,我想使用在 中找到的文件名列表动态地为 Name 参数提供值c:\names,以便tab始终提供最新的名称。我已经想出了如何使用类来实现这一点,但我还想做一些“聪明”的处理。如果用户提供*或?作为值,那么这也应该是可以接受的。我本质上想使用这些字符作为参数的“修饰符”。

以下是我所拥有的:

Function fooo{
    Param(
    [ValidateSet([validNames], "*", ErrorMessage = """{0}"" Is not a valid name")]
    #[ValidateSet([validNames], ErrorMessage = """{0}"" Is not a valid name")]           #'tab' works as expected here
    [string]$Name
    )
    if ($name -eq "*"){"Modifier Used, do something special insead of the usual thing"}
    $name
}

Class validNames : System.Management.Automation.IValidateSetValuesGenerator{
    [string[]] GetValidValues(){
        return [string[]] (Get-ChildItem -path 'C:\names' -File).BaseName
    }}

上述操作tab不会自动完成 Name 参数的任何值,有时我甚至会收到错误:

MetadataError: The variable cannot be validated because the value cleanup4 is not a valid value for the Name variable.

*我可以向 Name提供值,但我没有收到任何错误:

fooo -name *

#Modifier Used, do something special insead of the usual thing

我知道我可以在这里使用 switch 参数,而不是按照这条路线走,我主要关心的是如何在类提供的值之上添加其他值ValidNames?类似于:

...
[ValidateSet([validNames], "foo", "bar", "baz", ErrorMessage = """{0}"" Is not a valid name")]
...

我使用的是 PWS 7.4

powershell
  • 1 个回答
  • 50 Views
Martin Hope
Ralf_Reddings
Asked: 2023-09-22 01:31:36 +0800 CST

G33kDude cJson - 将 Json 字符串加载到 AHK 后,如何通过 AHK 表示法访问密钥?

  • 6

我有以下json文件并想将其转换为 AHK 对象:

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
            "files.exclude": {
            "**/.git": true,
            "**/.svn": true,
            "**/.hg": true,
            "**/CVS": true,
            "**/.DS_Store": true,
            "**/Thumbs.db": true,
            "**.lnk":true,
            "*.code-workspace" :true,
            "**.vscode/":true,
            "**.url":true
        },
        "terminal.integrated.fontSize": 12,
    }
}

我只关心密钥terminal.integrated.fontSize及其值12。我想增加/减少它并通过覆盖文件将其保存回来。

我已经尝试了以下操作,我一直在获取值terminal.integrated.fontSize,调试器显示Object {["files.exclude"]: Object, ["terminal.integrated.fontSize"]: 12}但OutPutDebug只打印任何内容:

path    := "C:\temp\Rest APIs.code-workspace"
Obj     := Json.Load(FileOpen(Path,"r").Read())
OutputDebug, % Obj.settings
OutputDebug, % Obj.settings[1]
OutputDebug, % Obj.settings[2]

如果您想知道我在做什么,我在这里解释一下, 如何使用热键增加“Settings.json”中设置的值?

任何帮助将不胜感激!

autohotkey
  • 1 个回答
  • 18 Views
Martin Hope
Ralf_Reddings
Asked: 2023-09-18 02:58:01 +0800 CST

Select-String RegEx 如何在此示例中仅选择“}”

  • 6

可以说我有这样的字符串:

foo
}
bar}
    }
{baz}

我只想匹配位于行开头或前面有空格(例如一个或多个 tab字符)的右大括号。

因此,对于上面的示例,只应选择第 2 行和第 4 行的大括号(从 1 开始计数)

第一次尝试似乎只是突出显示第 2 行的右大括号,第 4 行的大括号未被选中:

"foo
}
bar}
    }
{baz}" |Select-String "\n\s?\}" -AllMatches

我尝试使用 使空白标记成为可选?,但随后第 2 行的大括号被取消选择:

"foo
}
bar}
    }
{baz}" |Select-String "\n\s+?\}" -AllMatches

-replace嗯...我很确定我现在用or会成功,-match但我真的需要掌握Select-String.

powershell
  • 1 个回答
  • 28 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve