标签: powershell

当dotnet测试失败时,打破PowerShell脚本

在一个小的PowerShell构建脚本中,我构建并测试了一个.NET应用程序.

我现在想要在测试导致一个或多个失败时阻止脚本中的进一步步骤.

我试着像这样得到测试的结果:

$exitcode = dotnet test  .\Domain.Tests
Write-Host "result $exitcode" 
Run Code Online (Sandbox Code Playgroud)

但遗憾的是它没有返回退出代码表示成功我只是得到了测试测试说明.

有没有更好的方法然后解析失败的测试计数的字符串输出?

.net powershell .net-core dotnet-test

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

在一元运算符' - '之后缺少表达式

这是我的第一篇文章,因此对任何错误道歉.

我在Windows Powershell中运行了dotnet命令,它给了我如何使用它的说明:

Usage: dotnet [host-options] [command] [arguments] [common-options]

Common options:  
  -v|--verbose          Enable verbose output  
  -h|--help             Show help
Run Code Online (Sandbox Code Playgroud)

当我运行命令时

dotnet run -h|--help
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误:

At line:1 char:17
+ dotnet run -h|--help
+                 ~
Missing expression after unary operator '--'.
At line:1 char:15
+ dotnet run -h|--help
+               ~~
Expressions are only allowed as the first element of a pipeline.
At line:1 char:17
+ dotnet run -h|--help
+                 ~~~~
Unexpected token 'help' in expression or statement.
    + CategoryInfo          : ParserError: …
Run Code Online (Sandbox Code Playgroud)

.net powershell .net-core

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

从robocopy日志中删除百分比

我正在使用较旧的版本,robocopy即使已设置/NJH/NJS参数,该版本也会在日志中包含百分比。因此,我想从日志中删除百分比:

            72880735 H:\ 1.txt
100%  
            33038490 H:\ 10.txt
100%  
            64878348 H:\ 2.txt
100%  
            25875810 H:\ 3.txt
  0%  
100%  

我尝试过

(Get-Content $logfile) | Where-Object {
    $_ -match '[\s](\d{1,})(\s+)(\w\W\W.+)'
} | Set-Content $logfile
Run Code Online (Sandbox Code Playgroud)

但这导致

            72880735 H:\ 1.txt
            33038490 H:\ 10.txt 
            64878348 H:\ 2.txt
            25875810 H:\ 3.txt
  0%  

所以我去除了100%,但没有去除0%。

powershell robocopy

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

Powershell - 从字符串中删除

我使用以下语法从JSON文件中提取IP列表

$Request = 'https://url-to-json.com/file.json'
$AWSIPs = Invoke-WebRequest $Request | ConvertFrom-Json | Select-Object prefix -ExpandProperty prefixes -ExcludeProperty ("/.*") | Where-Object -Property "service" -EQ "service_name" | select ip_prefix
foreach ($awsip in $AWSIPs){
echo $awsip
}
Run Code Online (Sandbox Code Playgroud)

这将以这种方式返回IP列表: - 0.0.0.0/00

  • 0.0.0.0/00
  • 0.0.0.0/00
  • 0.0.0.0/00
  • 0.0.0.0/00
  • 0.0.0.0/00

我需要使用这个IP列表,但是在我能够这样做之前我需要在结尾处删除/ 00(显然不是00但它是子网掩码,这很少相同).

我非常感谢这方面的帮助.

谢谢.

windows powershell scripting json

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

在powershell中concat两个字符串并作为一个参数提供

我试图将两个字符串合并为一个变量,然后将新变量作为参数添加到命令中

$firstName = Read-Host -Prompt 'enter user first name'
$lastName = Read-Host -Prompt 'enter user last name'
$userAblevetsEmail = '' + $firstName + '.' + $lastName + '@company.com'
New-MsolUser -UserPrincipalName $userAblevetsEmail  -DisplayName $firstName + " " + $lastName -FirstName $firstName
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

"New-MsolUser:找不到接受参数'+'的位置参数."

string powershell concatenation

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

使用参数在脚本中导入函数

我有一个带参数的脚本:

param (
    [Parameter(Mandatory=$true)][string]$VaultName,
    [Parameter(Mandatory=$true)][string]$SecretName,
    [Parameter(Mandatory=$true)][bool]$AddToMono = $false
 )
...
Run Code Online (Sandbox Code Playgroud)

在这个脚本中,我想要包含我在另一个ps1文件中编写的函数:common.ps1

我经常导入这个

. .\common.ps1
Run Code Online (Sandbox Code Playgroud)

但如果我在脚本中这样做,我得到:

The term '.\common.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
Run Code Online (Sandbox Code Playgroud)

如何在此脚本中导入common.ps1?

谢谢!

