Category: Powershell


So as you can guess i am on a journey about learning powershell and i am trying to do things which might be useful.

This is a straight forward piece of script to help you baseline you lync installation.

My next edits to this script will output to files for comparison later and also i am going to section things into functions and also create a little menu system but here it the bones of a working script as it is… enjoy!

 

write-host “Welcome to the resource utilization baseline test” -Foregroundcolor Green -Backgroundcolor Black
write-host “Please confirm how long you want these tests to run for” -Foregroundcolor Green -Backgroundcolor Black
write-host “For exampe you may want to sample every 1 second for a max samples of 1000” -Foregroundcolor Green -Backgroundcolor Black
write-host “This will give you 1000seconds of samples… you may want this longer” -Foregroundcolor Green -Backgroundcolor Black
write-host “depending on the environmnet” -Foregroundcolor Green -Backgroundcolor Black
write-host “Comments and Suggestions to johm@microsoft.com or on Twitter: @mccabej” -foregroundcolor Yellow -backgrouncolor red
write-host “Please be kind i am still learning powershell :)” -foregroundcolor Yellow -backgrouncolor red
write-host
$sampleinttime = Read-host (“Please enter how often you want to sample in secs and press enter:”)
$maxsamp = Read-host (“Please enter the amount of samples you want to take and press enter:”)

write-host “Performing Resource Utilization Baseline Tests…….” -Foregroundcolor Green -Backgroundcolor Blue
write-host
write-host
write-host
$result = get-counter -counter “\Processor(_total)\% Processor Time” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$proctimetotal = $avg.average
write-host “Baseline Processor Time Overall” $proctimetotal -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Process(RTCSRV)\% Processor Time” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$proctimertcsrv = $avg.average
write-host “Baseline Processor Time for RTCSRV” $proctimertcsrv -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Process(IMMcuSvc)\% Processor Time” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$proctimeimmcusvc = $avg.average
write-host “Baseline Processor Time IMMcuSvc” $proctimeimmcusvc -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Memory\Pages/sec” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$mempages = $avg.average
write-host “Baseline for Memory Pages/sec” $mempages -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Network Interface(*)\Bytes Total/sec” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$netint = $avg.average
write-host “Baseline For Network Interfces Bytes Total/sec” $netint -Foregroundcolor Green -Backgroundcolor Blue

write-host
write-host
write-host
write-host “Performing User Load Baseline Tests…….” -Foregroundcolor Green -Backgroundcolor Blue
write-host
write-host

write-host

$result = get-counter -counter “\LS:SIP – 01 – Peers(*)\SIP – 001 – TLS Connections Active” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$tlsconnectionsactive = $avg.average
write-host “Baseline SIP – 001 – TLS Connections Active” $tlsconnectionsactive -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\LS:SIP – 01 – Peers(*)\SIP – 000 – Connections Active” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$connectionsactive = $avg.average
write-host “Baseline SIP – 000 – Connections Active” $connectionsactive -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\LS:SIP – 02 – Protocol\SIP – 001 – Incoming Messages/sec” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$incomingmsgpersec = $avg.average
write-host “Baseline SIP – 001 – Incoming Messages/sec” $incomingmsgpersec -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\LS:ImMcu – 00 – IMMcu Conferences\IMMCU – 000 – Active Conferences” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$immcuactiveconf = $avg.average
write-host “Baseline ImMCU Active Conferences” $immcuactiveconf -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\LS:ImMcu – 00 – IMMcu Conferences\IMMCU – 001 – Connected Users” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$immcuconusers = $avg.average
write-host “Baseline ImMCU Connected Users” $imccuconusers -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\LS:USrv – 00 – REGDBStore\USrv – 002 – Queue Latency (msec)” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$queuelatency = $avg.average
write-host “Baseline USrv – Queue Latency” $queuelatency -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\LS:USrv – 00 – REGDBStore\USrv – 004 – Sproc Latency (msec)” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$sproclatency = $avg.average
write-host “Baseline USrv – Sproc Latency” $sproclatency -Foregroundcolor Green -Backgroundcolor Blue

 

write-host
write-host
write-host
write-host “Performing Multipoint Conferencing Unit Resource Baseline Tests…….” -Foregroundcolor Green -Backgroundcolor Blue
write-host
write-host

write-host

