首页 > 解决方案 > Invoke-Command 后的 Get-ChildItem 在 Windows 7 上不起作用

问题描述

我在 PowerShell 上有一个脚本,可以在少数 Windows 7 和 Windows 10 计算机上远程调用命令。

远程运行的命令基本上会在 C:\ 分区内的目录上创建几个文件,并在完成调用我正在使用的命令Get-ChildItemWhere-Object选择特定文件之后。

例子:

$ComputerArray = @("win10-01", "Win10-02", "win7-01", "WIn7-02")

foreach ($comp in $ComputerArray) {
    Invoke-Command -ComputerName $comp -ScriptBlock {
        cd c:\temp
        New-Item test.txt
    }

    $file = Get-ChildItem \\$comp\C$\temp |
            Where-Object {$_.Name -match "test"}

    if ($file -eq $null) {
        'there is no file such as test'
    } else {
        $file
    }
}

在 Windows 10 机器上,有一个结果,它告诉我有一个“test.txt”文件,在 Windows 7 上它显示“没有诸如 test 之类的文件”。

不用说,我所有的机器都在运行 PowerShell 5.1。

标签: powershell

解决方案


推荐阅读