相关疑难解决方法(0)

如何在PowerShell中将输出捕获到外部进程的变量中?

我想运行一个外部进程并将其命令输出捕获到PowerShell中的变量.我目前正在使用这个:

$params = "/verify $pc /domain:hosp.uhhg.org"
start-process "netdom.exe" $params -WindowStyle Hidden -Wait
Run Code Online (Sandbox Code Playgroud)

我已经确认命令正在执行但我需要将输出捕获到变量中.这意味着我无法使用-RedirectOutput,因为这只会重定向到文件.

powershell process

140
推荐指数
7
解决办法
24万
查看次数

如何使用详细输出运行PowerShell脚本?

我想知道是否有办法运行PowerShell脚本,以便打印脚本的每一行的命令和输出.例如,在Bash中,您可以在脚本的顶部编写bash -x myscript或放置一个set -x.在批处理中,您将省略@echo off传统上留在脚本顶部的内容.PowerShell是否具有这些结构的等价物?

我尝试过的事情:跑步powershell -? | sls verbose,没有任何结果.

windows shell powershell windows-10

17
推荐指数
4
解决办法
4万
查看次数

Powershell:使用PS 5类时无法找到类型

我在PS中使用WinSCP Powershell Assembly.在其中一种方法中,我使用的是WinSCP中的各种类型.

只要我已经添加了程序集,这就可以正常工作 - 但是,由于Powershell在使用类时读取脚本的方式(我假设?),在加载程序集之前会抛出错误.

实际上,即使我将Write-Host放在顶部,它也不会加载.

在解析文件的其余部分之前,有没有办法强制运行某些东西?

