Home > OS >  TO GATHER INCOMING NFS SESSIONS
TO GATHER INCOMING NFS SESSIONS

Time:08-03

I want to gather incoming NFS Sessions. We then interpret this information and filter out any duplicates. If a duplicate is found, an occurrence value is upped. I wanted to keep a log of one unique record per connection, updated with the most recent connection datetime

How can I do that ?

Script :

[array]$ResultArray = Import-Csv C:\Scripts\IncomingNFSConnections.csv

$ServerConnections = Get-NfsMountedClient
foreach ($Connection in $ServerConnections) {
$Date = Get-Date -Format 'yyyy-MM-dd'
$ServerObject = [PSCustomObject]@{
  ClientComputerName = $Connection.ClientIpAddress
  Date               = $Date
  Occurence          = 1
  }
  $ResultArray  = $ServerObject
}

My desired output:

"ClientIpAddress","Date","Occurence"
"10.123.12.12","2022-08-02","7"
"10.123.12.14","2022-08-02","10"

the output of Get-NfsMountedClient

ClientId     ClientIpAddress TotalSessions
--------     --------------- -------------
141733920814 10.123.12.12    1            
141733920825 10.123.12.13    1            
   

CodePudding user response:

You will have to check ,if this is new connection or old connection and keep it in a loop

$mainobject = @()

foreach($connection in $connections ){
        
$IpExists = $mainobject | where {$_.remoteaddress -eq $connection.RemoteAddress }
if ($IpExists){

## update count
$IpExists.countt = $($IpExists.countt) 1

}
else {

##  we got new ip address
$mainobject  = [PSCustomObject]@{
Remoteaddress = $connection.RemoteAddress
countt =1

}

}

your get-nfsmountedclient and foreach should be in a never ending a loop with a sleep timer

CodePudding user response:

Looking at your desired output, I believe this is what you want to do:

$result = Import-Csv -Path 'C:\Scripts\IncomingNFSConnections.csv' | 
Group-Object ClientIPAddress | 
Select-Object @{Name = 'ClientIPAddress'; Expression = {$_.Name}},
              @{Name = 'Date'; Expression = {Get-Date -Format 'yyyy-MM-dd'}},
              @{Name = 'Occurrence'; Expression = {($_.Group | Measure-Object -Property TotalSessions -Sum).Sum}}


# show on screen
$result | Format-Table -AutoSize

# write to new csv file
$result | Export-Csv -Path 'C:\Scripts\NFSConnections.csv' -NoTypeInformation
  • Related