小编Eri*_*ris的帖子

在powershell 3中为json做好准备

给定一个标准的json字符串值:

$jsonString = '{ "baz": "quuz", "cow": [ "moo", "cud" ], "foo": "bar" }'
Run Code Online (Sandbox Code Playgroud)

如何才能让这一切变得非常漂亮,最好不要使用暴力正则表达式?

到目前为止我发现的最简单的方法是:

$jsonString | ConvertFrom-Json | ConvertTo-Json 
Run Code Online (Sandbox Code Playgroud)

然而,这似乎有点傻.

powershell json powershell-3.0

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

如何使用 powershell v2+ 检测脚本是否是点源脚本或作为模块的一部分加载?

给定一个 ps1 文件作为模块的一部分,代码如下:

function Get-Greeting {
    'Hello {0}' -f $Env:Username
}
Export-ModuleMember -Function:Get-Greeting
Run Code Online (Sandbox Code Playgroud)

当作为模块的一部分加载时,一切都很好。如果我对脚本进行点源,我会得到

Export-ModuleMember : The Export-ModuleMember cmdlet can only be called from inside a module.
Run Code Online (Sandbox Code Playgroud)

我知道我可以在 Export-ModuleMember 上添加 -ErrorAction:Ignore,但这不是重点。我想让脚本以不同的方式运行,无论它是导入的还是点源的。

在版本 2 中,人们可能可以围绕 编写一个 hack $PSScriptRoot,但这只是一个 hack,并且在版本 3 中不起作用,在版本 3 中它们“固定”$PSScriptRoot为永不为空。我尝试查看 中的各种项目$MyInvocation,但要么我错过了一些东西,要么它没有任何有用的东西。

我还尝试Get-Variable在模块内部和外部运行,但再次发现没有差异。

Import-Module当运行 as vs时我错过了什么不同的地方. myscript.ps1

powershell powershell-2.0 powershell-3.0

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

在Powershell中创建表

伙计我有一个查询远程计算机服务的脚本.它运行2个嵌套的foreach循环,1个用于计算机列表,内部用于服务列表.我想在二维数组中捕获进程的输出.

The desired output should be like this:

Computer    Service   State   StartupMode
-------------------------------------------
data         data      data     data
data         data      data     data
data         data      data     data
data         data      data     data
Run Code Online (Sandbox Code Playgroud)

我已经获取了数据并将其显示在控制台上,但未能将其存储在多列数组中.哈希表只能接受2列.

arrays powershell multidimensional-array

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

如果一个属性可为空,则在比较对象时,FluentAssertions失败

场景:我有一个带有可为空属性的对象,当我运行被测方法时,该对象将被更新。在期望的对象中,我没有指定它,因为我想单独验证该值。这是一个简单的测试演示

using System;
using FluentAssertions;
using NUnit.Framework;

namespace FluentAssertionsNullableFailure
{
    public class SimpleWithNullable
    {
        public Int64? nullableIntegerProperty
        { get; set; }

        public string strProperty
        { get; set; }
    }

    [TestFixture]
    public class Demo
    {
        public SimpleWithNullable actual = new SimpleWithNullable { nullableIntegerProperty = 1, strProperty = "I haz a string!" };
        public SimpleWithNullable expected = new SimpleWithNullable { strProperty = "I haz a string!" };

        [Test]
        public void NullableTest ()
        {
            actual.ShouldBeEquivalentTo (
                expected,
                opt => opt.Using<Int64?> ( c => …
Run Code Online (Sandbox Code Playgroud)

c# nunit fluent-assertions

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

Powershell添加成员.添加一个ArrayList的成员?

Powershell"add-member"命令非常有用.我用它来为自定义对象添加属性.有时我将一个成员设置为一个数组来保存多个对象.是否可以在自定义对象上添加ArrayList作为成员?

想象一下,文章列表中包含"索引","标题"和"关键字"属性.在Powershell中,您可以将此代码放在循环中:

for($i = 0; $i -lt 100; $i++) {
    $a = new-object -TypeName PSObject
    $a | Add-Member -MemberType NoteProperty -Name index -Value $i
    $a | Add-Member -MemberType NoteProperty -Name title -Value "Article $i"
    $a | Add-Member -MemberType NoteProperty -Name keywords -Value @()
    $articles += $a
}
Run Code Online (Sandbox Code Playgroud)

你最终会得到一个Article对象的数组文章,每个对象都包含成员索引,标题和关键字.此外,关键字成员是一个可以有多个条目的数组:

$articles[2].keywords += "Stack Exchange", "Powershell", "ArrayLists"
$articles[2].keywords[2]
Powershell
Run Code Online (Sandbox Code Playgroud)

这满足了我的大部分需求,但我不喜欢处理数组.如果只是因为,ArrayLists更容易使用

$arrayList1.remove("value")
Run Code Online (Sandbox Code Playgroud)

比直觉更直观

$array1 = $array1 |? {$_ new "value"}
Run Code Online (Sandbox Code Playgroud)

Add-Member是否有办法将ArrayList添加为成员?还是我坚持阵列?如果Powershell不支持,那么我可以使用一些C#代码来创建一个以ArrayList为成员的新类吗?

arrays powershell

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