Transfer() {
    $this.Logger = [Logger]::new()
    try {

        Add-Type -Path $this.Paths.WinSCP            
        $ConnectionType = $this.FtpSettings.Protocol.ToString()
        $SessionOptions = New-Object WinSCP.SessionOptions -Property @{
            Protocol = [WinSCP.Protocol]::$ConnectionType
            HostName = $this.FtpSettings.Server
            UserName = $this.FtpSettings.Username
            Password = $this.FtpSettings.Password
        }
Run Code Online (Sandbox Code Playgroud)

Protocol = [WinSCP.Protocol] :: $ ConnectionType

无法找到类型[WinSCP.Protocol].

powershell add-type winscp-net

8
推荐指数
2
解决办法
3741
查看次数

Powershell,使用(空格分隔的)字符串作为程序的参数

我读过这篇文章,它并没有解决我的问题。

我有一个以空格分隔的字符串,比方说$MyString = "arg1 arg2"。假设我有一个名为 的命令行程序MyProgram,它接受任意数量的位置参数,因此它可以像MyProgram arg1 arg2. 然而,doingMyProgram $MyString不起作用,norMyProgram ($MyString -split ' ')也不起作用MyProgram $($MyString -split ' ')。我得到了同样的错误,基本上说它无法识别参数“arg1 arg2”,我猜这是因为它仍然认为它是一个包含空格的参数而不是两个参数。实际上,$MyString可能相当大并且是从文件中读取的。我该如何进行这项工作?

string powershell parameter-passing

7
推荐指数
1
解决办法
3111
查看次数

在Powershell中创建具有动态属性名称的类

我需要能够在Powershell中创建一个类,在其中可以动态创建该类的属性名称。例如,

$binFiles = Get-ChildItem $files -Include *.BIN -recurse
$binFiles.Name
Run Code Online (Sandbox Code Playgroud)

这将打印如下内容:

File1.BIN

ThisFile.BIN

...

FileOther.BIN

我希望这些文件名成为我将多次创建的类中属性的名称。这有可能吗?

Class myItem {
    [String]$"File1.BIN"
    [String]$"ThisFile.BIN"
    ....
    [String]$"FileOther.BIN"
}
Run Code Online (Sandbox Code Playgroud)

powershell

6
推荐指数
1
解决办法
618
查看次数

在 PowerShell 中使用变量将多个参数传递给外部程序

我下载了用于合并 junit 报告的 npm 包 - https://www.npmjs.com/package/junit-merge

问题是我有多个文件要合并,并且我正在尝试使用字符串变量来保存要合并的文件名。

当我自己编写脚本时,如下所示:

junit-merge a.xml b.xml c.xml 
Run Code Online (Sandbox Code Playgroud)

这有效,正在创建合并文件,但是当我这样做时

$command = "a.xml b.xml c.xml"
junit-merge $command
Run Code Online (Sandbox Code Playgroud)

这是行不通的。错误是

错误:找不到文件

有人遇到过类似的问题吗?

powershell junit parameter-passing parameter-splatting

5
推荐指数
1
解决办法
1602
查看次数

带有 Invoke-Expression 的 powershell 的 Stderr 和 stdout 没有输出或错误

我想获得stdoutstderr运行一个Java进程,并看到了似乎是一个简单的,冷静的方式来做到这一点,但它没有工作:

$command = "java -jar secretserver-jconsole.jar -s 123456 Password"
Invoke-Expression $command -OutVariable output -ErrorVariable errors
Write-Host "stdout: $output"
Write-Host "stderr: $errors"
Run Code Online (Sandbox Code Playgroud)

没有显示$output$errors

我现在正在使用:

$output = cmd /c $command
Run Code Online (Sandbox Code Playgroud)

这确实让我得到了一些stdout,但它并不理想,因为我想要$error消息。

我什至尝试了这种精心设计的方法,但它似乎没有运行我认为的流程,因为它几乎立即返回,而且我知道正常流程任务几秒钟。它也没有显示输出或错误:

$psi = New-object System.Diagnostics.ProcessStartInfo 
$psi.CreateNoWindow = $true 
$psi.UseShellExecute = $false 
$psi.RedirectStandardOutput = $true 
$psi.RedirectStandardError = $true 
$psi.FileName = 'java' 
$psi.Arguments = @("-jar","secretserver-jconsole.jar","-
s","123456","Password") 
$process = New-Object System.Diagnostics.Process 
$process.StartInfo = $psi 
[void]$process.Start()
$output = $process.StandardOutput.ReadToEnd() 
$process.WaitForExit()
Write-Host "stdout: $output" …
Run Code Online (Sandbox Code Playgroud)

powershell stdout stderr

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

在PowerShell中按名称设置嵌套对象属性的值

我想使用PowerShell设置嵌套对象属性的值。当您尝试设置第一级属性的值时,这很简单:

$propertyName = "someProperty"
$obj.$propertyName = "someValue"  # ? It works
Run Code Online (Sandbox Code Playgroud)

对于嵌套属性,它不起作用:

$propertyName = "someProperty.someNestedProperty"
$obj.$propertyName = "someValue"  # ? It doesn't work and raises an error.
Run Code Online (Sandbox Code Playgroud)

如何使用PowerShell通过属性名称设置嵌套对象属性的值?

MCVE

对于那些想重现问题的人,这是一个简单的示例:

$Obj= ConvertFrom-Json '{ "A": "x", "B": {"C": "y"} }'
# Or simply create the object:
# $Obj= @{ A = "x"; B = @{C = "y"} }
$Key = "B.C"
$Value = "Some Value"
$Obj.$Key = $Value
Run Code Online (Sandbox Code Playgroud)

运行命令,您将收到一个错误:

“在此对象上找不到属性'BC'。请验证该属性存在并且可以设置。”

powershell anonymous-types cmdlet

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

使用变量间接访问PSObject属性

说我有JSON喜欢:

  {
    "a" : {
        "b" : 1,
        "c" : 2,
        }
  }
Run Code Online (Sandbox Code Playgroud)

现在很ConvertTo-Json乐意创造PSObjects出来.我想访问我能做的项目$json.a.b并得到1 - 很好的嵌套属性.

现在,如果我有字符串"a.b",问题是如何使用该字符串访问该结构中的相同项目?似乎应该有一些特殊的语法我缺少像&动态函数调用,因为否则你必须自己Get-Member反复使用我期望的解释字符串.

powershell

2
推荐指数
1
解决办法
450
查看次数

Powershell:如何从csv中检索powershell命令并逐一执行,然后将结果输出到新的csv

我有一个 Commands.csv 文件,例如:

| Command                                        |
| -----------------------------------------------|
|(Get-FileHash C:\Users\UserA\Desktop\File1).Hash|
|(Get-FileHash C:\Users\UserA\Desktop\File2).Hash|
|(Get-FileHash C:\Users\UserA\Desktop\File3).Hash|
Run Code Online (Sandbox Code Playgroud)

标头名称为“命令”

我的想法是:

  1. 使用ForEach ($line in Get-Content C:\Users\UserA\Desktop\Commands.csv ) {echo $line}
  2. 通过powershell.exe逐一执行$line,然后将结果输出到一个新的.csv文件——“result.csv”

您能给我一些实施这个想法的指导和建议吗?谢谢!

csv powershell

2
推荐指数
1
解决办法
327
查看次数

如何在 Windows 10,11 上使用 CLI 或 Powershell 命令执行curl

想要在 Linux 下的 Bash 中找到与此等效的内容

sh <(curl https://sample.com/cli.sh)
Run Code Online (Sandbox Code Playgroud)

在 Windows CLI 或 Powershell 中

我知道Windows 中安装了curl。我怎样才能实现它?

有人会说这样不安全。我还将有一个带有 Windows Powershell 的 cli.ps1。

windows powershell curl invoke-webrequest

2
推荐指数
1
解决办法
5010
查看次数