Solved

PowerShell

Posted on 2014-01-04
11
445 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
Comment Utility
Please try Send-mailmessage.

Please check this link
0
 

Author Comment

by:AXISHK
Comment Utility
Any idea for the message displays when running hte powershelll above ? Tks
0
 
LVL 38

Expert Comment

by:Vasil Michev (MVP)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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
Comment Utility
$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
Comment Utility
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
Comment Utility
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
Comment Utility
Tks.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

17 Experts available now in Live!

Get 1:1 Help Now