AXISHK
asked on
PowerShell
Any idea what's wrong for the script below ? Is it possible to disable the warning when I run the script ? Tks
$body = Get-WmiObject Win32_LogicalDisk -filter "DriveType=3' -Computer (Get-Content .\computer.csv) | select SystemName, DeviceID, @{Name="Free (%)";Expression={"{0:N0}" -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }}, @{Name="Size (GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name=" Free (GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | ConvertTo-Html
Send-MailMessage -SmtpServer exdag.abc.com.hk -From dc02.abc.com.hk -To admin@abcl.com.hk -Subject 'Disk Space' -Body ($body -join "`n") -BodyAsHtml
DiskSpace.png
$body = Get-WmiObject Win32_LogicalDisk -filter "DriveType=3' -Computer (Get-Content .\computer.csv) | select SystemName, DeviceID, @{Name="Free (%)";Expression={"{0:N0}" -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }}, @{Name="Size (GB)";Expression={"{0:N1}"
Send-MailMessage -SmtpServer exdag.abc.com.hk -From dc02.abc.com.hk -To admin@abcl.com.hk -Subject 'Disk Space' -Body ($body -join "`n") -BodyAsHtml
DiskSpace.png
The error is in the very first double quote. Change that to a tick (single quote), and all should be fine. You can also change the first tick to a double quote - doesn't matter which one you take (here), but they need to match.
Ok so I updated the script I gave you originally I see it was creating multiple html files:
I added an option to verify access first then run the wmi query )
I added an option to verify access first then run the wmi query )
$ErrorActionPreference= 'silentlycontinue'
$servers = @()
(Get-Content .\serverlist.txt) | % {
$error.clear()
$gwmival = Get-WmiObject Win32_LogicalDisk -computer $_
if ($error -ne $null)
{write-host "$_ failed with errors" -fore RED}
else {$servers += $_; }
}
$servers | out-file validlist.txt
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer (gc validlist.txt) | Select SystemName,DeviceID, @{Name=”Free (%)”;Expression={“{0:N0}” -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }}, @{Name=”Size (GB)”;Expression={“{0:N1}” -f($_.size/1gb)}},@{Name=”Free (GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} | ConvertTo-Html
Send-MailMessage -SmtpServer exdag.abc.com.cn -From dc02@abc.com.cn -To cngit-admin@abc.com.cn -Subject 'Servers Disk Space Analysis' -Body ($body -join "`n") -BodyAsHtml
becraig,
Don't want to be disrespectful, but your code has several design flaws, is overly complicated, and performing bad because of some work done more than once. It will work, though.
Viewing this question isolated, you seem to miss the point here - there was a syntax error, no WMI error. Of course WMI issues will spit out error messages, but again the script would still work for servers not reporting errors.
AXISHK,
To make sure you get what I told you as correction, and further to show good code formatting style:
Don't want to be disrespectful, but your code has several design flaws, is overly complicated, and performing bad because of some work done more than once. It will work, though.
Viewing this question isolated, you seem to miss the point here - there was a syntax error, no WMI error. Of course WMI issues will spit out error messages, but again the script would still work for servers not reporting errors.
AXISHK,
To make sure you get what I told you as correction, and further to show good code formatting style:
$body = Get-WmiObject Win32_LogicalDisk -filter 'DriveType=3' -Computer (Get-Content .\computer.csv) |
select SystemName, DeviceID,
@{Name="Free (%)"; Expression={"{0:N0}" -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }},
@{Name="Size (GB)"; Expression={"{0:N1}" -f($_.size/1gb)}},
@{Name="Free (GB)"; Expression={"{0:N1}" -f($_.freespace/1gb)}} |
ConvertTo-Html
Send-MailMessage -SmtpServer exdag.abc.com.hk -From dc02.abc.com.hk -To admin@abcl.com.hk `
-Subject 'Disk Space' -Body ($body -join "`n") -BodyAsHtml
No disrespect.
I accept the flaw in the additional overhead, I just whipped it together as a way to run the code and generate a clean server list for the OP.
Could you highlight for me as a part of my own learning what the other design flaws are, I am very new at this and excited to learn from those who know everything about PS.
That being said I check against ~800 servers for execution time just to see how much overhead was added and it was negligible, which was why I went ahead and posted it.
Thanks again for the critique.
I accept the flaw in the additional overhead, I just whipped it together as a way to run the code and generate a clean server list for the OP.
Could you highlight for me as a part of my own learning what the other design flaws are, I am very new at this and excited to learn from those who know everything about PS.
That being said I check against ~800 servers for execution time just to see how much overhead was added and it was negligible, which was why I went ahead and posted it.
Thanks again for the critique.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks !!!!
I did this in notepad and was not really paying attention to indenting :(
On the second note DEFINITELY (I really had not looked at it from that perspective but that improves performance :~) )
As for the reason to put the list together, the user had previously asked (on another question) about servers that have access denied errors.
The second point for sure I did not think about in whipping this together (GREAT CATCH)
You're awesome dude...
I did this in notepad and was not really paying attention to indenting :(
On the second note DEFINITELY (I really had not looked at it from that perspective but that improves performance :~) )
As for the reason to put the list together, the user had previously asked (on another question) about servers that have access denied errors.
The second point for sure I did not think about in whipping this together (GREAT CATCH)
You're awesome dude...
ASKER
Tks. I still have problem schedule it under Window. If I test the script under DOS, it will asked me to reply with "R" before execution. Is it possible to run the script directly ?
Tks
ChkDisk.png
Tks
ChkDisk.png
You can simply add
Set-executionpolicy bypass
In the first like of your script.
Set-executionpolicy bypass
In the first like of your script.
Better to switch off security for this call only (Set-ExecutionPolicy is a permanent change):
powershell -ExecutionPolicy Unrestricted -file ...
powershell -ExecutionPolicy Unrestricted -file ...
ASKER
Still receive the prompt. Actually, I have already set the "set-ExecutionPolicy" to "Unrestricted" but it doesn't help..
Tks
PowerShell.png
Tks
PowerShell.png
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The schedule powershell can' t run. I have attached a error message for your reference.
Tks
Task-Scheduler.png
Tks
Task-Scheduler.png
ASKER
Tks
https://www.experts-exchange.com/questions/28412669/Powerscript.html
If I recall the requirement was the same:
To not capture the error on servers you did not have access to then send only the correct info in the email:
Open in new window