Solved

PowerShell

Posted on 2014-01-04
11
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 42

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
Ready to trade in that old firewall?

Whether you need to trade-up to a shiny new Firebox or just ready to upgrade from whatever appliance you're using now, WatchGuard has the right appliance for you! Find your perfect Firebox today with appliance sizing tool!

 
LVL 70

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 70

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 70

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 70

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

What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

626 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