Solved

PowerShell

Posted on 2014-01-04
11
453 Views
Last Modified: 2014-01-11
I have encountered the following problem running a powershell script. I have already changed the execution enviornment "set-executionpolicy unrestricted". Any idea ?

Is it possible to email the output to adminstrator, rather than displaying it on the screen ? Tks

Import-csv computer.csv | foreach {Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_ -Filter "DeviceId='C:'" } |
Format-Table SystemName, @{Name="Free";
Expression={[math]::round($($_.FreeSpace/1GB), 2)}} -auto




PS C:\scripts\chkdisk> .\chkdisk.ps1
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\scripts\chkdisk\ChkDisk.ps1:1 char:49
+ Import-csv computer.csv | foreach {Get-WmiObject <<<<  -Class Win32_LogicalDi
sk -ComputerName $_ -Filter "DeviceId='C:'" } |
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand

PS C:\scripts\chkdisk>
0
Comment
Question by:AXISHK
11 Comments
 
LVL 11

Expert Comment

by:Miftaul
ID: 39755769
Please try Send-mailmessage.

Please check this link
0
 

Author Comment

by:AXISHK
ID: 39755773
Any idea for the message displays when running hte powershelll above ? Tks
0
 
LVL 40

Expert Comment

by:Vasil Michev (MVP)
ID: 39755915
Lots of possible causes, including but not limited to:

- firewall or AV is blocking the connection
- WMI services are disabled
- registry settings/policies prevent remote execution

Check this against a single computer and start troubleshooting with the firewall.

As for emailing the result, there are lot of sample scripts available, just do a quick search.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 69

Expert Comment

by:Qlemo
ID: 39756238
AFAIK the Import-CSV isn't what you should do here, and probably wrong. You are importing data as an object with named properties that way, but only use the object as whole as -ComputerName parameter. If the remote machine is not reachable, because the IP address is wrong, the name not resolveable, or any other reason, the RPC error will occur. So we first have to make sure you use the computername as such.

In regard of sending the results as email I (too) recommend Send-MailMessage, but won't send the Format-Table result. Instead you should create HTML tables for correct formatted output. Format-Table only works well on the console.
$body = Get-WmiObject Win32_LogicalDisk -ComputerName (get-content computer.csv) -Filter "DeviceId='C:'" } |
  select SystemName, @{Name="Free";
Expression={[math]::round($($_.FreeSpace/1GB), 2)}} |
  ConvertTo-Html
Send-MailMessage -SmtpServer mail.company.com -From me@company.com -To you@company.com -Subject 'WMI Results' -Body $body -BodyAsHtml

Open in new window

0
 

Author Comment

by:AXISHK
ID: 39768184
Still doesn't work. "You are importing data as an object with named properties that way, but only use the object as whole". What does you mean ? What's the difference between my previous CSV import with that of yours ?  Tks again.

PS C:\scripts\chkdisk> $body = Get-WmiObject Win32_LogicalDisk -ComputerName (ge
t-content computer.csv) -Filter "DeviceId='C:'"  | select SystemName, @{Name="Fr
ee";Expression={[math]::round($($_.FreeSpace/1GB), 2)}} | ConvertTo-Html Send-Ma
ilMessage -SmtpServer exdag.abc.com -From DC02@abc.com -To itadmin@abc.com -Subject 'Disk Space' -Body $body -BodyAsHtmlml
ConvertTo-Html : A parameter cannot be found that matches parameter name 'SmtpS
erver'.
At line:1 char:239
+ $body = Get-WmiObject Win32_LogicalDisk -ComputerName (get-content computer.c
sv) -Filter "DeviceId='C:'"  | select SystemName, @{Name="Free";Expression={[ma
th]::round($($_.FreeSpace/1GB), 2)}} | ConvertTo-Html Send-MailMessage -SmtpSer
ver <<<<  exdag.abc.com -From DC02@abc.com -To simon.mh.leung@abc.com
-Subject 'Disk Space' -Body $body -BodyAsHtmlml
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-Html], Parameter
   BindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm
   ands.ConvertToHtmlCommand
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39768456
There is a line break after ConvertTo-HTML, which seems to be missing in your command.

----

The difference between an object and its properties is simple. Firstly, you need to know that  Import-CSV always generates an object with particular properties (which are taken from the first line of the CSV file). If your Computers.CSV file looks like this:
Computer,Data1,Data2
pc1,d11,d12
pc2,d21,d22

Open in new window

the command $obj = import-csv Computers.CSV creates an object collection in $obj consisting of two objects, which again have properties "Computer", "Data1" and "Data2". You access "d11" with $obj[0].Data1 then.
Get-Content, however, will just result in 3 strings, each one containing one of the lines as a whole.
0
 

Author Comment

by:AXISHK
ID: 39771468
Still doesn't work... any idea ??

PS C:\scripts\chkdisk> .\ChkDisk.ps1
Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String'
 required by parameter 'Body'. Specified method is not supported.
At C:\scripts\chkdisk\ChkDisk.ps1:5 char:114
+ Send-MailMessage -SmtpServer exdag.abc.com.hk -From dc02@abc.com.hk -To it@ab
c.com.hk -Subject 'Disk Usage' -Body <<<<  $body -BodyAsHtml
    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], Paramet
   erBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Comma
   nds.SendMailMessage
ChkDisk.txt
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39771541
$body = Get-WmiObject Win32_LogicalDisk -ComputerName (get-content computer.csv) -Filter "DeviceId='C:'" } |
  select SystemName, @{Name="Free";
             Expression={[math]::round($($_.FreeSpace/1GB), 2)}} |
  ConvertTo-Html
Send-MailMessage -SmtpServer mail.company.com -From me@company.com -To you@company.com -Subject 'WMI Results' -Body ($body -join "`n") -BodyAsHtml

Open in new window

0
 

Author Comment

by:AXISHK
ID: 39772735
Tks. It works great.

computer.csv
10.150.1.30
10.150.1.20

Is it possible to include the disk to be analysed in the server as below ?

computer.csv
10.150.1.30    c:   d:    e:
10.150.1.20    c:   d:
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39773239
Including that requires to change the strategy, because we now have more than one "property" to include, with different meanings. But that now is a new and the third part of the question - you should not extend a question too much, coming with new requests each time. Your new request requires
a) a different design for the input file
b) to change the way the file is read in and data provided for WMI
c) to change the WMI filer or WMI query to allow for more than one drive name
d) to change the output to include the drive.
Usually that amount of effort deserves to be asked as a new question.

The straight-forward approach is to have one drive/pc combination. The file looks like this:
Computer, Drive
10.150.1.30, C:
10.150.1.30, D:
10.150.1.30, E:
10.150.1.20, C:
10.150.1.20, D:

Open in new window

Then we can read it in with Import-CSV Computer.csv, getting objects consisting of "Computer" and "Drive". We will have to stuff that into separate WMI queries, and so the code looks like this now:
Import-CSV Computer.CSV | % {
  Get-WmiObject Win32_LogicalDisk -ComputerName $_.Computer -Filter "DeciceId='$($_.Drive)'" |
  select SystemName, DeviceID, @(n='Free'; e={[math]::round($($_.FreeSpace/1GB), 2)}} |
  ConvertTo-Html | Set-Variable body

Send-MailMessage -SmtpServer mail.company.com -From me@company.com -To you@company.com -Subject 'WMI Results' -Body ($body -join "`n") -BodyAsHtml

Open in new window

0
 

Author Closing Comment

by:AXISHK
ID: 39773260
Tks.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question