prashant1987

Posts Tagged ‘Powershell

HELLO’S,

Again after longtime I am writing here 🙂

Earlier today I was having bunch of Servers on which I was not sure if I have access to all of them. I wanted to know the server list on which my network ID is added to the Local Administrator Group. 🙂

I came up with below powershell script which help us to find out if the network ID has been added to the administrator list or not (Mainly for Remoting into the Server :))

$Servers = Get-Content C:\Servers.txt
Foreach($strComputer in $Servers)
{
try
{
$computer = [ADSI](”WinNT://” + $strComputer + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)
[string]$members = ($Group.psbase.invoke(”Members”) | %{$_.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $_, $null)}) -replace (‘WinNT://DOMAIN/’ + $server + ‘/’), ” -replace (‘WinNT://DOMAIN/’, ‘DOMAIN\’) -replace (‘WinNT://’, ”)
$m = $members.Replace(” “,”,”)
$k = $m.Replace(“/”,”\”)
[array]$l = $k -split “,”
if(($l | %{$_}) -eq “Contoso.com\User1”)
{
Write-Host “You have admin rights on $StrComputer”
}
Else
{
Write-Host “You don’t have admin rights on $StrComputer”
}
}
Catch
{
Write-Host “$Strcomputer has an exception”
}
}

The server list will go in C:\Servers.txt

Contoso.com\User1 — is the account name which you want to validate.

I hope this helps.

Thanks,

-Prashant Girennavar.

We are often Worried about , how much C:\ Drive Space is left on our Server or Client systems . Below script will help us to find out the C:\Drive space on Multiple servers and output will be save in CSV File with server name and its C:\Drive FreeSpace in GB

Get-WMIObject Win32_LogicalDisk -filter “DeviceId=’c:'” -computer (Get-Content c:\Computers.txt) | Select SystemName,@{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} | out-file C:\FreeSpace.csv –force

C:\Computers.txt contains list of servers/client systems on which you want to perform the Query

C:\FreeSpace.csv is output file where query result will be stored.

Hope this helps.

Thanks,

-Prashant Girenanvar.

If you are a windows server administrator then , one or the other day you will be asked to get the UPTime report of list of windows servers (Most of the times your Boss will ask this :))

Below Script will help you to get the UPtime report of listed servers

Before you run the script ,

  • Create a text file servers.txt in C:\ Drive and list the servers against which you need to run this query (The Script will list out the UPtime report of listed server)

Description –

This script will make use of WMI Query (Class – win32_Operatingsystem) and get the lastboottime of listed server. Script will produce 2 TXT Files namely , UPtimeReport.txt and Offline.txt in your C:\ Drive

Note – If all the servers are online then offline.txt will not be created and Vice Versa . i.e if all the servers are offline then UPTimeReport.txt will not be created.

PowerShell Script

#Title – Script for querying windows server UPTime/Lastboot

#Description – This script queries windows servers listed in Servers.txt file (WMI Query) and get their last bootuptime

$servers = Get-Content C:\Servers.txt
foreach ($s in $servers)
{
try
{
$a=Get-WmiObject -ComputerName $s -Class Win32_OperatingSystem -ErrorAction ‘Stop’
$b = $a.convertToDateTime($a.Lastbootuptime)
[TimeSpan]$LastBoot = New-TimeSpan $b $(Get-Date)
(‘{0} {1}Day(s),{2}Hour(s),{3}Min(s),{4}Seconds’ -f $s,$LastBoot.Days,$lastboot.Hours,$LastBoot.Minutes,
$LastBoot.Seconds) | out-file C:\UptimeReport.txt -append -Encoding ascii
}
Catch
{
(‘{0} Server is notreachable’ -f $s)| Out-File C:\offline.txt -Append -Encoding ascii
}
}

I Hope this script will help , and it will make your BOSS Happy as well 🙂

Regards,

_Prashant_