我正在尝试从我的计算机在多个服务器上运行脚本来收集每日指标报告。由于添加服务器的需求,我需要将其打开,以便该组由 ou 过滤。我正在努力将从每台服务器提取的所有卷信息放入下面生成的 CSV 文件中。
我将不胜感激您能提供的任何帮助!我还需要将所有数据输出导入到每台服务器上,并将其合并到一个 Excel 电子表格中。仍在学习编写和导航脚本,请不要苛刻,哈哈
import-module ActiveDirectory
# Select the servers you want to hit
$servers = (Get-ADComputer -SearchBase "OU=OUGROUP" -filter *).name
$scriptblock = {
# Set the server name
$serverName = $env:computername
# Get the up/down status of the server
$pingResult = Test-Connection -ComputerName $serverName -Count 1
$status = if ($pingResult.StatusCode -eq 0) { "Up" } else { "Down" }
# Get the server build date
$os = Get-WmiObject -Class Win32_OperatingSystem
$buildDate = $os.InstallDate
# Get the server certificate validity
$cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.PSIsContainer -eq $false } | Sort-Object -Property NotBefore | Select-Object -First 1
$validFrom = $cert.NotBefore
$validTo = $cert.NotAfter
# Check for full drives (this is still having issues) I cant seem to get the information to populate in the CSV file.
$DriveSize = get_volume
#Check Trellix update
$TrellixDate = [datetime](get-itemproperty "C:\Program Files\Common Files\McAfee\Engine\AMCoreUpdater\amupdate.dat").lastwritetime.DateTime
# Create a custom object with the collected information
$serverInfo = [PSCustomObject]@{
ServerName = $serverName
Status = $status
BuildDate = $buildDate.datetime
CertificateValidFrom = $validFrom
CertificateValidTo = $validTo
FullDrives = $DriveSize | -list
TrellixLastUpdate = $TrellixDate
}
# Export the information to a CSV file
$serverInfo | Export-Csv -Path "\temp\ServerInfo.csv" -NoTypeInformation
}
Write-Host "Server information exported to ServerInfo.csv"
#command to run the script on the servers
foreach ($server in $servers){
invoke-command -computername $server -ScriptBlock $scriptblock
}
我可以获得除卷信息之外的所有输出,以按照我想要的方式填充。以上是我当前的工作脚本(一些识别信息已编辑)
您可以使用任务计划程序从 AD 运行脚本。
这是磁盘缺失的部分:
Import-Module activedirectory
foreach($server in Get-ADComputer -Filter "*") {
Write-Host $server.Name
foreach ($disk in (Get-WmiObject -ComputerName $server.Name -Class Win32_LogicalDisk -Filter "DriveType = 3")) {
$diskLabel = $disk.DeviceID
$freeSpace = $disk.FreeSpace / 1GB
$diskSize = $disk.Size / 1GB
$percent = $freeSpace / $diskSize * 100
#just use these variables for your csv.
Write-Host $diskLabel " Free: " $freeSpace "GB | Full size: " $diskSize "GB | Percent: " $percent "%"
#TODO your code...
}
}
欢迎询问。如果有帮助请采纳答案。