using Powershell with harbour App to check SMB

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
AUGE_OHR
Posts: 2062
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

using Powershell with harbour App to check SMB

Post by AUGE_OHR »

hi,

i want to "check" SMB Setting with Powershell

Code: Select all

Get-SmbServerConfiguration 
and want to get Result of Powershell
EnableOplocks : True
EnableSMB1Protocol : False
EnableSMB2Protocol : True
i also want check which Port ( 139 / 445 ) is "listening"

Code: Select all

Get-NetTCPConnection -LocalPort 139,445 -ea 0 | select Local*, Remote*, State, @{n="ProcessName";e={(Get-Process -Id $_.OwningProcess).ProcessName}} | ft -Auto
LocalAddress LocalPort RemoteAddress RemotePort State ProcessName
------------ --------- ------------- ---------- ----- -----------
:: 445 :: 0 Listen System
if both NetBIOS over TCP/IP and directly hosted SMB over TCP/IP are available (that is, if ports 445 and 139 are both listening), Windows tries both options at the same time ... :roll:

so how get Powershell Result into harbour App :idea:
have fun
Jimmy
User avatar
AUGE_OHR
Posts: 2062
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: using Powershell with harbour App to check SMB

Post by AUGE_OHR »

hi

found a Way using Clipboard to pass Result of PowerShell into App using

Code: Select all

| CLIP

Code: Select all

PROCEDURE DoCheckSMB()
LOCAL cStep1 := "Get-SmbServerConfiguration "
LOCAL cStep2 := 'Get-NetTCPConnection -LocalPort 139,445 -ea 0 | select Local*, Remote*, State, @{n="ProcessName";e={(Get-Process -Id $_.OwningProcess).ProcessName}} | ft -Auto '
LOCAL cClip  := " | CLIP "
LOCAL cRun   := "PowerShell.EXE "
LOCAL cPara  := "-ExecutionPolicy Bypass -Command "
LOCAL cFile1 := HB_DirBase()+"GETSMB.PS1"
LOCAL cFile2 := HB_DirBase()+"LISTSMB.PS1"
LOCAL nHandle, cMemo, nLines, ii, cShow, cLine, cMore

   ***************** Step1 ********************

   System.EmptyClipboard
   nHandle := FCreate( cFile1, FC_NORMAL )
   IF nHandle == -1
      MsgInfo("Error create File : " + cFile1, STR(FError() ) )
   ELSE
      FWrite( nHandle, cStep1 + cClip )
      FWrite( nHandle, CRLF + "EXIT" )
      FClose( nHandle )
   ENDIF

   cMore := cPara + cFile1
   ShellExecute( 0, "open", cRun, cMore,, SW_HIDE )

   DO WHILE EMPTY(System.Clipboard)
      hb_idleSleep( 0.1 )
   ENDDO

   cShow := ""
   cMemo := System.Clipboard
   nLines := MlCount( cMemo, 80 )
   FOR ii := 1 TO nLines
       cLine := Trim( MemoLine( cMemo, 80, ii ) )
       DO CASE
          CASE "EnableOplocks" $ cLine
             cShow  += cLine + CRLF
          CASE "EnableSMB1Protocol" $ cLine
             cShow  += cLine + CRLF
          CASE "EnableSMB2Protocol" $ cLine
             cShow  += cLine + CRLF
       ENDCASE
   NEXT
*  Msginfo(cShow)
   FERASE(cFile1)

   ***************** Step2 ********************

   System.EmptyClipboard
   nHandle := FCreate( cFile2, FC_NORMAL )
   IF nHandle == -1
      MsgInfo("Error create File : " + cFile2, STR(FError() ) )
   ELSE
      FWrite( nHandle, cStep2 + cClip)
      FWrite( nHandle, CRLF + "EXIT" )
      FClose( nHandle )
   ENDIF

   cMore := cPara + cFile2
   ShellExecute( 0, "open", cRun, cMore,, SW_HIDE )

   DO WHILE EMPTY(System.Clipboard )
      hb_idleSleep( 0.1 )
   ENDDO

*  cShow := ""
   cShow += CRLF

   cMemo := System.Clipboard
   nLines := MlCount( cMemo, 80 )
   FOR ii := 1 TO nLines
       cLine := Trim( MemoLine( cMemo, 80, ii ) )
       DO CASE
*          CASE "LocalAddress" $ cLine
*          CASE "------------" $ cLine
       OTHERWISE
          cShow  += cLine + CRLF
       ENDCASE
   NEXT
   Msginfo(cShow)
   FERASE(cFile2)

RETURN
! Note : PowerShell Script *.PS1 Script need "Full-Path"
SMB2_PowerShell.jpg
SMB2_PowerShell.jpg (15.24 KiB) Viewed 901 times
as i say it is not good when Windows "listen" to both Port
if Windows is "Listing" to UPD Port 139 disable "NetBIOS"
Disable_NetBIOS.JPG
Disable_NetBIOS.JPG (206.48 KiB) Viewed 901 times
p,s don´t forget to disable SMB2 "Local Cache"
https://docs.microsoft.com/en-us/previo ... dfrom=MSDN
DirectoryCacheLifetime to ZERO.
FileNotFoundCacheLifetime to ZERO.
FileInfoCacheLifetime to ZERO.
have fun
Jimmy
User avatar
AUGE_OHR
Posts: 2062
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: using Powershell with harbour App to check SMB

Post by AUGE_OHR »

hi,

here full Source Code ( no Resource )
CHECKSMB.ZIP
(1.63 MiB) Downloaded 81 times
SMB2_Checker.JPG
SMB2_Checker.JPG (137.69 KiB) Viewed 821 times
press "Start" to get Result
for more Information press F1

you can press Ctrl-R to re-check
have fun
Jimmy
User avatar
AUGE_OHR
Posts: 2062
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: using Powershell with harbour App to check SMB

Post by AUGE_OHR »

hi,

on some PC / OS you need Admin Rights to rum CheckSMB else it will "hang" while wait for Clipboard

i have include a "WaitWindow()" and a Counter for "Timeout" in this Version
CHECKSMB_07.zip
32 Bit EXE and Source-code
(1.64 MiB) Downloaded 91 times
have fun
Jimmy
Post Reply