Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Friday, 31 July 2015

Powershell - NetApp Volumes, Snapshots and Dedup

Some basic scripts that helped we get some sizing information.

First of all you'll need Powershell (who knew?!) and then the NetApp Powershell Toolkit - I found this link useful for this:

First script get the volume information with a loop due to having a number of Filers to get information on:

Import-Module DataOnTap

$filers = (Get-Content C:\Script_Output\Filers.txt)

Foreach ($filer in $filers){

Connect-NAController $filer -Credential <account-name>

Get-NaVol | Select @{Name="HostName";Expression={$filer}}`
,@{Name="VolumeName";Expression={$_.name}}`
,@{Name="TotalSize(GB)";Expression={[math]::Round([decimal]$_.SizeTotal/1gb,2)}}`
,@{Name="AvailableSize(GB)";Expression={[math]::Round([decimal]$_.SizeAvailable/1gb,2)}}`
,@{Name="UsedSize(GB)";Expression={[math]::Round([decimal]$_.SizeUsed/1gb,2)}}`
,PercentageUsed | Export-CSV C:\Script_Output\filers-vol-size.csv -Append -NoTypeInformation
}


Second script lists the snapshots per volume on each Filer:

Import-Module DataOnTap

$filers = (Get-Content C:\Script_Output\Filers.txt)

Foreach ($filer in $filers){
Connect-NAController $filer -Credential <account-name>
get-navol | get-nasnapshot | Select @{Name="HostName";Expression={$filer}}`
,@{Name="VolumeName";Expression={$_.TargetName}}`
,@{Name="SnapshotName";Expression={$_.Name}}`
,@{Name="CumlativeTotal(GB)";Expression={[math]::Round([decimal]$_.CumulativeTotal/1gb,2)}}`
,@{Name="Total(GB)";Expression={[math]::Round([decimal]$_.Total/1gb,2)}}`
,Created | Export-CSV C:\Script_Output\ifilers-snapshot-size.csv -Append -NoTypeInformation
}


