0
votes

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
1
$psise.CurrentPowerShellTab.ConsolePane.text <-- this - notjustme

1 Answers

0
votes

Rather than using $psise.CurrentPowerShellTab.ConsolePane.text to get the computer information from the Console output, you can assign that output directly to your $text variable:

$text = foreach ($Computer in $RemoteComputers) {

Then use it with Set-Content -path $file $text to create the file with your computer info in.

Your updated code only has minor changes:

$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