$result = get-counter -counter “\Memory\Pages/sec” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$mempages = $avg.average
write-host “Baseline for Memory Pages/sec” $mempages -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Network Interface(*)\Bytes Total/sec” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$netint = $avg.average
write-host “Baseline For Network Interfces Bytes Total/sec” $netint -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Processor(_total)\% Processor Time” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$proctimetotal = $avg.average
write-host “Baseline Processor Time Overall” $proctimetotal -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Process(ASMCUSVC)\% Processor Time” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$asmcusvc = $avg.average
write-host “Baseline Processor Time for ASMCUSVC” $asmcusvc -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Process(AVMCUSVC)\% Processor Time” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$avmcusvc = $avg.average
write-host “Baseline Processor Time for AVMCUSVC” $avmcusvc -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Process(DataMCUsvc)\% Processor Time” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$datamcusvc = $avg.average
write-host “Baseline Processor Time for Datamcusvc” $datamcusvc -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Process(meetingmcusvc)\% Processor Time” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$meetingmcusvc = $avg.average
write-host “Baseline Processor Time for meetingmcusvc” $meetingmcusvc -Foregroundcolor Green -Backgroundcolor Blue

 

$result = get-counter -counter “\Process(ASMCUSVC)\Private Bytes” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$asmcusvcpb = $avg.average
write-host “Baseline Private Bytes for ASMCUSVC” $asmcusvcpb -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Process(AVMCUSVC)\Private Bytes” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$avmcusvcpb = $avg.average
write-host “Baseline Private Bytes for AVMCUSVC” $avmcusvc -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Process(DataMCUsvc)\Private Bytes” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$datamcusvcpb = $avg.average
write-host “Baseline Private Bytes for Datamcusvc” $datamcusvc -Foregroundcolor Green -Backgroundcolor Blue

$result = get-counter -counter “\Process(meetingmcusvc)\Private Bytes” -sampleinterval $sampleinttime -maxsample $maxsamp
$avg = $Result | Foreach-object {$_.CounterSamples}| Measure-object -Property CookedValue -Average
$meetingmcusvcpb = $avg.average
write-host “Baseline Private Bytes for meetingmcusvc” $meetingmcusvc -Foregroundcolor Green -Backgroundcolor Blue

Advertisements

i am adding to this script but take it and try it out for yourself…

#****************************************************************

# This script will test direct access
# it is very much work in progress and will be modified…
# at the minute this script will test which tunnel mode you are using
# i have a deep problem into teredo and will do the reset!

$useteredo = “0”
$use6to4 = “0”
$usehttps = “0”

$6to4 = netsh interface 6to4 show state
write-host “Checking to see if you are using 6to4……..”
Foreach ($6 in $6to4)
{
if ($6 -match “disabled”)
{
write-host “6to4 protocol is disabled”
write-host “You must be behind a NAT Device..”
}
else
{
$use6to4 = “1”
}

}

$teredo = netsh interface teredo show state
write-host “Checking to see if you are using teredo……..”
Foreach ($t in $teredo)
{
if ($t -match “disabled”)
{
write-host “Teredo protocol is disabled”
write-host “Your NAT Device Seems to be blocking UDP 3544”
}
else
{
$useteredo = “1”

}

}

$iphttps = netsh interface httpstunnel show interface

Foreach ($i in $iphttps)
{
 

if ($i -match “disabled”)
{
write-host “IPHTTPS protocol is disabled”

}

else
{
$usehttps = “1”

}

}

