Solved

Getting Server name in this powershell script

Posted on 2014-10-28
6
333 Views
Last Modified: 2014-10-31
Here is the powershell script:

$TotalGB = @{Name=”Capacity(GB)”;expression={[math]::round(($_.Capacity/ 1073741824),2)}}
$FreeGB = @{Name=”FreeSpace(GB)”;expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
$FreePerc = @{Name=”Free(%)”;expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}

function get-mountpoints {
$volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize
}

$servers = (Get-Content .\servers.txt)

foreach ($server in $servers){
get-mountpoints
}

Open in new window


I want to save the output of this query in csv file. But whenever i try the out command only the last server name in my input file is processed and output is shown. Also I wanted to display the server names as well in the output file which I'm using as input in the servers.txt file to this query.

I'm new to powershell. Please have a look and suggest your views.

Thanks in advance.
0
Comment
Question by:Abhinav Singh
  • 3
  • 2
6 Comments
 
LVL 39

Expert Comment

by:footech
Comment Utility
If you're going to output objects to .CSV using Export-CSV, they can't be sent to any of the Format commands first.  So, something like this wouldn't work.
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize | Export-CSV file.csv
You could just substitute the Export-CSV cmdlet for Format-Table, but then you'd either need to dynamically name the file so that it is not being overwritten each time through the foreach loop, or if you're using PS 3.0+, then you can use the -append parameter with Export-CSV.

I suggest you remove the Format-Table command from your function to make it more flexible.
function get-mountpoints {
$volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc
}

Open in new window

Then you can use either of the following:
@(foreach ($server in (Get-Content .\servers.txt)){
get-mountpoints
}) | Export-CSV file.csv -notype

#or
Get-Content .\servers.txt | ForEach `
{
    $server = $_
    get-mountpoints
} | Export-CSV file.csv -notype

Open in new window

I'm not sure what you're getting at by wanting the server name.  The server name is already in the output by merit of the "SystemName" property.
0
 

Author Comment

by:Abhinav Singh
Comment Utility
Hey footech,

Thanks for your reply.
Actually in "SystemName", I'm getting the Name on which my SQL physical instances are hosted. Thta's the reason I wanted to include server name in my output.
Anways, I found an answer to this question in this forum itself.

Can you please resolve one more query?
I wanted to display only those mointpoints, which have capacity more than 0gb.
I'm currently using this piece of code

foreach ($server in $servers){

if ($_.”Capacity(GB)” -ne 0) {
get-mountpoints
}

}

Open in new window


But it's not working. Please look into this.

Thanks
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
SystemName comes from the output of the Get-WmiObject command, so it should always reflect the server that command was run against (i.e. should be the same as $server).  Since you said you have an answer for that though I would dwell on it further.

You can just change this line in the function to filter on capacity.
$volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null -and $_.Capacity -gt 0}

Open in new window

0
 

Author Comment

by:Abhinav Singh
Comment Utility
I tried your above statement, but it's not working.
Still showing volumes with zero capacity.
Kindly suggest some alternatives.

Also will it be possible for you to modify the script so that I can get disk space information of the physical drives present on the servers (D:, E:, etc.) except C: Drive. SInce it is not monitored by our team.

Thanks in advance
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
After changing that line you will need to run the code again to recreate the function.  I've tested and it's working good.

If you want other drives, just change the Where-Object condition.  The following should probably work for you.
$volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -ne "C:\" -and $_.Capacity -gt 0}

Open in new window

0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

744 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

9 Experts available now in Live!

Get 1:1 Help Now