小编San*_*zon的帖子

从 ZIP 存档中提取单个文件

我正在使用下面的代码下载一些 zip 存档:

$client = new-object System.Net.WebClient
$client.DownloadFile("https://chromedriver.storage.googleapis.com/$LatestChromeRelease/chromedriver_win32.zip","D:\MyFolder.zip")
Run Code Online (Sandbox Code Playgroud)

结果我得到了包含所需文件的 ZIP 存档“MyFolder.zip”(让我们想象一下“test.txt”)。

如何将这个特定文件从 ZIP 存档提取到给定文件夹中?

powershell zip unzip system.io.compression

8
推荐指数
1
解决办法
5067
查看次数

如何使用 powershell 压缩超过 2 GB 的文件?

我正在开发一个项目来压缩从几个 mb 到几个 GB 的文件,我正在尝试使用 powershell 将它们压缩成 .zip。我遇到的主要问题是使用压缩存档对单个文件大小有 2 GB 上限,我想知道是否有另一种方法来压缩文件。

编辑:

因此,对于这个项目,我们希望实现一个系统,从 Outlook 获取 .pst 文件并将其压缩为 .zip 并将其上传到服务器。上传后,它们将从新设备中拉出并再次提取到 .pst 文件中。

powershell zip system.io.compression compress-archive system.io.compression.zipfile

8
推荐指数
1
解决办法
8296
查看次数

PowerShell:“接收作业”如何从作业的代码块中详细提取输出?

请查看此测试脚本以及我对“Receive-Job”如何工作的详细结论。
我仍然有问题需要弄清楚,“接收作业”是如何从代码块中提取流的。

<# .SYNOPSIS Test the console output and variable capturing of Write- cmdlet calls in a code block used by 'Start-Job'
   .NOTES
    .NET Version                   4.7.2
    PSVersion                      5.1.16299.431
    PSEdition                      Desktop
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    BuildVersion                   10.0.16299.431
    CLRVersion                     4.0.30319.42000
    WSManStackVersion              3.0
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
#>

Set-StrictMode -Version latest

if ($host.Name -inotmatch 'consolehost') { Clear-Host }

$errorBuffer = $null
$warningBuffer = $null
$outBuffer = $null
$infoBuffer = $null

