标签: powershell

Powershell 从字符串中生成 System.Type

我正在动态生成某种类型的 .net 对象,如下所示$MyType=([System.Type]::GetType("System.String") 这对于标准 .net 对象来说效果很好。

我现在创建了一个像这样的自定义类

class InstanceName
{
    [String] hidden $Val
    InstanceName([String]$CVal)
    {
        $this.Val=$CVal
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,此方法不起作用,因为 powershell 无法找到类型。 $MyType=([System.Type]::GetType("InstanceName")

知道如何获取自定义 PS 类的 System.Type 吗?

谢谢!

.net reflection powershell types

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

将空值写入红色

我有一个基本命令,用于检查大约 100 个服务器的最近 5 个更新并返回它们的安装日期。如果安装日期为空,我想让脚本返回红色行。

命令是

 Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName $ComputerName | Select-object -Property PSComputerName, @{n='HotFix ID';e={$_.hotfixid}}, @{n='Installation Date';e={$_.installedon}} | select -Last 05
Run Code Online (Sandbox Code Playgroud)

我尝试使用格式颜色但它不起作用。命令的输出如下所示

PSComputerName HotFix ID Installation Date
-------------- --------- -----------------
DC1            KB4132216 10/6/2021 12:00:00 AM

DC1            KB4535680 11/3/2021 12:00:00 AM

DC1            KB4589210

DC1            KB5005698 10/6/2021 12:00:00 AM

DC1            KB5007192 11/16/2021 12:00:00 AM
Run Code Online (Sandbox Code Playgroud)

没有日期的线我想是红色的。这可能吗?

谢谢你!

powershell

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

将小数点数字转换为整数 - PowerShell

我正在编写一个 PowerShell 脚本,它将带小数点的数字转换为整数。

$val = 1024.24

如何将该值转换为整数?(我希望它是1024)

powershell integer decimal-point

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

tzutil - FIND:参数格式不正确的 PowerShell 命令

我正在使用 PowerShell 命令。以下命令在执行获取特定 上的时区时给出参数格式错误UTC。

C:\Windows\system32\tzutil /l | find /I "utc-06"
Run Code Online (Sandbox Code Playgroud)

FIND:参数格式不正确

powershell

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

Powershell:找不到名为“AmsiContext”的变量

概括

将 Vagrant 与 Virtualbox 和 Powershell 5.* 结合使用时,我在运行任何 vagrant 命令时收到一条消息。

PS C:\Users\dharris\SparkleShare\vagrant-salt> vagrant up es-master
Vagrant failed to initialize at a very early stage:

The version of powershell currently installed on this host is less than
the required minimum version. Please upgrade the installed version of
powershell to the minimum required version and run the command again.

  Installed version: Cannot find a variable with the name 'AmsiContext'.Cannot find a variable with the name 'AmsiInitFailed'.Cannot find a variable with the …
Run Code Online (Sandbox Code Playgroud)

windows powershell vagrant

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

如果文件正在更改(即正在下载的文件),Powershell 无法正确读取文件大小

我正在编写一个 Powershell 脚本,旨在在一个或多个文件下载完成后关闭 Windows 计算机(笔记本电脑/PC)(为了举例说明,我们假设这里有一个大文件)。

简而言之,我们每 x 秒读取整个下载文件夹的大小,并比较之前和之后的延迟大小。如果最近一段时间没有变化,则说明下载要么完成,要么卡住,这两种情况都会导致关机。

给出以下脚本(尺寸获取器在某些时候可以是一个独立的函数,是的):

#Powershell5
#Major  Minor  Build  Revision
# -----  -----  -----  --------
# 5      1      19041  1320  

$downloadDir = "C:\Users\edi\Downloads"

while (1) 
{
    $s1 = Get-ChildItem -Path $downloadDir | Measure-Object -Property Length -Sum | Select-Object Sum
    write-host "S1:" $s1.sum

    # this 3 seconds time is just for testing purposes; in real case scenario this will most
    # likely be set to 60 or 120 seconds.
    Start-Sleep -s 3

    $s2 = Get-ChildItem -Path $downloadDir …
Run Code Online (Sandbox Code Playgroud)

powershell ntfs filesize

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

从 PowerShell 对象生成 CLIXML 字符串,无需先序列化到磁盘

我有以下代码,它将一个对象导出到 XML 文件,然后将其读回并打印在信息流上。

try{
  # Sample object
  $Person = @{
    Name = 'Bender'
    Age  = 'At least 1074'
  }
  $Person | Export-CliXml obj.xml
  
  $cliXml = Get-Content -Raw ./obj.xml
  Write-Host $cliXml
} finally {
  if( Test-Path ./obj.xml ) {
    Remove-Item -Force ./obj.xml -EV rError -EA SilentlyContinue
    if( $rError ) {
      Write-Warning "Failed to remove ./obj.xml: $($rError.Exception.Message)"
    }
    Remove-Variable -Force $rError -EA Continue
  }
}
Run Code Online (Sandbox Code Playgroud)

有一个系统本地父会话,它监视其 STDOUT 以获取此输出,并将其重建为自己会话中的对象。

注意:我知道本地PSRemoting会话可以工作,但我需要它也可以在尚未配置或不会配置PSRemoting的系统上工作。

我想去掉中间人,而不是将对象写入磁盘。不幸的是,
Import-CliXMl和Export-CliXml是名称中唯一带有 和 的 cmdlet …

powershell serialization powershell-core

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

PowerShell 中的 LINQ

我正在将应用程序从 C# 转换为 PowerShell。如何从 PowerShell 调用 LINQ?



    [Data.DataTable]$dt = New-Object System.Data.DataTable
    [Data.DataColumn]$column = New-Object System.Data.DataColumn "Id", ([int])
    $dt.Columns.Add($column)
    
    # add data
    [Data.DataRow]$row = $dt.NewRow() # 
    $row["Id"] = 1
    $dt.Rows.Add($row)
    $row = $dt.NewRow() # 
    $row["Id"] = 2
    $dt.Rows.Add($row)
    
    # LINQ in C#: int[] results = dt.AsEnumerable().Select(d => d.Field("Id")).ToArray();
    [int[]]$results = [Linq.Enumerable]::Select($dt,[Func[int,int]]{ $args[0]})
    # Error: Cannot find an overload for "Select" and the argument count: "2"
    Write-Host $results

Run Code Online (Sandbox Code Playgroud)

linq powershell

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

我创建了一个自定义 powershell .psm1 模块,但编辑后它不会更新

我在目录中创建了一个自定义 powershell 模块
C:\Program Files\WindowsPowerShell\Modules\PennoniAppManagement。每当我对模块中的函数进行更改,然后将模块导入到脚本中时,更新的代码都不会生效。有什么解决办法吗?

powershell module

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

如何在 PowerShell 窗口中传递卷曲请求中的标头

curl -X POST <myUrl> -H "authorization: Bearer <valid token>"
Run Code Online (Sandbox Code Playgroud)

但是当我发送它时,我收到异常 - 无法绑定参数“标头”。无法将“System.String”类型的“authorization: Bearer”值转换为“System.Collections.IDictionary”类型

powershell curl http

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