我用C#编写了一个简单的 PowerShell cmdlet来展示我遇到的问题。随意克隆 repo,或 fork 并使其工作并提交 PR,或者只是查看源代码以确切了解我在做什么。
我已经使用PowerShellStandard.Library NuGet 包创建了一个简单的 PowerShell Core cmdlet 。我正在使用 xUnit 并尝试针对我创建的 PowerShell cmdlet 运行单元测试。问题是当我调用.Invoke()cmdlet 实例的方法时,它抛出一个NullReferenceException.
这是我创建的 cmdlet:
[Cmdlet(VerbsCommon.Get, "RepeatedString")]
[OutputType(typeof(string))]
public class GetRepeatedStringCmdlet : Cmdlet
{
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Alias("Word")]
[ValidateNotNullOrEmpty()]
public string Phrase { get; set; }
[Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true)]
[Alias("Repeat")]
public int NumberOfTimesToRepeatPhrase { get; set; }
protected …Run Code Online (Sandbox Code Playgroud) 我正在尝试向我们的团队频道发送一个 base64 字符串,它是一个 png,具有“传入 Webhook”设置。
消息已发送到频道,但未显示图像,当我搜索此消息时,似乎无法将图像或任何其他类型的附件作为 base64 字符串发送到团队?
对 json 来说非常新,并且通常使用/使用 rest api。
我不知道如何指示正文字符串是 base64 字符串,并且预期输出应该是 png 文件。
试过这个:
$webhook = 'URLtoTeamsWebhook'
$base64 = "longBase64string"
$Body = @{
"text" = "Hello World! from PowerShell"
"imageData" = "$base64"
}
$params = @{
Headers = @{'accept'='application/json'}
Body = $Body | convertto-json
Method = 'Post'
URI = $webhook
}
Invoke-RestMethod @params
Run Code Online (Sandbox Code Playgroud)
但它没有用,还尝试了下面的代码,对身体进行了更改:
$Body = @{
"type" = "string",
"contentEncoding" = "$base64",
"contentMediaType" = "image/png"
}
Run Code Online (Sandbox Code Playgroud)
它也不起作用。
“不起作用”= 发布到团队但根本不显示图像。
更新:
能够解决这个问题,有点,但仍然希望得到答复;
https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/cards/cards-reference
我想你可以“只”使用他们的卡通过他们的“传入网络钩子”发送数据,我想使用的卡是“英雄卡”,因为数据是带有图形图像的警报,但是它是使用“连接器”时不支持:(
我找到了这个网站; …
PowerShell 版本:5.x、6
我正在尝试创建 的新对象System.Collections.Generic.Dictionary,但失败了。
我尝试了以下“版本”:
> $dictionary = new-object System.Collections.Generic.Dictionary[[string],[int]]
New-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComObject'. Specified method is not supported.
At line:1 char:25
+ ... ry = new-object System.Collections.Generic.Dictionary[[string],[int]]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewObjectCommand
> $dictionary = new-object System.Collections.Generic.Dictionary[string,int]
New-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComObject'. Specified method is not supported.
At line:1 char:25
+ ... …Run Code Online (Sandbox Code Playgroud) 我喜欢git的posh-git powershell env,但在一些(非常大的)存储库上,它明显减慢了 ps 提示。
我想知道是否可以在某些特定存储库上禁用它
我有一个批处理文件,我的程序会自动与剪贴板一起工作。
但我想清除剪贴板并使用它:
echo>nul|clip
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以清除 Windows 剪贴板?
好的,最近我正在寻找一种在 PowerShell 中空合并的方法,我遇到了这篇文章:在 powershell 中空合并。
我看到@Zenexer 的评论,很感兴趣。这是语法:
Clear-Host
#expected one
"Test 1: " + ("one", "two", 1 -ne $null)[0]
#expected two
"Test 2: " + ($null, "two", 1 -ne $null)[0]
Run Code Online (Sandbox Code Playgroud)
这完美地工作。然而,我和一位同事 (Walter Puckett) 非常感兴趣,并且对语法进行了更多的挖掘,发现了一些真正的奇怪之处。
在我陷入奇怪之前,谁能指出任何解释这种语法的文档?
## THE WEIRDNESS:
# it does not matter what the number is evidently
"Test 3: " + ($null, "two", 8675309 -ne $null)[0]
# reversing the comparison test breaks the coalesce
"Test 4: " + ($null, "two", $null -ne 1)[0]
# Moving the test …Run Code Online (Sandbox Code Playgroud) 我正在使用PowerShell和PowerShell Core并行。并非我为“大”PowerShell 编写的所有脚本都在 PowerShell Core 中运行,反之亦然。
为了避免混乱,我在考虑是否应该使用两个文件扩展名:
.ps1: 电源外壳.pwsh: PowerShell 核心在Windows 中,扩展比在Unix系统中更重要,其中shebang(#!/bin/mytool) 有助于解决问题。
是使用两个扩展“最佳实践”还是有更好的选择?
附加:.pwsh当我从命令行/终端调用脚本时,我不确定PowerShell 是否会执行脚本。
我在 Unix / Linux 上下文中发现了一个类似的问题。 https://unix.stackexchange.com/questions/182882/use-sh-or-bash-extension-for-bash-scripts
如果程序失败,是否有一个优雅的 Powershell 脚本设置可以退出正在运行的 Powershell 脚本(或 shell 实例)?
我正在想象类似 Bash 功能set -o errexit(或set -e)但用于 Powershell 的东西。在该功能中,如果 bash 脚本中的程序失败(进程返回代码不是0),则 bash shell 实例会立即退出。
在 powershell 中,脚本可以检查$LastExitCode. 但是,对于每个程序调用来说,这变得很麻烦。也许powershell有一个功能可以自动检查程序返回码并做出反应。
Set-Powershell-Auto-Exit 'ProgramReturnCode' # what should this be?
& program.exe --fail # this program fails with an error code
& foo.exe # this never runs because script has exited
Run Code Online (Sandbox Code Playgroud) 我想用这种方法捕捉图像
DISM.exe /capture-ffu /imagefile=e:\WinOEM.ffu /capturedrive=\\.\PhysicalDrive0 /name:disk0 /description:"Windows 10 FFU"
Run Code Online (Sandbox Code Playgroud)
参考:
https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/deploy-windows-using-full-flash-update--ffu
Run Code Online (Sandbox Code Playgroud)
在powershell中我用这种方式
& DISM.exe /capture-ffu /imagefile=e:\WinOEM.ffu /capturedrive=\\.\PhysicalDrive0 /name:disk0 /description:"Windows 10 FFU"
Run Code Online (Sandbox Code Playgroud)
它在捕获时显示命令窗口。我可以在捕获时创建一个像进度条这样的 GUI 吗?
感谢您的任何建议。
我已经创建了这个 PowerShell 脚本,但我不知道我应该把这个命令放在哪里
& DISM.exe /capture-ffu /imagefile=e:\WinOEM.ffu /capturedrive=\\.\PhysicalDrive0 /name:disk0 /description:"Windows 10 FFU"
Add-Type -assembly System.Windows.Forms
## -- Create The Progress-Bar
$ObjForm = New-Object System.Windows.Forms.Form
$ObjForm.Text = "Image Capturing"
$ObjForm.WindowState = 'Maximized'
$ObjForm.BackColor = "White"
$ObjForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$ObjForm.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
## -- Create The Label
$ObjLabel = New-Object System.Windows.Forms.Label
$ObjLabel.Text = "Starting. Please wait ... …Run Code Online (Sandbox Code Playgroud) 我有一个关于从注册表项 HKU (HKEY_USERS) 中删除的问题。如果我用 powershell 运行它,我会收到一个错误:
Invoke-Command -ComputerName $inputPC -ScriptBlock { Remove-Item -Path 'HKU:\S-1-5-25\Software\Microsoft\Windows\CurrentVersion\RunOnce'}
Run Code Online (Sandbox Code Playgroud)
错误:
Cannot find drive. A drive with the name 'HKU' does not exist.
+ CategoryInfo : ObjectNotFound: (HKU:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
+ PSComputerName : clt64792
Run Code Online (Sandbox Code Playgroud)
但是,如果HKU:我将HKLM:它交换为例如它可以工作并删除注册表项,我该如何访问HKU?我尝试了不同的方法来删除注册表项,并且所有方法都相同,但它们都没有映射 HKU 注册表项。
powershell ×10
bash ×1
batch-file ×1
c# ×1
clipboard ×1
cmd ×1
cmdlet ×1
posh-git ×1
progress-bar ×1
registry ×1
unit-testing ×1
windows ×1