Solved

PowerShell

Posted on 2014-01-04
11
457 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A procedure for exporting installed hotfix details of remote computers using powershell
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

837 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