Third script is for the Data Dedup information on each Filer (this script needs more work on it as I had to run it against each Filer instead of having a text file list of Filers

Import-Module DataOnTap

$controller = "isg-filer06"

Connect-NAController $controller -Credential <account-name>

Get-NaVol | foreach {
$volname = $_.name
Get-NaVolSis $_ | Select-Object @{n="HostName";e={$controller}}`
,@{n="VolumeName";e={$volname}}`
,@{Name="TotalSaved(GB)";Expression={[math]::Round([decimal]$_.TotalSaved/1gb,2)}}`
,State,PercentageSaved | Export-CSV C:\Script_Output\filers-dedup1.csv -Append -NoTypeInformation
}


Again, thanks to a number of bloggers out there where I managed to re-hash a few scripts to get what I wanted. I will credit them when I find their pages again!

Monday, 2 June 2014

Powershell - SID to User account

Usual issue of finding that the disk on a server is filling up.

One of the culprits can be the Recycle Bin which just gives you the SID of the offending user.

I found this useful link on the Microsoft site which describes how to find the information in the Registry.

It is a very short article which essentially states:
Use this tool at your own risk.
  1. Open Registry Editor and navigate to:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion \ProfileList
  2. Under the ProfileList key, you will see the SIDs. By selecting each one individually, you can look at the value entry and see what user name is associated with that particular SID.
Worked great for me - I am also looking for a simple Powershell script to give me the information. Once I do I'll post it here.

To Powershell the query I found this useful script (just add the appropriate SID):

$objSID = New-Object System.Security.Principal.SecurityIdentifier `
 ("S-1-5-21-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxx-27810")
 $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
$objUser.Value

Thursday, 22 May 2014

Powershell - Disk usage

I found a very useful script online a few weeks ago (but unfortunately I can't remember where from otherwise I would credit them!)

The script pulls machine names from a text file and then hunts down each disk and capacity used/free then saves the information to  htm file.

#requires -version 2.0
#use parameter  drive report to html.ps1 computer1,computer2 or a computer list file
#change file path and name on line 7 below to reflect name and  path of computer list file using.
#script will open web browser with current report when completed.

Param (
$computers = (Get-Content  "C:\Temp\Computers.txt")
)

$Title="Hard Drive Report to HTML"

#embed a stylesheet in the html header
$head = @"
<mce:style><!--
mce:0
--></mce:style><style _mce_bogus="1"><!--
mce:0
--></style>
<Title>$Title</Title>
<br>
"@ 

#define an array for html fragments
$fragments=@()

#get the drive data
$data=Get-WmiObject -Class Win32_logicaldisk -filter "drivetype=3" -computer $computers

#group data by computername
$groups=$Data | Group-Object -Property SystemName

#this is the graph character
[string]$g=[char]9608 

#create html fragments for each computer
#iterate through each group object
        
ForEach ($computer in $groups) {
    
    $fragments+="<H2>$($computer.Name)</H2>"
    
    #define a collection of drives from the group object
    $Drives=$computer.group
    
    #create an html fragment
    $html=$drives | Select @{Name="Drive";Expression={$_.DeviceID}},
    @{Name="SizeGB";Expression={$_.Size/1GB  -as [int]}},
    @{Name="UsedGB";Expression={"{0:N2}" -f (($_.Size - $_.Freespace)/1GB) }},
    @{Name="FreeGB";Expression={"{0:N2}" -f ($_.FreeSpace/1GB) }},
    @{Name="Usage";Expression={
      $UsedPer= (($_.Size - $_.Freespace)/$_.Size)*100
      $UsedGraph=$g * ($UsedPer/2)
      $FreeGraph=$g* ((100-$UsedPer)/2)
      #I'm using place holders for the < and > characters
      "xopenFont color=Redxclose{0}xopen/FontxclosexopenFont Color=Greenxclose{1}xopen/fontxclose" -f $usedGraph,$FreeGraph
    }} | ConvertTo-Html -Fragment 
    
    #replace the tag place holders. It is a hack but it works.
    $html=$html -replace "xopen","<"
    $html=$html -replace "xclose",">"
    
    #add to fragments
    $Fragments+=$html
    
    #insert a return between each computer
    $fragments+="<br>"
    
} #foreach computer

#add a footer
$footer=("<br><I>Report run {0} by {1}\{2}<I>" -f (Get-Date -displayhint date),$env:userdomain,$env:username)
$fragments+=$footer

#write the result to a file
ConvertTo-Html -head $head -body $fragments  | Out-File "C:\SQL Reports\drivereport_$((Get-Date).ToString('yyyy-MM-dd-hhmmss')).htm"


The only real amendment I made was adding the date time stamp to the output file.

Once armed with the information about the disk usage I can then investigate further by seeing what is taking the space up. I found a free tool which does the job - WinDirStat.

I will endeavour to find out who wrote the script........
And I just have!! - Gandalf 50


Wednesday, 26 March 2014

Powershell - Creating a scheduled task to run a script gathering application logs from a number of servers

Hmmm.... long title for such a small task.

As the title says - gathering the application log from a number of servers, running it as a scheduled task.

First of lets start with the script

## List of servers
$server = @('Server1','Server2','Server3');
## Get the application log of every server listed above
foreach ($computer in $server) {
Get-EventLog -ComputerName $server -LogName "Application" |
Export-csv -Path "C:\Data\Log_$((Get-Date).ToString('yyyy-MM-dd-hhmmss')).csv"}

I have found that the script does keep running and running and running......
(Note to self - fix!)

Which leads me onto the scheduled task.

The task is going to run on Windows 2008 R2 server.
Administrative Tools > Task Scheduler > Task Scheduler Library.

General
Created a Domain account purely for this scheduled task.
Run whether user is logged on or not.

Triggers
On a schedule
Daily
Set date/time
Set occurance
Stop task if runs longer than (I set mine to 4 hours)
Check the enabled box

Actions
Action - Start a Program
Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments: & 'C:\<path>\script-name.ps1'

Conditions
Left at default

Settings
Leave at default settings
History
Log of the task

The scheduled task should now run - the difficult part was working out what arguments should be added to the Actions.

Tuesday, 7 January 2014

Powershell - Query Group Membership in another Domain

This in one way that I know of to search another Domain by mounting it as a drive (I'm sure there are different ways of skinning a cat but this technique would work for a number of Domains - again I'm sure there are further options to be able to the same thing!!).

## Query Group Membership ##
Import-Module ActiveDirectory

## Mount AD as a drive ##
New-PSDrive -Name OtherDomain -PSProvider ActiveDirectory -Root 'DC=other,DC=domain,DC=co,DC=uk' -Server dc1.other.domain.co.uk -Credential DOMAIN\account
cd OtherDomain:
Get-ADGroup -Filter {Name -eq "ADGroup"} |
     ForEach-Object{
          $hash = @{GroupName = $_.Name ; UserID = '' ; Member = ''}
          $_ | Get-ADGroupMember -ea 0 -recurs |
              ForEach-Object{
                   $hash.Member = $_.Name
                   $hash.UserID = $_.SamAccountName
                   New-Object psObject -Property $hash
              }
          } | sort groupname, member | Export-CSV "C:\Data\ADGroup.csv" –NoTypeInformation

Powershell - Query Group Membership

A quick script to pull a list of users from a AD group:

## Query Group Membership ##
Import-Module ActiveDirectory
Get-ADGroup -Filter {Name -eq "ADGroup"} |
     ForEach-Object{
          $hash = @{GroupName = $_.Name ; UserID = '' ; Member = ''}
          $_ | Get-ADGroupMember -ea 0 -recurs |
              ForEach-Object{
                   $hash.Member = $_.Name
                   $hash.UserID = $_.SamAccountName
                   New-Object psObject -Property $hash
              }
          } | sort groupname, member | Export-CSV "C:\Data\ADGroup.csv" –NoTypeInformation

Thursday, 14 November 2013

VMware - Checking on snapshots

The backups failed one night and left a number of snapshots marooned.

(The backup process snapshots the NetApp volume, backups up the VMs, then deletes the snapshots).

To check which machines were affected I created and ran a Powershell script.

First of all I downloaded the VMware PowerCLI from here, you will need an VMware account to download.

Then opened up the PowerCLI to run the following commands:

Set-ExecutionPolicy RemoteSigned

Connect-VIServer vCenterServerName

get-vm | get-snapshot | format-list

This produces quite an in depth list of information, but a little too much as all I'm after is the name of machines with snapshots so I can check them for the backup snapshot. The date and description (as the backup names them as "SMVI Snapshot generated for.......")

Format list will give you the following objects:
Description
Created
Quiesced
PowerState
VM
VMId
Parent
ParentSnapshotId
ParentSnapshot
Children
SizeMB
IsCurrent
IsReplaySupported
ExtensionData
Id
Name
Uid

So my actual query was this which ends with an output option to a text file:

get-vm | get-snapshot | format-list vm,date,description | out-file c:\snapshot.txt

If you want to narrow your search down to a specific container as listed in "VMs and Templates", for example "Citrix" where all your Citrix servers are collected then amend the "get-vm" part of the query:

get-vm -location ”Citrix” | get-snapshot | format-list vm,date,description | out-file c:\snapshot.txt

 







Friday, 23 November 2012

Powershell - List users belonging to VM console access groups

As part of the upgrade from vSphere 4.1 to 5 we needed a list of users who access the VI via a shortcut (mainly external Third Party's) so we could contact them and explain how they can access the console in vSphere 5 (Web Client).

Each VM that requires other staff, or Third Party's, to access the console has an AD group created with the appropriate Virtual Center permissions. Each AD group is then populated with the appropriate accounts.

The first thought was just to have a script that listed the user account plus email address but no Group listing. The drawback to this was that external accounts didn't have any email accounts listed and it didn't list which VM the user needed to access.

New approach was to identify and list the Group in the output along with Users names. This would enable us to contact the Application Support people and explain the change and how to now connect. Then they would pass this information onto their Third Party suppliers.

Script

import-module activeDirectory
$vmwareGroups = get-adGroup -filter 'name -like "VMware*Console Access"'

## output object
$output = @()

$vmwareGroups | foreach {

    $groupName = $_.name   
    $groupMembers = $_ | get-adGroupMember
    $groupMembers | foreach {
        $memberName = $_.name
        $obj = new-object System.Management.Automation.PSObject
        $obj = $obj | add-member -memberType NoteProperty -name groupName -value $groupName -passthru
        $obj = $obj | add-member -memberType NoteProperty -name userName -value $memberName -passthru
        $output += $obj
    }
}


$output | export-csv "c:\vwmareGroups.csv" -NoTypeInformation


Thanks go to Klaas Vandenberghe (powershell.org) who helped with the initial script then one of my colleagues, Damian Shiell, who created the final script.