if($useteredo -eq “1”)
{
write-host “You are using Teredo”
$teredo1 = netsh interface teredo show state
$teredo2 = $teredo1 | where-object {$_ -match “Server Name”} |foreach-object {$_.Split(“:”)[1].Trim()}
$teredo3 = $teredo2.split(“(“)
Write-Host
Write-Host
Write-host “Direct Access Public Hostname is…. :” $teredo3[0]
Write-Host
Write-Host “Pinging host….” $teredo3[0]
$ping = ping $teredo3
$ping1 = $ping | Where-Object{ $_ -match “Request”}
$ping2 = $ping | Where-Object{ $_ -match “Reply”}
Write-Host
Write-Host
if ($ping1 -ne $null)
{
write-host $teredo3[0] “could not be contacted”
$noreach = 1
}
elseif ($ping2 -ne $null)
{
Write-Host $teredo3[0] “could be reached”
$noreach = 0
}

if ($noreach = 1)
{
write-host “Testing Internet Connection…..”
$testhost = “tpm.daccess.microsoft.com”
$inettest = ping $testhost
$inettest1 = $inettest | Where-Object{ $_ -match “Reply”}

if ($inettest1 -ne $null)
{
Write-Host “You have a connection to the internet!”
}
else {write-host ” you have no internet connection please check with your provider”}
}

}
elseif($use6to4 -eq “1”)
{
write-host “You are using 6to4”
}
elseif($usehttps -eq “1”)
{
write-host “You are using HTTPSTUNNEL”
}
else
{write-host “All protocols are disabled”}

#*************************************************************************************************

This is the next part to it Smile

so it will check the users home directory attribute, browse the directory for files that have not been accessed in 30 days and then create a report and send an email to the user…

 

going to add in the next few days, some control logic for errors etc… in the next few days…. so what out for an update…

comments welcome on how to make it better…

 

 

******************************************************************************************************

import-module ActiveDirectory
write-host "Checking For HomeDirectory Attribute…."
$enabledusers = get-aduser -filter {Enabled -eq "True"} -properties Homedirectory |where {$_.HomeDirectory -ne $null}

Foreach ($u in $enabledusers)
{
    write-host "Current User…." $u.Name
    write-host "You Home Directory is…" $u.HomeDirectory
    $pathtocheck = $u.homedirectory

    $statuscheck = get-childitem $pathtocheck -Recurse

    Foreach ($s in $statuscheck)

    {
   

    $access = $s | % {(get-date) – $_.LastAccessTime }
        if ($access.days -ge 2)
        {
        #write-host $s.fullname
        #write-host "File Was Last Accessed" $access.days "Days Ago"

        $outputfilename = ( "c:\" + $u.samaccountname + "30dayReport.csv")
        #$outputfilename
        $s |format-table -property Fullname |out-file $outputfilename -append
        $attachment = $outputfilename
        }
   
   
    }

$emailto = ("<" + $u.samaccountname + "@test.com>")
$emailfrom = "<admin@test.com>"
$smtpserver = "dc-01.contoso.local"

Write-host "Sending Report….."
send-mailmessage -to $emailto -from $emailfrom -subject "warning you have files which have not been accessed in over 30days" -smtpserver $smtpserver -attachment $attachment

 

*********************************************************************************************************

 

}

so this is the next level… with one more step to add later on…

and remember this is me beginning to play with powershell i am sure there are nicer ways to code…

this will check a users properties for the homedirectory attribute and then scan their home directory and see what files have not been used in the last 30days and write the results out to a csv file

I plan to add to this script but this is the core of it so far…

 

import-module ActiveDirectory
write-host "Checking For HomeDirectory"
$enabledusers = get-aduser -filter {Enabled -eq "True"} -properties Homedirectory |where {$_.HomeDirectory -ne $null}

Foreach ($u in $enabledusers)
{
    $u.Name
    $u.HomeDirectory
    $pathtocheck = $u.homedirectory

    $statuscheck = get-childitem $pathtocheck -Recurse

    Foreach ($s in $statuscheck)

    {

    $access = $s | % {(get-date) – $_.LastAccessTime }
        if ($access.days -ge 2)
        {
        write-host $s.fullname
        write-host "File Was Last Accessed" $access.days "Days Ago"
        $s |format-table -property Fullname |out-file c:\results.csv -append
        }
   
    }

}

This is actually part of a larger script but i taught it might be useful for some people.

basically place it in the root directory of your system or the root of the directory you want to check i.e. a users home directory run the script and it will output a CSV file with the results…

enjoy

 

*********************************************************************************************

$statuscheck = get-childitem -Recurse

Foreach ($s in $statuscheck)
{

    $access = $s | % {(get-date) – $_.LastAccessTime }
    if ($access.days -ge 2)
    {
    write-host $s.fullname
    write-host "File Was Last Accessed" $access.days "Days Ago"
    $s |format-table -property Fullname |out-file d:\results.csv -append
    }
   
}

 

*********************************************************************************************

 

I was a little bored this morning so i wrote this script as i am trying to learn powershell

it will give you a little menu system to check, stop & start lync services….

Hopefully it will help someone… and i might improve it Smile

 

******************************************************************************************************

 

set-executionpolicy unrestricted

$foreverloop = 4

Write-host "Thank you for using this little powershell script" -Backgroundcolor Black -Foregroundcolor Green
Write-host "Please repost and feel free to use!" -Backgroundcolor Black -Foregroundcolor Green
write-host "Always nice to hear feedback… Twitter @mccabej or email johm@microsoft.com" -Backgroundcolor Black -Foregroundcolor Green

do

{
Write-host "Please Select 1 if you want to check the status of all Lync Services"
Write-host "Please Select 2 if you want to stop all services"
write-host "Please Select 3 if you want to start all services"
write-host "Press 0 to exit"
$menu = Read-host ("Enter your Selection and Press Enter")
$foreverloop = $menu

cls

if ($menu -eq 1)
{

$service = get-service RTC*
$service
Read-Host "Press Enter Key To Return To The Menu"
cls
}
Elseif ($menu -eq 2)
{
    Foreach ($s in $service)
    {
    write-host
    write-host
     $s
   
   
    {
    if ($s.Status -eq "Running") {Stop-Service $s.Name -Force}
   
    }

Read-host "Press Enter Key To Return To The Menu"
cls
}
Elseif ($menu -eq 3)
{
    Foreach ($s in $service)
    {
    write-host
    write-host
     $s
    if ($s.Status -eq "Stopped") {Start-Service $s.Name}
    }
Read-host "Press Enter Key To Return To The Menu"
cls
}

Else
{Write-host "Exiting…."
}
}
Until ($foreverloop -eq 0)

 

 

***************************************************************************************************************