我有一个脚本是由另一种状态构建和测试的,目的是扫描域中每台计算机上的某个目录,查找.pfx文件,然后在记录操作时将它们复制到存储目录中,每月一次。从那里,我们可以在管理方面检查证书,看看是否需要保留它。
因此该脚本应该执行三项基本操作:
它没有做的是复制文件或对其进行日志记录。我浏览了该脚本,然后浏览了该网站上的其他内容,但似乎无法弄清楚我的意思,否则该脚本做错了。我需要它来复制文件并将其写入指定的位置,就像在其他运行状态中一样。该脚本在下面,省略了某些敏感信息。
$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\10 - 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."
}
}
答案 0 :(得分:0)
虽然我显然没有要在其中进行测试的环境,但是以下代码可能有效。我使用@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\10 - 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."
}
}