I am a novice with powershell so the script is a mixture of others work edited to suit my needs.
It works perfectly when run through the ISE, but when it is run through the Console or called from the script it fails and the text file it creates is empty.
Have tried various parameters like no-exit, -sta with no luck, the file is still blank.
$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