我是powershell的新手,所以脚本是其他工作的混合编辑,以满足我的需要。
它在通过ISE运行时非常有效,但是当它通过控制台运行或从脚本调用时它会失败,并且它创建的文本文件为空。
尝试过各种参数,例如no-exit
,-sta
没有运气,文件仍为空白。
$RemoteComputers = "xxxxxx"
Clear-Host
foreach ($Computer in $RemoteComputers) {
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
$computerCPU = get-wmiobject Win32_Processor -Computer $Computer
$computerHDDs = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
$Hddtest = Get-WmiObject -Class Win32_LogicalDisk -computername $Computer -filter drivetype=3
write-output "System Information for: " $computerSystem.Name
"Manufacturer: " + $computerSystem.Manufacturer
"Model: " + $computerSystem.Model
"Serial Number: " + $computerBIOS.SerialNumber
"CPU: " + $computerCPU.Name
"RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory / 1GB) + "GB"
"IP Address: " + $NetworkConfig.IpAddress
"MAC Address:" + $NetworkConfig.MACAddress
"Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
"Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
""
"Installed HDD'S: " + $computerHDDs.Count
foreach ($computerhdd in $computerhdds) {
"HDD Drive Letter: " + $computerhdd.DeviceID
"HDD Capacity: " + "{0:N2}" -f ($computerHDD.Size / 1GB) + "GB"
"Free Space: " + "{0:P2}" -f ($computerHDD.FreeSpace / $computerHDD.Size) +
"Free (" + "{0:N2}" -f ($computerHDD.FreeSpace / 1GB) + "GB)"
"Used Space:" + "{0:N2}" -f ($computerhdd.Size / 1GB - $computerhdd.FreeSpace / 1GB) + "GB"
}
""
"-------------------------------------------------------"
}
Start-Sleep -Milliseconds 1000
$Text = $psise.CurrentPowerShellTab.ConsolePane.text
set-content -path c:\Serverinfo.txt $text
$file = "C:\serverinfo.txt"
$mailsettings = @{
'SmtpServer' = "xxxxx"
'To' = "xxxxx" #, "xxxx"
'From' = " Sxxxxxx>"
'Subject' = "Server System Info"
'Body' = "xxxxxx"
'Attachments' = $file
}
Send-MailMessage @mailsettings
答案 0 :(得分:0)
您可以直接将该输出分配给$psise.CurrentPowerShellTab.ConsolePane.text
变量,而不是使用$text
从控制台输出中获取计算机信息:
$text = foreach ($Computer in $RemoteComputers) {
然后将其与Set-Content -path $file $text
一起使用,以创建包含计算机信息的文件。
您的更新代码只有一些细微的变化:
$RemoteComputers = "computer1","computer2"
$file = "C:\serverinfo.txt"
$text = foreach ($Computer in $RemoteComputers) {
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
$computerCPU = get-wmiobject Win32_Processor -Computer $Computer
$computerHDDs = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
"System Information for: " + $computerSystem.Name
"Manufacturer: " + $computerSystem.Manufacturer
"Model: " + $computerSystem.Model
"Serial Number: " + $computerBIOS.SerialNumber
"CPU: " + $computerCPU.Name
"RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory / 1GB) + "GB"
"IP Address: " + $NetworkConfig.IpAddress
"MAC Address:" + $NetworkConfig.MACAddress
"Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
"Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
""
"Installed HDD'S: " + $computerHDDs.Count
foreach ($computerhdd in $computerhdds) {
"HDD Drive Letter: " + $computerhdd.DeviceID
"HDD Capacity: " + "{0:N2}" -f ($computerHDD.Size / 1GB) + "GB"
"Free Space: " + "{0:P2}" -f ($computerHDD.FreeSpace / $computerHDD.Size) +
"Free (" + "{0:N2}" -f ($computerHDD.FreeSpace / 1GB) + "GB)"
"Used Space:" + "{0:N2}" -f ($computerhdd.Size / 1GB - $computerhdd.FreeSpace / 1GB) + "GB"
}
""
"-------------------------------------------------------"
}
Set-Content -path $file $text
$mailsettings = @{
'SmtpServer' = "xxxxx"
'To' = "xxxxx" #, "xxxx"
'From' = " Sxxxxxx>"
'Subject' = "Server System Info"
'Body' = "xxxxxx"
'Attachments' = $file
}
Send-MailMessage @mailsettings