# Start the job
$job = Start-Job -ScriptBlock {

    Set-StrictMode -Version latest …
Run Code Online (Sandbox Code Playgroud)

powershell asynchronous io-redirection start-job

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

System.IO.FileInfo 和相对路径

我想知道是否有人可以帮助我理解为什么System.IO.FileInfo在处理相对路径时 Windows 上的行为与 Linux 上的行为不同。

例子

  • 在 Linux 上
PS /home/user/Documents> ([System.IO.FileInfo]'./test.txt').FullName
/home/user/Documents/test.txt
Run Code Online (Sandbox Code Playgroud)
  • 在 Windows 上
PS C:\Users\User\Documents> ([System.IO.FileInfo]'.\test.txt').FullName
C:\Users\User\test.txt
Run Code Online (Sandbox Code Playgroud)

编辑

System.IO.FileInfo为了澄清上述内容, Windows 或 Linux 上处理相对路径的方式没有区别。该问题与或 未[System.IO.Directory]::GetCurrentDirectory()更新有关。Push-LocationSet-Location

一个简单的例子:

PS /home/user> [System.IO.Directory]::GetCurrentDirectory()
/home/user
PS /home/user> cd ./Documents/
PS /home/user/Documents> [System.IO.Directory]::GetCurrentDirectory()
/home/user
Run Code Online (Sandbox Code Playgroud)

假设这是预期的行为,那么处理param(...)脚本和函数上的块以接受两种情况(绝对和相对)的最佳方法是什么。我曾经输入约束路径参数,System.IO.FileInfo但现在我可以看到它显然是错误的。

这是我遇到的情况,但我想知道是否有更好的方法。
我相信Split-Path -IsAbsolute使用网络路径也会带来问题,如果我错了,请纠正我。

param(
    [ValidateScript({ 
        if(Test-Path $_ -PathType Leaf) {
            return $true
        }
        throw 'Invalid File Path'
    })]
    [string] $Path
)

if(-not (Split-Path $Path -IsAbsolute)) { …
Run Code Online (Sandbox Code Playgroud)

powershell system.io.fileinfo

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

PowerShell:类内的类对象数组

我创建了一个包含简单值的类:

Class Volumes {
  [String]$ID
  [String]$Name
}
Run Code Online (Sandbox Code Playgroud)

现在我想创建另一个包含Volume对象数组的类。

就像是:

Class Storage {
  [String]$Name
  [String]$IP
  [Array]@Volumes  # <- I know this is wrong, but I don't know how to do it
}
Run Code Online (Sandbox Code Playgroud)

因此,有2个问题:

  • 如何在类中创建对象数组?
  • 一旦我构建了类的新对象,如何访问/修改数组成员Storage

提前致谢,拉尔夫。

arrays powershell class object

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

可以使用 Microsoft Graph PowerShell cmdlet 从AdditionalProperties 字典中提取信息吗?

我尝试使用 PowerShell Graph cmdlet 而不是 Azure AD 模块 cmdlet。使用 Azure AD 模块,我可以执行以下操作:

# This is what I want:
get-azureadgroupmember -objectid $GroupID | select-object -property displayname, `
    mail, userprincipalname, objectid

DisplayName     Mail                        UserPrincipalName  ObjectId
-----------     ----                        -----------------  --------
John Smith      John.Smith@example.org      jsmith@example.org 4bae8291-6ec3-192b-32ce-dd21869ef784
(...)


# All of these properties are directly accessible in the returned objects:
$res = get-azureadgroupmember -objectid $GroupID
$res[0] | fl -prop *
# Shows long list of directly accessible properties
Run Code Online (Sandbox Code Playgroud)

我试图找出 PowerShell Graph 的等价物:

$res = get-mggroupmember …
Run Code Online (Sandbox Code Playgroud)

powershell azure-active-directory microsoft-graph-api

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

Powershell 创建 CSV 文件

我是 powershell 新手。但是学习......我的脚本距离实现我想要的还很远。我只是想念最后一部分。我希望有人可以 我遇到的问题是在创建 CSV 文件时没有得到正确的结果。

好的,我有一个类似这样的 CSV 文件:

A           B
AAAA        111111
CCCC        222222
EEEE        333333
HHHH        444444
Run Code Online (Sandbox Code Playgroud)

现在我有一个包含以下信息的数组:

AAAA
BBBB
CCCC
DDDD
HHHH
Run Code Online (Sandbox Code Playgroud)

我将数组中的值与 CSV 文件进行比较。我想收到B列的结果。

这工作正常。我收到这个结果:

111111
222222
444444
Run Code Online (Sandbox Code Playgroud)

所以基本上这个是正确的。但是......我需要收到这个:

111111

222222

444444
Run Code Online (Sandbox Code Playgroud)

因此,当找不到该值时,我想要一个空单元格

我希望我的问题足够清楚;)

我的代码是这样的:

$CSV = import-csv C:\tmp\mac-vendors-export.csv -Delimiter ';'
$vendormacarray = $Vendormac
$vendorname = foreach ($UniqueVendorMac in $Vendormacarray) {
    $csv | where-object {$_.A -match $UniqueVendorMac} | select-object  -Expand B
}
Run Code Online (Sandbox Code Playgroud)

我想我必须添加类似-or $_.A -eq 'null'......的内容,但无法弄清楚最后一部分。

希望有人能帮我解答这个问题。

谢谢

csv powershell compare

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

为什么 powershell 控制台错误地调整了我的 GUI 表单的大小?

我想在 1920 x 1080 像素的 Windows 11 屏幕上打开特定尺寸的 powershell GUI 表单。设置中的系统缩放比例设置为 125%。这不可能是一个不寻常的配置。

作为测试,我尝试打开一个大小为屏幕四分之一的窗体,如下所示:

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object System.Windows.Forms.Form
$form.width = 960
$form.height = 540
$GuiResponse = $Form.ShowDialog()
Run Code Online (Sandbox Code Playgroud)

问题很简单 - 这在 Powershell ISE 中完美运行,但我无法让它在 Powershell 控制台中运行,因为窗口总是明显大于应有的大小 - 我认为是因为控制台正在执行某种反向缩放。

我当然可以自己进行缩放并使窗口按比例变小,以便从控制台运行时大小正确 - 但还有一个额外的相关问题,因为写入窗口的任何文本都是脏污的,因为它被不适当地缩放(猜测)。

Stack Overflow 和其他地方已经有一些关于此类问题的讨论,但我没有尝试解决这个问题。然而,这似乎是一个简单的问题,它肯定有一个简单的解决方案吗?

forms powershell user-interface scaling winforms

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

如何将原子计数器添加到 powershell ForEach -Parallel 循环

在这个问题中,解释了如何添加到并发ThreadSafe集合Powershell:如何将结果添加到数组(ForEach-Object -Parallel)

我有一个更简单的用例,我只想增加一个值。(整数)。

是否可以在 Powershell 中使用某种原子整数数据类型来完成?

$myAtomicCounter = 0

$myItems | ForEach-Object -Parallel {
    #...other work

    $myAtomicCounter.ThreadSafeAdd(2)

    # .. some more work using counter
}

Write-Host($myAtomicCounter)
Run Code Online (Sandbox Code Playgroud)

parallel-processing powershell multithreading

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

如何在 powershell 中使用分隔符连接和连接字符串数组?

PS C:\Users\User\ps-modules> 更多 .\MyStrings.Tests.ps1

function slist { "1", "2", "3" }

Describe 'StringTests' {

  It 'literal -join' {
    "1", "2", "3" -join "," | Should -Be "1,2,3"
  }

  It 'literal -join' {
    @("1", "2", "3") -join "," | Should -Be "1,2,3"
  }

  It 'slist returns a list of string' {
    slist | Should -Be @("1", "2", "3")
  }

  It 'slist -join' {
    slist -join "," | Should -Be "1,2,3"
  }

}
Run Code Online (Sandbox Code Playgroud)

PS C:\Users\User\ps-modules> pwsh .\MyStrings.Tests.ps1

Starting discovery in 1 files. …
Run Code Online (Sandbox Code Playgroud)

arrays string powershell concatenation pester

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