Pages

1/14/2015

Powershell: File Dialog

Here is an example of presenting the user with a file dialog. 
This script also does some conversion and opens the CSV in Excel when it's done.
############################################################################################
#
# SSID Report Conversion
# Process SSID report from NCS.  Calculate connection time in seconds and KB transferred
#

Function Get-FileName($initialDirectory)
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.ShowHelp = $true
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "All files (*.*)| *.*"
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
} #end function Get-FileName

# 

[string]$infile = Get-FileName -initialDirectory "Downloads"
if (-not $infile) { exit }

$outfile = $infile -replace ".csv", "-ADJUSTED.csv"

$file = get-content $infile
$today = Get-Date
$today = $today.touniversaltime()

write-output "Converted:  $today" | out-file -encoding ASCII -filepath $outfile
foreach ($line in $file) {
 $field = $line.split(",")
 if ($field.count -eq 13) {# report body
  if ($field[0] -eq "Client Username") { #header line (first field heading matches)
   $heading = $line + ",Seconds Connected,KBytes Transferred"
   write-output $heading | out-file -encoding ASCII -filepath $outfile -append
   continue
  } #end header line

  #fix average Kb
  $avgKbits = $field[6]
  if ($avgKbits -eq "<0.1") { $avgKbits = 0 }
 
  #Connection time in seconds
  $conn = $field[5]
  [array]$seperator = " hrs " , " min " , " sec"
  $option = [System.StringSplitOptions]::RemoveEmptyEntries
  $hours = 0
  $minutes = 0
  $seconds = 0
  $connection = 0
  $temp = $conn.split($seperator, $option)
  if ($temp.count -eq 3) {
   $hours = [int]$temp[0]
   $minutes = [int]$temp[1]
   $seconds = [int]$temp[2]
   }
  elseif ($temp.count -eq 2) {
   $minutes = [int]$temp[0]
   $seconds = [int]$temp[1]
   }
  elseif ($temp.count -eq 1) {
   $seconds = [int]$temp[0]
   }
  $connection = ( $hours * 60 * 60 )  + ( $minutes * 60 ) + $seconds

  #Bytes transferred
  $KBytes = $connection * $avgKbits * 8
  $line = $line + "," + $connection + "," + $KBytes
  write-output $line | out-file -encoding ASCII -filepath $outfile -append
 }#end if 16 fields

 else {# report heading line (does not have 13 fields)
  write-output $line | out-file -encoding ASCII -filepath $outfile -append
 }#end else 

}#end foreach line

start excel $outfile

1/12/2015

Troubleshooting TCP Throughput

Good presentation of TCP Throughput troubleshooting:
PDF:  http://packetbomb.com/understanding-throughput-and-tcp-windows
Video with example:
https://www.youtube.com/watch?v=qFWjugyKyrE

Thanks to kory@packetbomb.com

Packet-Level: Am I looking at a trace from client side or server side?

Look at 3 way handshake (SYN, SYN/ACK, ACK.)
  - Client side trace will have delay between SYN & SYN/ACK
  - Server side trace will have delay between SYN/ACK & ACK.

Duh, this is obvious!  Some might say.  But I find it insightful as TCP analysis is just a "hobby" -- I do it so rarely in my work that I learn and re-learn each time I need to slog through a trace file.

powershell query remote sharepoint

Example of query to a sharepoint list on a remote machine.
############################################################################################
# Create $list of server names - in Service Catalog where status equals selection
#

[array]$list = $null
$uri = "http://portal/apps/systemscatalog/_vti_bin/lists.asmx?WSDL"
$listName = "Server Catalog" 

# Create xml query - get the whole list
$xmlDoc = new-object System.Xml.XmlDocument
$query = $xmlDoc.CreateElement("Query")
$vfxml = "" +
 "" +
 "" +
 "" +
 ""
$viewFields = $xmlDoc.CreateElement("ViewFields")
$queryOptions = $xmlDoc.CreateElement("QueryOptions")
$query.set_InnerXml("FieldRef Name='Full Name'") 
$rowLimit = "3000"
$serverlist = $null 
$service = $null  
try{
    $service = New-WebServiceProxy -Uri $uri  -Namespace SpWs  -UseDefaultCredential
}
catch{ 
    Write-Error $_ -ErrorAction:'SilentlyContinue' 
}
if($service -ne $null){
    try{        
        $serverlist = $service.GetListItems($listName, "", $query, $viewFields, $rowLimit, $queryOptions, $null)
    }
    catch{ 
        Write-Error $_  -ErrorAction:'SilentlyContinue'
    }
}
$output = $serverlist.data.row


if ( -not $output) {
 clear-host
 "ERROR:  No output from sharepoint"
 "ERROR:  No output from sharepoint" | out-file $logfile -append
 exit
 }

[string]$status = $null
foreach ($item in $output) {
 [string]$status = $item.ows_DeploymentStatus
 $Server = $item.ows_Title
 $dmz = $item.ows_IsDMZServer
 if ($dmz -eq $null) { $dmz = 0 }
 if($status -eq $selection) { 
   if ($filterDMZ -and (-not $dmz)) {
  if ($Server) { $list = $list + $Server} #skip a null value
   }#end if
   else {
       if ($Server) { $list = $list + $Server} #skip a null value
       }
 }#end if
 [string]$status = $null 
} #end foreach

#

$list = $list | sort-object

if ( -not $list) { 
    "ERROR:  No server list to check"
    "ERROR:  No server list to check" | out-file $logfile -append
    exit
    }