PFX/软件证书无法正常运行

PFX / Software Certificate Not Functioning Correctly

我有一个由另一个州构建和测试的脚本,目的是扫描我们域中每台计算机上的特定目录,查找 .pfx 文件,然后将它们复制到存储目录,同时记录行动,每月一次。从那里,我们可以在管理方面检查证书,看看我们是否需要保留它。

所以脚本应该做三件基本的事情:

  1. 在目录中查找 .pfx 文件。
  2. 将任何文件复制到存储目录。
  3. 在月度报告中记录所有这些操作。

它没有做的是复制文件或记录文件。我查看了脚本,然后探索了该网站上的其他内容,但我似乎无法弄清楚我在做什么,或者这个脚本做错了。我需要它来复制文件并写入指定位置,就像它在 运行 的其他状态下所做的那样。脚本如下,省略了某些敏感信息。

$computers = Get-ADComputer -Filter * -SearchBase "OU=CAC Not Required,OU=DESKTOPS,OU=WORKSTATIONS,OU=WIN10,OU=,OU=,DC=,DC=,DC=,DC="
$destination = "\NGVT-SA7V-02\Software - Patching Logs\Soft Cert Clean"

foreach ($computer in $computers){

    $client = $computer.name

    if (Test-Connection -ComputerName $client -Count 1 -ErrorAction SilentlyContinue) {

            $outputdir = "$destination$client"
            $ext = "*.pfx"
            $filerepo = "$outputdir\Files"
            $files = Get-ChildItem -Path \$client\c$\Users -Filter $ext -Recurse -ErrorAction SilentlyContinue

                if (!$files){

                    Write-Host -ForegroundColor Green "There are no .pfx files on $client."
                }

                else {

                    if (Test-Path -Path $outputdir) {

                        Write-Host -ForegroundColor Cyan "PFX files found on $client"
                        Write-Host -ForegroundColor Gray "Output directory exists for $client"

                        $files | Select fullname, length | Out-File $outputdir\PFX_List.txt
                        $files | Copy-Item -Destination $filerepo -ErrorAction SilentlyContinue

                        Write-Host -ForegroundColor Cyan "Files moved to share server for $client."

                    }

                    else {

                        Write-Host -ForegroundColor Cyan "PFX files found on $client"

                        New-Item -ItemType Directory $outputdir -Force | Out-Null
                        New-Item -ItemType Directory $filerepo -Force | Out-Null

                        Write-Host -ForegroundColor Yellow "Output directory and File repo created for $client"

                        $files| Select fullname, length | Out-File $outputdir\PFX_List.txt
                        $files | Copy-Item -Destination $filerepo -ErrorAction SilentlyContinue 

                        Write-Host -ForegroundColor Cyan "Files moved to share server for $client."

                    }
                }
    }
    else {
        Write-Host -ForegroundColor Red "$client is not online."
    }    
}

虽然我显然没有您的测试环境,但以下代码可能有效。我使用了@Theo 关于目录创建的建议。 New-Item cmdlet 将创建中间目录,因此无需调用它两次。

$computers = Get-ADComputer -Filter * -SearchBase "OU=CAC Not Required,OU=DESKTOPS,OU=WORKSTATIONS,OU=WIN10,OU=,OU=,DC=,DC=,DC=,DC="
$destination = "\NGVT-SA7V-02\Software - Patching Logs\Soft Cert Clean"
$ext = "*.pfx"

foreach ($computer in $computers) {
    $client = $computer.name

    if (Test-Connection -ComputerName $client -Count 1 -ErrorAction SilentlyContinue) {
        $outputdir = "$destination$client"
        $filerepo = "$outputdir\Files"
        $files = Get-ChildItem -Path \$client\c$\Users -Filter $ext -Recurse -ErrorAction SilentlyContinue

        if (!$files) {
            Write-Host -ForegroundColor Green "There are no .pfx files on $client."
        } else {
            Write-Host -ForegroundColor Cyan "PFX files found on $client"

            if (-not (Test-Path -Path $filerepo)) {
                New-Item -ItemType Directory $filerepo -Force | Out-Null
                Write-Host -ForegroundColor Yellow "Output directory and File repo created for $client"
            }

            $files | Select fullname, length | Out-File $outputdir\PFX_List.txt
            $files | Copy-Item -Destination $filerepo -ErrorAction SilentlyContinue

            Write-Host -ForegroundColor Cyan "Files moved to share server for $client."
        }
    } else {
        Write-Host -ForegroundColor Red "$client is not online."
    }
}