I’m still pretty new to powershell and practical implementations of it… but I created a script to monitor disk health and have some very bizarre results on 2012 R2. Wondering if anyone has seen the likes of this and/or has a solution… Or is it a bug?
The script in question is:
$OutputFile=$env:temp + "\~VMRepl-Status.txt"
get-vmreplication | Out-File -FilePath $OutputFile
measure-vmreplication | Out-File -FilePath $OutputFile -Append
Get-PhysicalDisk | Sort Size | FT FriendlyName, Size, MediaType, Manufacturer, Model, HealthStatus, OperationalStatus -AutoSize | Out-File -FilePath $OutputFile -Append
Get-PhysicalDisk | Get-StorageReliabilityCounter | ft deviceid, temperature, wear -AutoSize | Out-File -FilePath $OutputFile -Append
$body=(Get-Content $OutputFile | out-string)
Send-MailMessage -To [removed] -From [removed] -body $body -SmtpServer 10.24.42.45 -subject "Hyper-V Replica and Disk Disk Status"
I execute the above powershell script using a two line batch file:
@echo off Powershell.exe -executionpolicy remotesigned -File c:\Windows\scripts\DailyReplicaStatusUpdate.ps1
If I execute the powershell script from a command line or by right clicking and running as administrator, I get:
FriendlyName Size MediaType Manufacturer Model HealthStatus OperationalStatus ------------ ---- --------- ------------ ----- ------------ ----------------- PhysicalDisk15 119185342464 SSD INTEL SS DSC2BW120A4 Healthy OK PhysicalDisk3 119185342464 SSD INTEL SSDSC2BW120A4 Healthy OK PhysicalDisk0 1000203804160 UnSpecified SAMSUNG HD103SI Healthy OK PhysicalDisk9 1499480457216 UnSpecified ST315003 41AS Healthy OK PhysicalDisk14 1499480457216 UnSpecified ST315003 41AS Healthy OK PhysicalDisk2 1999575711744 HDD TOSHIBA DT01ACA200 Healthy OK PhysicalDisk1 1999575711744 HDD ST2000DL003-9VT166 Healthy OK PhysicalDisk6 1999575711744 HDD WDC WD20 EARS-00MVWB0 Healthy OK PhysicalDisk4 1999575711744 HDD ST2000DL 003-9VT166 Healthy OK PhysicalDisk11 1999575711744 UnSpecified ST320005 42AS Healthy OK PhysicalDisk21 2000398934016 UnSpecified Seagate Desktop Healthy OK PhysicalDisk10 2999766220800 UnSpecified WDC WD30 EZRX-00DC0B0 Healthy OK PhysicalDisk5 2999766220800 UnSpecified TOSHIBA DT01ACA300 Healthy OK PhysicalDisk7 2999766220800 UnSpecified TOSHIBA DT01ACA300 Healthy OK PhysicalDisk12 2999766220800 HDD ST3000DM 001-1CH166 Healthy OK PhysicalDisk13 2999766220800 HDD ST3000DM 001-1CH166 Healthy OK PhysicalDisk8 2999766220800 HDD ST3000DM 001-9YN166 Healthy OK PhysicalDisk22 3000592977920 UnSpecified Seagate Expansion Desk Healthy OK
But if I initiate it from a scheduled task, I get:
FriendlyName Size MediaType Manufacturer Model Healt hStat us ------------ ---- --------- ------------ ----- ----- PhysicalDisk15 119185342464 SSD INTEL SS DSC2BW120A4 He... PhysicalDisk3 119185342464 SSD INTEL SSDSC2BW120A4 He... PhysicalDisk0 1000203804160 UnSpecified SAMSUNG HD103SI He... PhysicalDisk9 1499480457216 UnSpecified ST315003 41AS He... PhysicalDisk14 1499480457216 UnSpecified ST315003 41AS He... PhysicalDisk2 1999575711744 HDD TOSHIBA DT01ACA200 He... PhysicalDisk1 1999575711744 HDD ST2000DL003-9VT166 He... PhysicalDisk6 1999575711744 HDD WDC WD20 EARS-00MVWB0 He... PhysicalDisk4 1999575711744 HDD ST2000DL 003-9VT166 He... PhysicalDisk11 1999575711744 UnSpecified ST320005 42AS He... PhysicalDisk21 2000398934016 UnSpecified Seagate Desktop He... PhysicalDisk10 2999766220800 UnSpecified WDC WD30 EZRX-00DC0B0 He... PhysicalDisk5 2999766220800 UnSpecified TOSHIBA DT01ACA300 He... PhysicalDisk7 2999766220800 UnSpecified TOSHIBA DT01ACA300 He... PhysicalDisk12 2999766220800 HDD ST3000DM 001-1CH166 He... PhysicalDisk13 2999766220800 HDD ST3000DM 001-1CH166 He... PhysicalDisk8 2999766220800 HDD ST3000DM 001-9YN166 He... PhysicalDisk22 3000592977920 UnSpecified Seagate Expansion Desk He...
I'd love to know how to get it to stop truncating the lines and allow me to get the actual health status - full text - without losing any other information along the way (I COULD drop the size or something else and shorten each line's length).
At the end of the day, the goal is to get the output like I get from running it on a command line rather than the output I'm getting when it runs via the Task Scheduler.
Alright, your problem is the use of the
Out-String
cmdlet. It has a default value of 80, though you can change that by defining a different width, with the-Width
switch. Looks like you're 25 characters over, so you should be able to fix it by changing line 6 to:The console window has a different width when run from the scheduled task (if you could see the window it would look more like a standard black command prompt than the blue Powershell window you're used to seeing).
This matters because you're using
ft
(Format-Table
), which is typically used for output to a screen only.You can resize the console window using
$host.Console.RawUI
and its properties but I do recommend not using aFormat-
command even though it may be easier in this instance. Also this will not work in ISE.Instead, format the strings yourself for the email message.