Scripting

Fun with Powershell - Retrieve space utilization from Netapp systems

Alright, so my PowerShell adventures continue and this time, its taken me to the Netapp world. Now I am by no means a Netapp guru or claim to be one. This report was requested by my storage team and I wanted to explore this with PowerShell!

First in order to run any netapp related commands, you need to down the Netapp Tool Kit for Powershell which add a bunch of cmdlets. Click here to download that!

Once that is done, all you need to do is run the below script in PowerShell against your Netapp controller and you should see a neat html report generated in  the location specified in the script. I have also included the CSS to make it look pretty!

I have highlighted the values in bold that you will have to change as per your environment and once done, you should be one your way.

Import-Module DataONTAP

Hello, World!

$controller = Get-Content C:\powershell\Scripts\Netapp\controller.txt

$vservers = Get-Content C:\powershell\Scripts\Netapp\vserver.txt

$report = "\\server1\d$\wamp\www\main.html"

ConvertTo-Html –title "NDMP REPORTS" –body "<H1>NDMP REPORTS</H1>" -head "<link rel='stylesheet' href='http://meassets/css/style.css'>" | Out-File -Encoding unicode $report

ConvertTo-Html –title "NDMP REPORTS" –body "<H4>Date and time     $(get-date)</H4>" | Out-File -Encoding unicode -Append $report

$password = ConvertTo-SecureString -AsPlainText -Force "hello123"

$credential = new-object management.automation.pscredential "admin", $password

$one = foreach($control in $controller)

{

Connect-NcController $control -Credential $credential

ConvertTo-Html –body "<H2> $($control.ToUpper()) </H2>" | Out-File -Encoding unicode -Append $report

ConvertTo-Html -Body "<H4>$(Get-NcSystemVersion)</H4>" | Out-File -Encoding unicode -Append $report

$main = get-NcAggr |Select-Object Name,State,@{n="Total Space in TB";e {[math]::round($_.TotalSize / 1TB,2)}},

@{n="Available Space Remaining in TB";e={[math]::round($_.Available / 1TB,2)}},@{n="Utilized Percentage %";e={($_.Used)}}| ConvertTo-Html | Out-File -Encoding unicode -Append $report

$main2 = get-ncaggr| Measure-Object -Property Available,TotalSize -Sum | select-object @{name="Name";e={$_.Property}},@{name="Size in TB";e={[math]::round($_.sum / 1TB,2)}} | ConvertTo-Html –body "<H2>Summary: </H2>" |  Out-File -Encoding unicode -Append $report

ConvertTo-Html - Body "<br>================================================================================================</br>" | Out-File -Encoding unicode -Append $report

$global:CurrentNcController = $null

}

$one

Send-MailMessage -SmtpServer mail.smtp.com -To  you@yourcompany.com -Subject "AWESOME NDMP REPORTS" -From  you@yourcompany.com -Priority High -Attachments $report

-Alstar

Fun with Powershell - Find serial number of physical servers

I have to do this quite a lot for my physical HP servers, and previously it was quite a time consuming task. So here is a small script which might make things a little easier.

$servers = Get-Content C:\powershell\Scripts\SerialNumber\servers.txt

foreach ($server in $servers) {
Get-WmiObject win32_bios -ComputerName $server -ErrorAction SilentlyContinue                         -ErrorVariable ProcessError | Select-Object pscomputername,serialnumber | Sort-Object PSComputername 
if($ProcessError) {
Write-Warning -Message "Something went wrong, with the server $server!!"
}
}

The good  thing about this is that, its got error handling in place. So if a script cannot find the information of a server, it will display a warning message with the server name, instead of spitting about that ugly red message in powershell by default.

-Alstar

Fun with Powershell - Random name picker!

This was fun to make, so we had a situation where a task was meant to be done and no one openly volunteered for it. (not even me!). So they decided to pick a representative for the work, but I wanted to make it fair and REALLY random. So I wrote up a script and had the system pick a random team member to be assigned with the task!. This way no one could be blamed but the computer itself... (The computer didn't pick me! and NO I did not manipulate the code..)

What you need to do is use the GET-RANDOM function in powershell and work work with variables, its pretty self explanatory.

$array = "Batman","Ironman","Blackwindow","Hulk","Neo"

$random = Get-Random $array
Write-Host "The random person selected for this task is: $($random.ToUpper()) !!" -ForegroundColor Green

 -Alstar

Fun with Powershell - Calculate the size of a given folder

This is a pretty awesome script, what it does is that it asks the user for a folder path they want to calculate the size for and once they key that in, it goes and does what it needs to.(Calculate the size!!)

I had seen  this concept on a MSDN page, however I have extended that base concept into something more interactive. 

Have fun and remember be careful!

$startFolder = Read-Host "Enter the folder path you want to calculate"

write-host "`n"
Write-Host "Calculating the size of the folder $startFolder" -ForegroundColor Green
write-host "`n"
Write-Host "Size of $startFolder" -ForegroundColor Green
write-host "==============="
write-host "`n"
if (-not(Test-Path $startFolder)){
Write-Host "The folder path you have entered is invalid!" -ForegroundColor Red
}
else{
$collection = (Get-ChildItem $startFolder -Recurse| Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($collection.sum / 1MB) + " MB"
write-host "`n"
Write-Host "Size of all sub-folders under $startFolder" -ForegroundColor Green
write-host "====================================="
$collection = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $collection)
    {
        $subFolderItems = Get-ChildItem $i.FullName | Measure-Object -property length -sum
        $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    }
write-host "`n"
}

-Alstar

Fun with Powershell - Stopping services remotely

Powershell is something can you get addicted to very fast, if you know what  you are doing! So this morning I was messing around with the "read-host" cmdlet and I wanted to try something with services. I wanted to ask the user what service he/she wanted to restart and on what computer. After that I wanted to go ahead and restart that service, assuming you have the privileges on the remote machine and then display a status message. Its not a ground breaking script but something to do for fun! (Well, don't go about restarting services in your production environment now, just coz I have mentioned the "fun" bit!)

You can extend the below script by checking if the user has entered a valid service name or a valid computer name and if not you can display a warning message. I leave that part to you and if you do extend it, please comment below and let me know!

$service = read-host "What is the service you want to STOP?"
$computer = read-host "What is the server name you want to STOP this service on?"
Get-Service $service -ComputerName $computer | Stop-Service -Force
Get-Service $service -ComputerName $computer
$svc = Get-Service -ComputerName $computer -Name $service
write-host "`n"
if ($svc.Status -eq "Stopped"){
Write-Host "Looks like you have stopped the service, using the $env:userdomain\$env:username account" -ForegroundColor Green
}
else
{
write-host "SORRY, the service could not be stopped :-("
}
-Alstar