Powershell hash table 调用外部命令

Powershell hash table call external command

我有一个 powershell 脚本,我计划检查前十天的日期 "transaction" 文件并按 LastWriteTime 排序,因为年初至今的数据总是在增长。周末没有变化,如果本周有 none,我知道服务器上的进程没有 运行。

生成的 table 包含列 "Name"、"Size (KB)"(将 "Length" 从字节转换为 KB)和 LastWriteTime。我想添加一个 "MD5" 列。我的计算机上有一个 md5sum.exe 命令。我想构建散列 table,以便它使用 $_.FullName 属性 在 expression 字段中调用外部命令。

我找到了如何使用 this post 本机使用 PS 计算 MD5 校验和的方法,并且我已将其作为单行程序集成到新列的 expression 值中。不过,我仍然想知道是否可以在哈希 table 中调用外部命令。

原代码如下:

$LOOKBACKXDAYS=( Get-Date ).AddDays(-10)
$SrcMachine='TheServerInQuestion'

if( -not ( Test-Path -Path \$SrcMachine\C$ ) ) { net use \$SrcMachine\C$ /user:mydomain\administrator }

Get-ChildItem -Path \$SrcMachine\C$\temp transactions*.tab | `
    Where-Object {$_.LastWriteTime -ge $LOOKBACKXDAYS } | `
        Select-Object `
            -Property `
                Name , `
                @{name='Size (KB)'; expression={[string]([math]::ceiling($_.Length / [math]::pow(2, 10)))}} , `
                LastWriteTime | `
            Sort-Object `
                -Property `
                    'Size (KB)', `
                    LastWriteTime, `
                    Name | `
                Out-Host
Write-Host -ForegroundColor Green "`n`nAll Done!`n`n"
pause

它产生如下所示的输出:

Name                          Size (KB) LastWriteTime
----                          --------- -------------
transactions for 12172018.tab 13783     2018-12-17 12:05:04
transactions for 12182018.tab 13824     2018-12-18 12:05:06
transactions for 12192018.tab 13869     2018-12-19 12:05:16
transactions for 12202018.tab 13901     2018-12-20 12:05:14
transactions for 12212018.tab 13901     2018-12-21 12:05:12
transactions for 12222018.tab 13931     2018-12-22 12:05:16
transactions for 12232018.tab 13931     2018-12-23 12:05:12
transactions for 12242018.tab 13954     2018-12-24 12:05:14
transactions for 12252018.tab 13954     2018-12-25 12:05:16
transactions for 12262018.tab 14001     2018-12-26 12:05:26

这是修改后的代码,生成小写字母,连字符删除了 MD5 校验和,并且我添加了一个列,如果星期几是周末则显示 'Y',这样我就可以忽略相同的文件。我也改变了一些其他的东西。

$LOOKBACKXDAYS=( Get-Date ).AddDays(-10)
$SrcMachine='TheServerInQuestion'

if( -not ( Test-Path -Path TabFiles:\temp ) ) {
    New-PSDrive `
        -PSProvider FileSystem `
        -Name TabFiles `
        -Root \TheServerInQuestion\C$ `
        -Credential mydomain\administrator
}

Get-ChildItem -Path TabFiles:\temp transactions*.tab | `
    Where-Object {$_.LastWriteTime -ge $LOOKBACKXDAYS } | `
        Select-Object `
            -Property `
                Name , `
                @{name='Size (KB)'; expression={[string]([math]::ceiling($_.Length / [math]::pow(2, 10)))}} , `
                @{name='MD5'; expression={ [System.BitConverter]::ToString($(New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider).ComputeHash([System.IO.File]::ReadAllBytes($_.FullName))).replace('-','').ToLower() }}, `
                @{name='Is Weekend'; expression={ $( if ($_.LastWriteTime.dayofweek -match 'Saturday|Sunday'){return 'Y'} else{return ''} ) } }, `
                LastWriteTime | `
            Sort-Object `
                -Property `
                    'Size (KB)', `
                    LastWriteTime, `
                    Name | `
                Format-Table
Write-Host -ForegroundColor Green "`n`nAll Done!`n`n"
pause

这是上面脚本的输出:

Name                          Size (KB) MD5                              Is Weekend LastWriteTime
----                          --------- ---                              ---------- -------------
transactions for 12172018.tab 13783     e825b1a58954203da9f8a16c454d9441            2018-12-17 12:05:04
transactions for 12182018.tab 13824     4de56734ceacd02db11bdd8a3dcdc628            2018-12-18 12:05:06
transactions for 12192018.tab 13869     1e4401afd2734004da372bfcf1e90395            2018-12-19 12:05:16
transactions for 12202018.tab 13901     49b1c5e29ea4da7e8a7a72bf610cecd6            2018-12-20 12:05:14
transactions for 12212018.tab 13901     49b1c5e29ea4da7e8a7a72bf610cecd6            2018-12-21 12:05:12
transactions for 12222018.tab 13931     36e40a546c6049e550b0feac9aa7adc7 Y          2018-12-22 12:05:16
transactions for 12232018.tab 13931     36e40a546c6049e550b0feac9aa7adc7 Y          2018-12-23 12:05:12
transactions for 12242018.tab 13954     9c97ccf81ce4cbb583fff348a739cc66            2018-12-24 12:05:14
transactions for 12252018.tab 13954     9c97ccf81ce4cbb583fff348a739cc66            2018-12-25 12:05:16
transactions for 12262018.tab 14001     ad18d544c8dea1d3d9cf1512c4a772e4            2018-12-26 12:05:26

周末和平安夜到圣诞节有可忽略的重复数据,但20日和21日有不可原谅的重复数据。

为了回答您的问题,可以从计算的 属性 表达式字段调用可执行文件,因为这对应于标准脚本块。

以下应该有效

$md5exepath = "C:\Program Files\Git\usr\bin\md5sum.exe"
#Make sure you have a few png on your desktop or change the get-childitem so it matches something.
$Items = get-childitem -Path "$($Env:USERPROFILE)\desktop" -Filter '*.png' | Select -First 5

$Items | Select Name,
        @{n='md5';e={(& $md5exepath $_.FullName).split(' ')[0]}},
        @{n='test';e={(& 'cmd' "/c echo test")}}   

假设您可以从 echo 命令中看到 test",那么它确认外部调用也适用于您。

关于 md5sum.exe,验证以下内容。

  • 确保使用 md5sum.exe 的正确路径。
  • 从 windows 命令行尝试 运行:"c:\windows\md5sum.exe" "C:\windows\system32\notepad.exe" 并确保它产生所需的输出并且不抛出任何错误。
  • 以管理员身份尝试运行脚本

我将 md5sum.exe 复制到 Windows 文件夹,结果也是空的。 正如我发现的那样,由于 Powershell 没有产生任何错误,所以在我复制该工具时,我没有复制它的依赖项,因此它失败了。