标签: powershell

使用 Powershell 中的 Stack,如何传递包含空格的测试参数?

在堆栈外运行美味测试时,我需要将字符串“失败日志”作为美味模式参数传递。

这在 bash 中非常简单:

PS C:\Pyrethrum> stack test --fast --ta "-p \"fail log\""

但尝试使用 Powershell 实现同样的目标让我抓狂:


PS C:\Pyrethrum> stack test --fast --ta "-p ""fail"" "

当没有空间时可以工作(运行测试)


PS C:\Pyrethrum> stack test --fast --ta "-p ""fail log"" "

解析目标时出错:找不到目录:日志


PS C:\Pyrethrum> stack test --fast --ta "-p `"fail log`""

尝试安装一个名为 log 的包


PS C:\Pyrethrum> stack test --fast --ta "-p \"fail log\""

选项 --ta:未终止的字符串:endOfInput


PS C:\Pyrethrum> stack test --fast --ta "-p /fail log/ "

...构建但模式需要引号

除虫菊-0.1.0.0:测试(套件:除虫菊测试,参数:-p /失败日志/)

选项-p:无法解析模式


让它在 Powershell 中运行的正确命令行是什么?

powershell haskell command-line-arguments haskell-stack

3
推荐指数
1
解决办法
229
查看次数

Powershell:循环子目录并移动文件

我的目标是简单的任务。我想在提供的根文件夹“D:Temp\IMG”的所有子文件夹中创建常量名称“jpg”的文件夹,并将每个扩展名为“.jpg”的子文件夹中的所有文件移动到新创建的“jpg”文件夹。

我以为我可以在不深入了解 powershell 的情况下自己解决这个问题,但看来我不得不问。

到目前为止,我创建了这段代码

$Directory = dir D:\Temp\IMG\ | ?{$_.PSISContainer};
foreach ($d in $Directory) {
Write-Host "Working on directory $($d.FullName)..."
Get-ChildItem -Path "$($d.FullName)" -File -Recurse -Filter '*.jpg' |
  ForEach-Object {
      $Dest = "$($d.DirectoryName)\jpg"
      If (!(Test-Path -LiteralPath $Dest))
      {New-Item -Path $Dest -ItemType 'Directory' -Force}

      Move-Item -Path $_.FullName -Destination $Dest
  }
}
Run Code Online (Sandbox Code Playgroud)

我从中得到的是在每个子文件夹中创建文件夹“jpg”的无限循环。请问我的代码和逻辑哪里失败了?

powershell

3
推荐指数
1
解决办法
8467
查看次数

SQL 查询作为 Powershell 中的事务

我希望在 PowerShell 中将一些 SQL 查询作为事务执行。但是,我遇到错误并且找不到合适的答案。除了错误之外,BeginTransaction() 和 CommitTransaction() 是在 Powershell 中执行事务的方法吗?Intellisense 似乎没有显示 CommitTransaction() 方法。

错误:

Exception calling "ExecuteNonQuery" with "0" argument(s): "ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been 
initialized
Run Code Online (Sandbox Code Playgroud)

代码:

try {
    #SQL Connection
    $Connection = New-Object System.Data.SQLClient.SQLConnection
    $Connection.ConnectionString = "Server=$server,1433;Initial Catalog=$Database;Persist Security Info=False;User ID=$UserID;Password=$Password;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;;"

    $Connection.Open()

    $Command = New-Object System.Data.SQLClient.SQLCommand
    $Command.Connection = $Connection

    #Begin Transaction
    $Connection.BeginTransaction()

    #INSERT …
Run Code Online (Sandbox Code Playgroud)

sql powershell

3
推荐指数
1
解决办法
4719
查看次数

如何查明“-replace”是否确实做了什么

我对 PowerShell 相当熟悉,并且使用Get-Content管道连接到字符串替换表达式。但是,如果确实发生了变化,我只想将“新”内容写回到文件中。

我一直在使用简单的新/旧文件内容比较,但在较大的文件上它非常慢(就像非常慢)。我发现正则表达式替换实际上运行得相当快,因此如果有某种方法可以在替换完成后询问 PowerShell 是否发现任何内容,那就太理想了。如果没有进行任何更改,则不要将文件写回。

我尝试过测试$Matches.count,但我得到了

无法检索变量“$Matches”,因为尚未设置它。

我错过了什么吗?我真的不想在进行替换之前寻找匹配项,因为这看起来也很浪费(并且违反了“告诉不要问”)。

这是我目前正在做的事情:

function Convert-ToUTF8 {
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [string[]] $File)
    process {
        $File |
            %{
                $fileFullName = $_

                Write-Verbose "Loading $filefullname..."
                $content = (Get-Content $fileFullName)

                Write-Verbose "   Fixing xml prolog..."
                $newcontent = $content -replace '^<\?xml version="(\d+\.\d+)" encoding="(.+)"\?>$', '<?xml version="$1" encoding="UTF-8"?>'

                Write-Verbose "   Checking to see if there were changes..."
                $changed = $newcontent -ne $content

                if ($changed) {
                    if ($PSCmdlet.ShouldProcess("Write changes to $filefullname")) {
                        Write-Host "Writing changes to …
Run Code Online (Sandbox Code Playgroud)

regex powershell

3
推荐指数
1
解决办法
1151
查看次数

Azure Automation Runbook 无法将 webhook 数据解析为 JSON 对象

我对这个问题深感震惊。我请求您回答或给予提示。我已经没有选择了。

我通过 WebHook 在 CPU 利用率较高时调用 azure runbook。我的问题是 Runbook 内部数据未正确解码。例如,下面的行不打印任何内容。

 Write-Output $WebHookData.RequestHeader
Run Code Online (Sandbox Code Playgroud)

如果我尝试将数据显式转换为 JSON,就像这样

*$WebhookData = ConvertFrom-Json $WebhookData*
Run Code Online (Sandbox Code Playgroud)

那么这是一个投掷错误。

ConvertFrom-Json:无效的 JSON 原语:。在行:6 字符:31 + $WebhookData = $WebhookData | 从 Json 转换

顺便说一句,我正在尝试使用 Azure 库上提供的 Runbook {使用 Azure 自动化垂直扩展 Azure 资源管理器 VM}

我的 Webhook 是从虚拟机上创建的警报调用的。

一个非常奇怪的观察:

工作 WebHood 示例(在示例中找到) {"WebhookName":"test1","RequestBody":" [ \r\n {\r\n \"Message\": \"Test Message\"\r\n } \r\n****]****"

不工作(从虚拟机调用 Runbook 时发送的数据):

{"WebhookName":"test2","RequestBody":" { \"schemaId\":\"AzureMonitorMetricAlert\" } }

谢谢

powershell webhooks azure-autoscaling-block azure-automation azure-runbook

3
推荐指数
1
解决办法
4767
查看次数

在javascript中如何调用powershell脚本

我有一个 powershell 脚本,我在 powershell 上运行,如下所示:

.\download-packages-license.ps1

但我想在这些行之前调用 javascript 文件。

var json =fs.readFileSync('../../dev/licenses/AllLicenses.json', 'utf8');
var options = {compact: true, ignoreComment: true, spaces: 4};
var result = convert.json2xml(json, options);
Run Code Online (Sandbox Code Playgroud)

我在 stackoverflow 中什么也做不到,除了:How to run a powershell script from javascript? 所以请帮忙谢谢

javascript powershell

3
推荐指数
1
解决办法
3万
查看次数

PowerShell 脚本在粘贴到 PowerShell 窗口时运行,但在从快捷方式运行时不运行

我正在尝试编写一个脚本,将多个 Excel 文件转换为 PDF 文件。我在网上找到了一个有效的链接。

\n
$path = Read-Host -Prompt \'Input Directory Path and Press Enter\'\n$xlFixedFormat = \xe2\x80\x9cMicrosoft.Office.Interop.Excel.xlFixedFormatType\xe2\x80\x9d -as [type]\n$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse\n$objExcel = New-Object -ComObject excel.application\n$objExcel.visible = $false\nforeach($wb in $excelFiles)\n{\n    $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + \xe2\x80\x9c.pdf\xe2\x80\x9d)\n    $workbook = $objExcel.workbooks.open($wb.fullname, 3)\n    $workbook.Saved = $true\n    \xe2\x80\x9csaving $filepath\xe2\x80\x9d\n    $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)\n    $objExcel.Workbooks.close()\n}\n$objExcel.Quit()\n
Run Code Online (Sandbox Code Playgroud)\n

如果我将其复制并粘贴到 PowerShell 窗口中,程序将按预期运行。但是,当我尝试创建运行该程序的快捷方式时,出现了几个错误(文件保存为 .ps1)。

\n

这是我在设置快捷方式时所做的路径和参数:

\n
$path = Read-Host -Prompt \'Input Directory Path and Press Enter\'\n$xlFixedFormat = \xe2\x80\x9cMicrosoft.Office.Interop.Excel.xlFixedFormatType\xe2\x80\x9d -as …
Run Code Online (Sandbox Code Playgroud)

unicode powershell utf-8 character-encoding codepages

3
推荐指数
1
解决办法
2765
查看次数

获取 PSObject 数组大小或计数

我创建一个像这样的数组:

$Array = @()

$Item = New-Object PSObject
$Item | Add-Member -Type NoteProperty -Name item1 -Value test
$Item | Add-Member -Type NoteProperty -Name item2 -Value test

$Array += $Item
Run Code Online (Sandbox Code Playgroud)

现在我想$Item在添加之前添加一个检查以确定是否为空$Array。我怎样才能得到 的会员数量$Item?

我尝试过类似的东西:

$Item.count
$Item.length
@($Item).count
($Item | Measure).count
($Item | Get-Member).count
$Item.psobject.members.count
Run Code Online (Sandbox Code Playgroud)

但他们都没有给我实际的会员人数。

powershell psobject

3
推荐指数
1
解决办法
7952
查看次数

如何使用 Powershell 扩展 Docker RUN 命令中的参数?

我有一个使用 Windows Nano Server 基础映像并使用 Powershell 作为 shell 的 Dockerfile:

FROM microsoft/nanoserver
SHELL ["powershell"]
Run Code Online (Sandbox Code Playgroud)

我现在想使用命令定义一个变量(或通过 via 传递它--build-arg)ARG,然后在RUN命令中使用它,但我似乎无法让它工作。我试过了:

ARG mydir=tmp
RUN mkdir %mydir%
RUN mkdir $mydir
Run Code Online (Sandbox Code Playgroud)

但这些都不起作用。

如何告诉 docker / powershell 正确扩展我的变量?

windows powershell docker dockerfile nano-server

3
推荐指数
1
解决办法
2669
查看次数

-File 参数的参数“install-sshed.ps1”不存在 | 电源外壳

我正在使用 powershell 添加密钥到 windows 7 x64

到目前为止,我已经使用 powershell 尝试了以下命令

powershell -executionpolicy bypass -file install-sshed.ps1
Run Code Online (Sandbox Code Playgroud)

我收到这样的错误:

-File 参数的参数“install-sshed.ps1”不存在。提供现有“.ps1”文件的路径作为 -File 参数的参数

我做错了什么?

ssh powershell windows-7-x64

3
推荐指数
1
解决办法
8903
查看次数