Solved

PowerShell

Posted on 2014-01-04
11
450 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 39

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
 
LVL 68

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
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 68

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 68

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 68

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

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.

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…
A procedure for exporting installed hotfix details of remote computers using powershell
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now