我的问题是,我不确定我是否写对了,因为我在 excel 中遇到了错误,没有输出。你能告诉我我哪里搞错了吗?
以下是我尝试编写的脚本。我需要提取以下信息:
- 上下状态(使用 w32tm 实现)
- 服务器列表
- 向上/向下状态(当它站立时)
- 建成时间
- 最后更新文件夹
- 服务器证书未过期
- 检查驱动器是否已满
Import-Module activedirectory
Import-Module psremoteregistry
# Set your Active Directory filter
$searchbase = Get-AdComputer -Filter * -SearchBase "(redacted ou here)" |ft
# Define an array to store server information
$server = @()
#servers from AD
$serversfromAD = Get-ADComputer -filter * -searchbase "(redacted ou here)" -properties Name, operatingsystem, operatingsystemservicepack, whenCreated, lastlogon, lastLogonTimestamp, lastlogondate
# Iterate through the list of servers
foreach ($server in $serversFromAD) {
$serverData = New-Object PScustomObject -Property @{
"ServerName" = $server.Name
"OperatingSystem" = $server.OperatingSystem
"InstallDate" = (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $server.Name).InstallDate
"LastReboot" = [System.DateTime]::FromFileTime($server.LastLogonTimestamp)
"ServerStatus" = ( (get-service -Name W32Time -ComputerName $server.name).Status -ne "Running")
"TrellixDate" = (get-itemproperty "C:\Program Files\Common Files\McAfee\Engine\AMCoreUpdater").lastwritetime -le ((get-date).adddays(-2))
}
}
$report += $temp
$csvfile="C:\Scripttest\AD-All-Computers.csv"
$report | export-csv -notypeinformation $csvfile
"Wrote file for All Computers"
答案1
您列出的错误意味着您无法连接到服务器。几乎任何连接问题都会发生这两种错误,即使您只是使用了错误的计算机名称。
您可以先通过基本连接测试来捕获这些问题。以下是一个例子,您只需将属性设置为“不可用”,而不是完全跳过服务器:
# Iterate through the list of servers
$report = foreach ($server in $serversFromAD) {
# query remote server for properties
try {
Test-Connection $server.name -ErrorAction Stop
$serverData = New-Object PScustomObject -Property @{
"ServerName" = $server.Name
"OperatingSystem" = $server.OperatingSystem
"InstallDate" = (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $server.Name).InstallDate
#...
}}
# but if connection fails, set properties to "Not Available" instead
catch {
$serverData = New-Object PScustomObject -Property @{
"ServerName" = $server.Name
"OperatingSystem" = $server.OperatingSystem
"InstallDate" = 'NotAvailable'
# etc. for other properties
}}
return $ServerData
}
$report | Format-Table
+请注意,"TrellixDate"
您进行的检查是在本地计算机上进行的,而不是在服务器上进行的