powershell

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

停止为Connect-VIServer返回连接消息

我有一台IIS计算机,托管一些调用Powershell文件的PHP。在Powershell文件中添加Powercli快照后,我建立了与vSphere的连接。

当它完成时,它会产生一些我不想要的输出,因为它被放到每个php输出页面上。

名称端口用户---- ---- ---- myhostaddress.com 443 mynetwork \ reporteruser

我试图在最后添加变量以停止反馈

Connect-VIServer -server myhostaddress.com -User $logincred.User -Password $logincred.Password  -WarningAction SilentlyContinue -ErrorAction Continue
Run Code Online (Sandbox Code Playgroud)

但没有用。

有没有办法阻止它。还是一种巧妙的调用方式,以便将输出转储到其他地方?

powershell powercli

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

带参数的Powershell调用MSI

我正在使用Powershell脚本来安装Autodesk产品,但遇到了一些麻烦。

我尝试了许多不同的方法,并不断遇到错误。

使用双引号

(Start-Process "msiexec.exe" -ArgumentList ""/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR="C:\Program Files\Autodesk\" ADSK_SETUP_EXE=1 /qb!"" -NoNewWindow -Wait -PassThru).ExitCode
Run Code Online (Sandbox Code Playgroud)

错误无法验证参数'ArgumentList'上的参数参数为null或为空。

使用变量定义InstallDir

$RevitInstallDir = "C:\Program Files\Autodesk\"
    (Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR=$RevitInstallDir ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode
Run Code Online (Sandbox Code Playgroud)

这样做我得到msiexec / option必需参数错误。

也在单引号和路径引号上尝试过

(Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR="C:\Program Files\Autodesk\" ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode
Run Code Online (Sandbox Code Playgroud)

我收到一个找不到接受参数C:\ Program的位置参数

在InstallDir上使用单引号

(Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR='C:\Program Files\Autodesk\' ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode
Run Code Online (Sandbox Code Playgroud)

这样做我得到msiexec / option必需参数错误。

在外面使用单引号

(Start-Process "msiexec.exe" -ArgumentList '/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR="C:\Program Files\Autodesk\" ADSK_SETUP_EXE=1 /qb!' -NoNewWindow -Wait -PassThru).ExitCode
Run Code Online (Sandbox Code Playgroud)

如果执行此操作,它将阻止$ …

powershell

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

生成没有外部库的自签名证书

我很想知道是否有一种简洁的方法来创建一个与下面的New-SelfSignedCertificate命令相当的自签名证书(例如,其他提供商也可以).我想只使用没有P/Invoke的.NET库或Bouncy Castle等外部库,或者不使用应用程序中的PowerShell.

New-SelfSignedCertificate -DnsName $certificateName -CertStoreLocation $certificateStore -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter $certificateNotAfter
Run Code Online (Sandbox Code Playgroud)

我想最简单的替代方法是调用PowerShell或使用Nuget库,如Bouncy Castle,如果没有外部设施这是不可行的?虽然感觉就像我知道如何构建证书一样,但是可以创建一个字节数组模板等,并在X509Certificate2构造函数中使用它.

似乎有人需要

public X509Certificate2 GenerateCertificate(string fileName, string password, string subjectName, StoreName storeName, DateTime endDate, DateTime notAfter, string provider = "Microsoft Enhanced RSA and AES Cryptographic Provider")
{
    //Could provider be taken from https://stackoverflow.com/questions/43474902/generate-self-signed-rsa-2048-sha-256-certificate-pfx-file-using-openssl?            
    var newCertificate = new X509Certificate2(fileName, password, X509KeyStorageFlags.Exportable);

     /*
      # The following creates a self-signed certificate with one year of running time.
     $currentDate = …
Run Code Online (Sandbox Code Playgroud)

.net powershell x509certificate2 x509certificate .net-core

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

如何设置PowerShell Cmdlet的默认输出格式?

我正在C#中创建PowerShell Cmdlet,以从JSON文件读取字段并输出一些对象。这些对象是简单的POCO类型:

public class FieldDefinition
{
    public FieldDefinition(string name, TypeCode code)
    {
        Name = name;
        Code = code;
    }

    public string Name { get; }

    public TypeCode Code { get; }
}
Run Code Online (Sandbox Code Playgroud)

写入输出时,条目显示为:

PS ~Debug> Get-Field

Name : Session
Code : Int64

Name : Instance
Code : String
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望默认显示形式为表格:

PS ~Debug> Get-Field | Format-Table

Name     Code
----     ----
Session  Int64
Instance String
Run Code Online (Sandbox Code Playgroud)

当我调用诸如之类的命令Get-Process时,总是显示表格格式。如何使命令的输出显示为表格?

c# powershell

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