Powershell and Purebasic
Powershell and Purebasic
Now that i've switched over to Windows 10 i'm looking towards the future. Presently I use the PB "RunProgram" function to use the command prompt and parse/filter the output to get what I want, however, there are some things that will possibly go away in the future. One of these things is the netsh.exe function in Windows as this is planned (possibly) to be moved to the Powershell environment in future builds of Windows 10 and the familiar netsh function I access through the command prompt with PB when needed will go away.
So, my question is, how do you communicate with Powershell in Windows via PureBasic? Its got many attractive attributes already and it seems it will be better to start changing over and get used to using it.
So, my question is, how do you communicate with Powershell in Windows via PureBasic? Its got many attractive attributes already and it seems it will be better to start changing over and get used to using it.
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.
Re: Powershell and Purebasic
not an answer to your question sorry, but interestingly just a few days ago i read that Microsoft is also bringing Powershell to Linux and OSX:
http://arstechnica.com/information-tech ... inux-os-x/
http://arstechnica.com/information-tech ... inux-os-x/
Re: Powershell and Purebasic
I happen to be looking into this at the moment.
This may help to get you started: http://www.purebasic.fr/english/viewtop ... 55#p365055

This may help to get you started: http://www.purebasic.fr/english/viewtop ... 55#p365055
- It was too lonely at the top.
System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Powershell and Purebasic
I wrote this some time ago. I couldn't remember when if you waterboarded me, not even if the Donald himself was on the bucket:
You could modify this easily to take a string parameter and return any desired path that exists in the %PATH.
Code: Select all
Procedure.s GetPath_PowerShell()
Protected path$ = GetEnvironmentVariable("PATH"), ; List of all paths
PowerShell$ = "PowerShell not found!", ; Initialize with failure, success will change it
thispath$ = #Empty$, ; Temporary var to hold path for comparison
i.i ; Loop counter set to number of paths available
For i = 1 To CountString(path$, ";") + 1
thispath$ = StringField(path$, i, ";")
If FindString(thispath$, "powershell", 1, #PB_String_NoCase)
PowerShell$ = thispath$
Break
EndIf
Next
ProcedureReturn PowerShell$
EndProcedure
Debug GetPath_PowerShell()
BERESHEIT
Re: Powershell and Purebasic
Laid up with an injured leg right now, so I spent yesterday playing around with powershell in Windows 10. Turns out it works great with the PB RunProgram function so..... its not that pretty right now but I slapped something together for demo/play purposes ....
want to know if your system prefers IPv6 over IPv4?
Code: Select all
Procedure.b GetTestIPv46Valid(ipadrtoval$) ; works for IPv4 and IPv6
ipadr$ = Chr(34) + Chr(39) + ipadrtoval$ + Chr(39) + Chr(34)
OutCmd$ = ""
KeepOutCmdA$ = ""
CmdPromptRun = RunProgram("powershell.exe", " [System.Net.IPAddress]::TryParse( " + ipadr$ + ", [ref] $NULL)","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmdA$ = KeepOutCmdA$ + OutCmd$
Wend
KillProgram(CmdPromptRun)
CloseProgram(CmdPromptRun)
EndIf
If Trim(KeepOutCmdA$) = "True"
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure.s GetIPv4ODNS() ; this one to get public IPv4 address
; multiple sources for Ipv4 address in case one is down, times out, or something....we have other sources to ensure we always get an Ipv4 address
gotipv4addrX.b = #True
gotipv4addrA.b = #True
gotipv4addrB.b = #True
gotipv4addrC.b = #True
gotipv4addrD.b = #True
gotipv4addrE.b = #True
gotipv4addrF.b = #True
gotipv4addrG.b = #True
gotipv4addrH.b = #True
gotipv4addrI.b = #True
gotipv4addrJ.b = #True
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Resolve-DnsName" + " -Name whoami.akamai.com","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Position = FindString(KeepOutCmd$, "AnswerIP4Address", 1, #PB_String_NoCase)
getipline$ = Mid(KeepOutCmd$, Position)
Result4$ = Trim(RemoveString(getipline$, "AnswerIP4Address", #PB_String_NoCase))
Result4$ = Trim(RemoveString(Result4$, ":", #PB_String_NoCase))
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrX = #True
Else
gotipv4addrX = #False
Result4$ = "(No Information)"
EndIf
If gotipv4addrX = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Resolve-DnsName" + " -Name myip.opendns.com" + " -Server resolver1.opendns.com","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Position = FindString(KeepOutCmd$, "Answer", 1, #PB_String_NoCase)
getipline$ = Mid(KeepOutCmd$, Position)
Result4$ = Trim(RemoveString(getipline$, "Answer", #PB_String_NoCase))
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrA = #True
Else
gotipv4addrA = #False
Result4$ = "(No Information)"
EndIf
EndIf
If gotipv4addrA = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Resolve-DnsName" + " -Name myip.opendns.com." + " -Server resolver2.opendns.com","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Position = FindString(KeepOutCmd$, "Answer", 1, #PB_String_NoCase)
getipline$ = Mid(KeepOutCmd$, Position)
Result4$ = Trim(RemoveString(getipline$, "Answer", #PB_String_NoCase))
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrB = #True
Else
gotipv4addrB = #False
Result4$ = "(No Information)"
EndIf
EndIf
If gotipv4addrB = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Resolve-DnsName" + " -Name whoami.akamai.net" + " -Server ns1-1.akamaitech.net","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Position = FindString(KeepOutCmd$, "Answer", 1, #PB_String_NoCase)
getipline$ = Mid(KeepOutCmd$, Position)
Result4$ = Trim(RemoveString(getipline$, "Answer", #PB_String_NoCase))
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrC = #True
Else
gotipv4addrC = #False
Result4$ = "(No Information)"
EndIf
EndIf
If gotipv4addrC = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Resolve-DnsName" + " -Name whoami.akamai.net" + " -Server NS2-193.AKAMAITECH.NET","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Position = FindString(KeepOutCmd$, "Answer", 1, #PB_String_NoCase)
getipline$ = Mid(KeepOutCmd$, Position)
Result4$ = Trim(RemoveString(getipline$, "Answer", #PB_String_NoCase))
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrD = #True
Else
gotipv4addrD = #False
Result4$ = "(No Information)"
EndIf
EndIf
If gotipv4addrD = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $url = 'http://ipv4.whatismyip.akamai.com/'" + #CRLF$ + "$wc = New-Object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString($url)" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result4$ = KeepOutCmd$
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrE = #True
Else
gotipv4addrE = #False
Result4$ = "(No Information)"
EndIf
EndIf
If gotipv4addrE = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $url = 'http://ipv4.myip.dk/api/info/IPv4Address'" + #CRLF$ + "$wc = New-Object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString($url)" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result4$ = RemoveString(KeepOutCmd$, Chr(34), #PB_String_NoCase, 1)
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrF = #True
Else
gotipv4addrF = #False
Result4$ = "(No Information)"
EndIf
EndIf
If gotipv4addrF = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $wc = new-object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString('http://myexternalip.com/raw')" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result4$ = KeepOutCmd$
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrG = #True
Else
gotipv4addrG = #False
Result4$ = "(No Information)"
EndIf
EndIf
If gotipv4addrG = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $wc = new-object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString('http://myip.dnsomatic.com/')" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result4$ = KeepOutCmd$
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrH = #True
Else
gotipv4addrH = #False
Result4$ = "(No Information)"
EndIf
EndIf
If gotipv4addrH = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $wc = new-object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString('http://ipv4.icanhazip.com/')" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result4$ = KeepOutCmd$
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrI = #True
Else
gotipv4addrI = #False
Result4$ = "(No Information)"
EndIf
EndIf
If gotipv4addrI = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $wc = new-object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString('http://ipinfo.io/ip')" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result4$ = KeepOutCmd$
If GetTestIPv46Valid(Result4$) = #True
Result4$ = Result4$
gotipv4addrJ = #True
Else
gotipv4addrJ = #False
Result4$ = "(No Information)"
EndIf
EndIf
If gotipv4addrA = #False And gotipv4addrB = #False And gotipv4addrC = #False And gotipv4addrD = #False And gotipv4addrE = #False And gotipv4addrF = #False And gotipv4addrG = #False And gotipv4addrH = #False And gotipv4addrI = #False And gotipv4addrJ = #False
Result4$ = "Unable to get IPv4 Address"
Else
Result4$ = Result4$
EndIf
ProcedureReturn Result4$
EndProcedure
Procedure.s GetIPv6ODNS() ; this one to get public IPv6 address
;OpenDNS IPv6 DNS Server = 2620:0:ccc::2 - 2620:0:ccd::2
; resolver1.ipv6-sandbox.opendns.com - resolver2.ipv6-sandbox.opendns.com
; note: these are recursive > https://www.opendns.com/about/innovations/ipv6/
gotipv6A.b = #True
gotipv6B.b = #True
gotipv6C.b = #True
gotipv6D.b = #True
gotipv6E.b = #True
gotipv6F.b = #True
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " nslookup myip.opendns.com 2620:0:ccc::2","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result1$ = RemoveString(KeepOutCmd$, "Address:", #PB_String_NoCase, 1)
Result2$ = RemoveString(Result1$, "Server:", #PB_String_NoCase, 1)
Result3$ = RemoveString(Result2$, "resolver1.ipv6-sandbox.opendns.com", #PB_String_NoCase, 1)
Result4$ = RemoveString(Result3$, "myip.opendns.com", #PB_String_NoCase, 1)
Result5$ = RemoveString(Result4$, "Name:", #PB_String_NoCase, 1)
Result5$ = RemoveString(Result5$, "2620:0:ccc::2", #PB_String_NoCase, 1)
Result6$ = Trim(Result5$)
If GetTestIPv46Valid(Result6$) = #True
Result6$ = Result6$
gotipv6A = #True
Else
gotipv6A = #False
EndIf
If gotipv6A = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " nslookup myip.opendns.com 2620:0:ccd::2","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result1$ = RemoveString(KeepOutCmd$, "Address:", #PB_String_NoCase, 1)
Result2$ = RemoveString(Result1$, "Server:", #PB_String_NoCase, 1)
Result3$ = RemoveString(Result2$, "resolver2.ipv6-sandbox.opendns.com", #PB_String_NoCase, 1)
Result4$ = RemoveString(Result3$, "myip.opendns.com", #PB_String_NoCase, 1)
Result5$ = RemoveString(Result4$, "Name:", #PB_String_NoCase, 1)
Result5$ = RemoveString(Result5$, "2620:0:ccd::2", #PB_String_NoCase, 1)
Result6$ = Trim(Result5$)
If GetTestIPv46Valid(Result6$) = #True
Result6$ = Result6$
gotipv6B = #True
Else
gotipv6B = #False
EndIf
EndIf
If gotipv6B = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $url = 'http://ipv6.whatismyip.akamai.com/'" + #CRLF$ + "$wc = New-Object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString($url)" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result6$ = KeepOutCmd$
If GetTestIPv46Valid(Result6$) = #True
Result6$ = Result6$
gotipv6C = #True
Else
gotipv6C = #False
EndIf
EndIf
If gotipv6C = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $url = 'http://ipv6.myip.dk/api/info/IPv6Address/'" + #CRLF$ + "$wc = New-Object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString($url)" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result6$ = RemoveString(KeepOutCmd$, Chr(34), #PB_String_NoCase, 1)
If GetTestIPv46Valid(Result6$) = #True
Result6$ = Result6$
gotipv6D = #True
Else
gotipv6D = #False
EndIf
EndIf
If gotipv6D = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $wc = new-object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString('http://ipv6.icanhazip.com/')" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result6$ = KeepOutCmd$
If GetTestIPv46Valid(Result6$) = #True
Result6$ = Result6$
gotipv6E = #True
Else
gotipv6E = #False
EndIf
EndIf
If gotipv6E = #False
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $url = 'http://ipv6.whatismyip.akamai.com/'" + #CRLF$ + "$wc = New-Object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString($url)" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Result6$ = KeepOutCmd$
If GetTestIPv46Valid(Result6$) = #True
Result6$ = Result6$
gotipv6F = #True
Else
gotipv6F = #False
EndIf
EndIf
If gotipv6A = #False And gotipv6B = #False And gotipv6C = #False And gotipv6D = #False And gotipv6E = #False And gotipv6F = #False
Result6$ = "Unable to get IPv6 Address or No Information or Non-RFC4291/section 2.2 format for IPv6 Info"
Else
Result6$ = Result6$
EndIf
ProcedureReturn Result6$
EndProcedure
Debug GetIPv4ODNS()
Debug GetIPv6ODNS()
Code: Select all
Procedure.b GetIfPreferIpv6OverIpv4() ; if the return address is IPv6 address then your system is prefering IPv6 over IPv4
; return #True if IPv6 address returned
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $wc = new-object System.Net.WebClient" + #CRLF$ + "$wc.DownloadString('https://diagnostic.opendns.com/myip')" + #CRLF$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
Resultx$ = KeepOutCmd$
If GetTestIPv46Valid(Resultx$) = #True
If FindString(Resultx$, ":", 1, #PB_String_NoCase) > 0
gotipv6over4.b = #True
Else
gotipv6over4.b = #False
EndIf
EndIf
ProcedureReturn gotipv6over4
EndProcedure
Debug GetIfPreferIpv6OverIpv4()
Last edited by smacker on Wed Sep 14, 2016 8:52 pm, edited 3 times in total.
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.
Re: Powershell and Purebasic
Turns out Powershell in Win10 is kinda fun to play with and use, aside from the above already posted here is another example - getting values from Win10 registry and using a little WMI:
getting some stuff from the registry:
and using a little WMI via powershell:
Need to test if a web site is up or down ?
or... if you do not need the site content if its up and just need a status, then this one:
the above is fine for web sites, but how do you tell if a server (e.g... whoami or dns server or a web site server if the web site is down) is up? we can also do this with powershell
something I found out : some powershell commandlets will take a string directly for items in the comandlet command line and others need escaped strings so the new purebasic EscapeString(....) function comes in handy, so when escape is needed > e.g...
Result$ = EscapeString(Chr(34) + "itemtextstringneedingescape" + Chr(34), #PB_String_EscapeInternal) ; for text passing to powershell commandlet always use the #PB_String_EscapeInternal mode
getting some stuff from the registry:
Code: Select all
Procedure.s GetRegValueWinInfo(valuetogetname$) ; powershell v 5.0 in Win10 - with fixed path
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name " + valuetogetname$, "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
If FindString(KeepOutCmd$, "does not exist at path", 1, #PB_String_NoCase) > 0 Or FindString(KeepOutCmd$, "Cannot find path", 1, #PB_String_NoCase) > 0
If FindString(KeepOutCmd$, "does not exist at path", 1, #PB_String_NoCase) > 0
ProcedureReturn valuetogetname$ + " value does not exist at path"
EndIf
If FindString(KeepOutCmd$, "Cannot find path", 1, #PB_String_NoCase) > 0
ProcedureReturn "Cannot find path"
EndIf
Else
ProcedureReturn KeepOutCmd$
EndIf
EndProcedure
Debug GetRegValueWinInfo("'InstallTime'") ; REG_QWORD
Debug GetRegValueWinInfo("'DigitalProductId'") ; REG_BINARY
Debug GetRegValueWinInfo("'CurrentMajorVersionNumber'") ; REG_DWORD
Debug GetRegValueWinInfo("'ProductName'") ; REG_SZ
Debug GetRegValueWinInfo("'UBR'") ; REG_SZ
Debug GetRegValueWinInfo("'BuildLabEx'") ; REG_SZ
Debug GetRegValueWinInfo("'ReleaseId'") ; REG_SZ
Debug GetRegValueWinInfo("'InstallationType'") ; REG_SZ
Debug GetRegValueWinInfo("'CurrentBuild'") ; REG_SZ
Procedure.s GetRegValueWinInfoVar(pathreg$, valuetogetname$) ; powershell v 5.0 in Win10 - user choice path so change pathval$ to desired path using the format shownn
pathval$ = " Get-ItemPropertyValue -Path " + pathreg$ + " -Name " + valuetogetname$
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", pathval$, "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
If FindString(KeepOutCmd$, "does not exist at path", 1, #PB_String_NoCase) > 0 Or FindString(KeepOutCmd$, "Cannot find path", 1, #PB_String_NoCase) > 0
If FindString(KeepOutCmd$, "does not exist at path", 1, #PB_String_NoCase) > 0
ProcedureReturn valuetogetname$ + " value does not exist at path"
EndIf
If FindString(KeepOutCmd$, "Cannot find path", 1, #PB_String_NoCase) > 0
ProcedureReturn "Cannot find path"
EndIf
Else
ProcedureReturn KeepOutCmd$
EndIf
EndProcedure
regkeypath$ = "'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'"
Debug GetRegValueWinInfoVAR(regkeypath$, "'InstallTime'") ; REG_QWORD
Debug GetRegValueWinInfoVar(regkeypath$, "'DigitalProductId'") ; REG_BINARY
Debug GetRegValueWinInfoVar(regkeypath$, "'CurrentMajorVersionNumber'") ; REG_DWORD
Debug GetRegValueWinInfoVar(regkeypath$, "'ProductName'") ; REG_SZ
Debug GetRegValueWinInfoVar(regkeypath$, "'UBR'") ; REG_SZ
Debug GetRegValueWinInfoVar(regkeypath$, "'BuildLabEx'") ; REG_SZ
Debug GetRegValueWinInfoVar(regkeypath$, "'ReleaseId'") ; REG_SZ
Debug GetRegValueWinInfoVar(regkeypath$, "'InstallationType'") ; REG_SZ
Debug GetRegValueWinInfoVar(regkeypath$, "'CurrentBuild'") ; REG_SZ
Code: Select all
Procedure.s GetNetAdapDNSOrder()
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Get-WmiObject Win32_NetworkAdapterConfiguration -computername . | select DNSServerSearchOrder", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
ProcedureReturn KeepOutCmd$
EndProcedure
Debug GetNetAdapDNSOrder()
Code: Select all
Procedure.s GetTestIfSiteIsUp(thesitetotest$) ; if site is down (or does not exist) PowerShell returns "Access down..." - good for IPv6 sites too
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " $url = " + "'" + thesitetotest$ + "'" + #CRLF$ + "$wc = New-Object System.Net.WebClient" + #CRLF$ + "Try{$wc.DownloadString($url)}" + #CRLF$ + "catch{Write-host -nonewline " + Chr(34) + "Access down..." + Chr(34) + "}","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
outx$ = KeepOutCmd$ ; if site is up this has web page content, if down (or does not exist) then has "Access down..." only
;{filter here for what you want, or if this is that, or use a true/false thing, etc.... e.g... the below
If outx$ = "Access down..."
ProcedureReturn "The " + thesitetotest$ + " site is down"
Else
ProcedureReturn "The " + thesitetotest$ + " site is up"
EndIf
; true/false
; If outx$ = "Access down..."
; ProcedureReturn #False ; site is not up
; Else
; ProcedureReturn #True ; site is up
; EndIf
EndProcedure
Debug GetTestIfSiteIsUp("http://www.purebasic.com")
Debug GetTestIfSiteIsUp("http://www.superpurebasic.com") ; does not exist
Code: Select all
Procedure.s GetTestIfSiteIsUp(thesitetotest$) ; if site is down or does not exist PowerShell returns an error of 'The remote name could not be resolved', else returns the site status code
; web site status codes here > https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
; 'wget' is an alias for 'Invoke-WebRequest'
; - good for IPv6 sites too
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " wget " + thesitetotest$ + " | % {$_.StatusCode}","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
If KeepOutCmd$ = "200" ; 200 = site OK - but may return other status codes indicating other things so if that's what you need then filter for it
ProcedureReturn "the site is up"
Else
ProcedureReturn "site is down or could not be found"
EndIf
EndProcedure
Debug GetTestIfSiteIsUp("http://www.purebasic.com")
Debug GetTestIfSiteIsUp("http://www.superpurebasic.com") ; does not exist
; Note that the actual full translation of the wget line use above is - $variable = Invoke-WebRequest $url | ForEach-Object {$PSItem | Select-Object -Property StatusCode} -
; And the Invoke-WebRequest can be used To download a file from the target site, however this is blocked For actual download in Windows 10 unless
; the 'Unblock-File' comandlet in Powershell is called so using the Unblock-File comandlet one could use this one liner To download a
; file from a web site somewhere e.g... http://somesitesomewhere/files/somefile.zip - the Invoke-WebRequest
; is safe To use here in the above because the because 'Unblock-File' comandlet is Not used in the above.
Code: Select all
Procedure.b GetTestIfSiteIsUpPing(thesitetotest$) ; use this for servers that are not web sites e.g... a dns server or a whoami server
; uses pings
; if you need certain number of pings or delay change -Count 1 -Delay 2
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " Test-Connection -ComputerName " + thesitetotest$ + " -Count 1 -Delay 2","", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
If FindString(KeepOutCmd$, "Time(ms)", 1, #PB_String_NoCase) > 0
siteupordown.b = #True
Else
siteupordown.b = #False
EndIf
ProcedureReturn siteupordown
EndProcedure
Debug GetTestIfSiteIsUpPing("whoami.akamai.com") ; for Ipv4
; does IPv6 too so if working with IPv6 ...
Debug GetTestIfSiteIsUpPing("2620:0:ccc::2") ; for IPv6
Result$ = EscapeString(Chr(34) + "itemtextstringneedingescape" + Chr(34), #PB_String_EscapeInternal) ; for text passing to powershell commandlet always use the #PB_String_EscapeInternal mode
Last edited by smacker on Sat Aug 27, 2016 2:03 pm, edited 11 times in total.
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.
Re: Powershell and Purebasic
Thanks smacker! One man's pain is another man's gain (sore leg)
Here's a recent article that gives hope to Linux and OS X users: http://www.powershellmagazine.com/2016/ ... x-and-osx/

Here's a recent article that gives hope to Linux and OS X users: http://www.powershellmagazine.com/2016/ ... x-and-osx/
- It was too lonely at the top.
System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Re: Powershell and Purebasic
Keep it BASIC.
Re: Powershell and Purebasic
yep, could do that, thanks for the snippet of code.netmaestro wrote:I wrote this some time ago. I couldn't remember when if you waterboarded me, not even if the Donald himself was on the bucket:You could modify this easily to take a string parameter and return any desired path that exists in the %PATH.Code: Select all
Procedure.s GetPath_PowerShell() Protected path$ = GetEnvironmentVariable("PATH"), ; List of all paths PowerShell$ = "PowerShell not found!", ; Initialize with failure, success will change it thispath$ = #Empty$, ; Temporary var to hold path for comparison i.i ; Loop counter set to number of paths available For i = 1 To CountString(path$, ";") + 1 thispath$ = StringField(path$, i, ";") If FindString(thispath$, "powershell", 1, #PB_String_NoCase) PowerShell$ = thispath$ Break EndIf Next ProcedureReturn PowerShell$ EndProcedure Debug GetPath_PowerShell()
If ya just wanna know if powershell is installed or not and don't need the path, so...
Code: Select all
Procedure.b GetPowerShellInstalled()
fndpsinst = RunProgram("powershell.exe", "", "", #PB_Program_Hide|#PB_Program_Open)
If fndpsinst > 0
KillProgram(fndpsinst)
CloseProgram(fndpsinst)
ProcedureReturn #True ; powershell installed
Else
ProcedureReturn #False ; powershell not installed
EndIf
EndProcedure
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.
Re: Powershell and Purebasic
Another example.
Code: Select all
Procedure.s CreateFirewallRulesForFirefoxTestExample()
DisplayNameOut$ = "'A Disconnect Block For Firefox Outbound Test'"
DisplayNameIn$ = "'A Disconnect Block For Firefox Inbound Test'"
ProgramPathName$ = "'%ProgramFiles%\Mozilla Firefox\firefox.exe'"
Description$ = "'A block for Firefox'"
LocalAddressToBlock$ = "'192.168.0.0/16'"
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " New-NetFirewallRule -DisplayName " + DisplayNameOut$ + " -Action Block -Description " + Description$ + " -Direction Outbound -LocalAddress " + LocalAddressToBlock$ + " -Program " + ProgramPathName$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmdA$ = KeepOutCmdA$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
If FindString(KeepOutCmdA$, "The rule was parsed successfully", 1) > 0
ruleA.b = #True
;ProcedureReturn "The firewall rule was successfully created"
Else
ruleA.b = #False
;ProcedureReturn "The firewall rule was NOT successfully created"
EndIf
Delay(1000)
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", " New-NetFirewallRule -DisplayName " + DisplayNameIn$ + " -Action Block -Description " + Description$ + " -Direction Inbound -LocalAddress " + LocalAddressToBlock$ + " -Program " + ProgramPathName$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmdB$ = KeepOutCmdB$ + OutCmd$
Wend
CloseProgram(CmdPromptRun)
EndIf
If FindString(KeepOutCmdB$, "The rule was parsed successfully", 1) > 0
ruleB.b = #True
;ProcedureReturn "The firewall rule was successfully created"
Else
ruleB.b = #False
;ProcedureReturn "The firewall rule was NOT successfully created"
EndIf
If ruleA = #True And ruleB = #True
ProcedureReturn "The firewall rules for Inbound and Outbound were successfully created."
EndIf
If ruleA = #True And ruleB = #False
ProcedureReturn "The firewall rule for Inbound was successfully created but creation of the Outbound rule failed."
EndIf
If ruleA = #False And ruleB = #True
ProcedureReturn "The firewall rule for Outbound was successfully created but creation of the Inbound rule failed."
EndIf
If ruleA = #False And ruleB = #False
ProcedureReturn "The creation of the firewall rules for Outbound and Inbound failed."
EndIf
EndProcedure
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.
Re: Powershell and Purebasic
This is a kinda neat use - internet speed test;
if you don't like the idea of the test file in the above being written to a drive then remove or comment out the 'drvwininst$' and ' Drvfile$' lines and use this 'cmdline$' instead:
Every 'error' powershell throws either begins with, or has, the word ' Exception', so if you want a simple error trapping thing then replace this section :
with this (or something similar concept you come up with):
When an internet speed test site, like the one used here, says they send you a file to test did you ever wonder where this file went or what was in it? The truth is they don't actually send you a file, what they do is load up the content output in the response to your browser (into your browser) with the amount of data they send (i.e.. in the above examples 100 Mb of data). You can see the data sent if you start up powershell and copy and paste the below into a powershell window at its prompt then hit enter and wait about a minute (actually a little less):
This can be leveraged to our advantage if you do not like the idea of a file being d/l to your system for the speed test as basically the so called data file is really a big string. Although the data string sent in this speed test is harmless, if I were producing a speed test application for distribution i'd want to try and keep away from placing anything on the users system so as to not cause concern. So here is how to leverage the idea this is just really a big string being sent:
In this one 'downloadstring' method example above, powershells 'Out-NUll' is the (basically) equal of the Linux/unix 'dev/null'. In other words the 'Out-NUll' is a black hole in which anything sent to it is obliterated and goes no where. So in the 'downloadstring' method example above we get the advantage of the 'file' download measurement and don't need to worry about placing anything on the users system and in all the above examples we do not need to consider web browser interaction (which actually slews the test results some depending on the browser and system function at the moment). My least chosen method would be the second example, not that its bad but because its still possible on some systems for a file to be written to the users computer in some form temporarily or the user system condition to slew the result some and although unlikely in most cases of well functioning systems if the goal is to not write a test file to the users computer then to be sure the third method 'downloadstring' method method would probably be best to use as 'Out-NUll' does not care about the users system interaction. Now, the bad news about speed tests on the internet - A connection speed test is only reasonably valid (for a range) within the users ISP network and then only with the measurement method provided by the ISP, the reason for this is because 'connection speed' is always relative to the end point and the entire (user accessable) ISP network is an end point for the user and the only end point for the user for which the conditions are known and measurable by the ISP. In other words, another way of putting it, the connection speed one gets at any given moment is what the end point will give and the user has absolutely zero control over it (given that everything is working on the user end properly). So although one may use a speed test site and brag about (or not brag about) their connection speed it means absolutely nothing and really can not be used for comparing one thing to another, for example saying that "when I connect to site xyz.com I only get 10 Mbps but the speed test site says I am getting 50 Mbps" is an absolutely useless thing to say and means nothing, the reason is that connection speed is always, and only, relative to the end point and no where else and if you only get, for example, 10 Mbps from the end point on an ISP rated 50 Mbps connection then that's what you get and it does not mean there is a problem. (note: the 'end point' results also includes the path between the user and the site end point. i.e. the latency for example). Another thing, sometimes people will say "oh the speed test site says I'm getting 100 Mpbs and my ISP connection is only 50 Mbps", or similar, well, first, its a physics impossibility to have more bandwidth than your ISP will allow which means your connection speed can not exceed that permitted by your allotted bandwidth unless basically you found a way to exceed the speed of light from your computer, and second, see first. The speedtest.net site and others are infamous for telling people they have, for example, 25% to 75% more connection speed than is a physics possibility and ISP's will frequently use these sites to back up how well they say they perform over other ISP's. If you have been or are chasing that speed test site result trying to get that connection speed everywhere on the internet then you have been or are wasting your time along with thousands or millions of others. Once your packets leave the ISP network all bets for connection speed are off and at the mercy of the internet. The only way to get a somewhat reasonable assessment of connection speed for a specific end point is to download an actual file to your system from the end point and measure the time it takes (which is something ISP's do not like people to know so they always say use "speedtest site so and so"), your computer is also part of the path and excluding that by not writing an actual file to the system can slew the results greatly (depending on the users system), so using the first method I outlined in this post where an actual file is written to the system and then deleted is probably the best although the other two examples will work too for a general thing (if considering the above), but remember that the test is only valid to the test site end point used and no where else on the internet so do not expect to get the connection speed given in the results elsewhere or everywhere on the internet.
Code: Select all
Procedure.s GetDownloadConSpeedPeak()
; note: writes a test use file to the root of the drive you choose in drvwininst$ (can also choose a directory), the file is called "out-null" (no . extension)
; after test complete the test file is deleted. Length of time depends on how fast your connection is, a connection running around 50 Mbit/s the run time is about 1 minute and 20 seconds to
; complete which is about the same time it takes, give or take, using a browser and going to 'http://testmy.net/dl-100MB' to run the test in the browser
drvwininst$ = "C:\"
Drvfile$ = Chr(34) + Chr(39) + drvwininst$ + "out-null" + Chr(39) + Chr(34)
cmdline$ = "$wc = New-Object net.webclient; '{0:N2}' -f ((100/(Measure-Command {$wc.Downloadfile('http://testmy.net/dl-100MB'," + Drvfile$ + ")}).TotalSeconds)*8); del " + Drvfile$
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", cmdline$, "", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
KillProgram(CmdPromptRun)
CloseProgram(CmdPromptRun)
EndIf
dlspd$ = Trim(RemoveString(KeepOutCmd$, "-", #PB_String_NoCase, 1))
mbytesec.f = ValF(dlspd$) / 8
mbsecstr$ = StrF(mbytesec, 2)
dlspd$ = dlspd$ + " Mbit/s (megabit per second) Peak { = " + mbsecstr$ + " MB/s (MegaBytes Per Second) Peak}"
ProcedureReturn dlspd$
EndProcedure
Debug GetDownloadConSpeedPeak()
Code: Select all
cmdline$ = "$wc = New-Object net.webclient; '{0:N2}' -f ((100/(Measure-Command {$wc.Downloadfile('http://testmy.net/dl-100MB'," + "[ref] $NULL" + ")}).TotalSeconds)*8)"
Code: Select all
dlspd$ = Trim(RemoveString(KeepOutCmd$, "-", #PB_String_NoCase, 1))
mbytesec.f = ValF(dlspd$) / 8
mbsecstr$ = StrF(mbytesec, 2)
dlspd$ = dlspd$ + " Mbit/s (megabit per second) Peak { = " + mbsecstr$ + " MB/s (MegaBytes Per Second) Peak}"
Code: Select all
If CountString(KeepOutCmd$, "Exception") = 0
dlspd$ = Trim(RemoveString(KeepOutCmd$, "-", #PB_String_NoCase, 1))
mbytesec.f = ValF(dlspd$) / 8
mbsecstr$ = StrF(mbytesec, 2)
dlspd$ = dlspd$ + " Mbit/s (megabit per second) Peak { = " + mbsecstr$ + " MB/s (MegaBytes Per Second) Peak}"
Else
dlspd$ = "Not able to test at this time" ; or you could just display the error
EndIf
Code: Select all
$wc = new-object System.Net.WebClient;
$wc.DownloadString('http://testmy.net/dl-100MB')
Code: Select all
Procedure.s GetDownloadConSpeedPeak() ; download string to out-null version
cmdline$ = "$wc = New-Object System.Net.WebClient; '{0:N2}' -f ((100/(Measure-Command {$wc.DownloadString('http://testmy.net/dl-100MB') | Out-Null}).TotalSeconds)*8)"
OutCmd$ = ""
KeepOutCmd$ = ""
CmdPromptRun = RunProgram("powershell.exe", cmdline$, "", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If CmdPromptRun
While ProgramRunning(CmdPromptRun)
OutCmd$=ReadProgramString(CmdPromptRun)
KeepOutCmd$ = KeepOutCmd$ + OutCmd$
Wend
KillProgram(CmdPromptRun)
CloseProgram(CmdPromptRun)
EndIf
If CountString(KeepOutCmd$, "Exception") = 0
dlspd$ = Trim(RemoveString(KeepOutCmd$, "-", #PB_String_NoCase, 1))
mbytesec.f = ValF(dlspd$) / 8
mbsecstr$ = StrF(mbytesec, 2)
dlspd$ = dlspd$ + " Mbit/s (megabit per second) Peak { = " + mbsecstr$ + " MB/s (MegaBytes Per Second) Peak}"
Else
dlspd$ = "Not able to test at this time"
EndIf
ProcedureReturn dlspd$
EndProcedure
In this one 'downloadstring' method example above, powershells 'Out-NUll' is the (basically) equal of the Linux/unix 'dev/null'. In other words the 'Out-NUll' is a black hole in which anything sent to it is obliterated and goes no where. So in the 'downloadstring' method example above we get the advantage of the 'file' download measurement and don't need to worry about placing anything on the users system and in all the above examples we do not need to consider web browser interaction (which actually slews the test results some depending on the browser and system function at the moment). My least chosen method would be the second example, not that its bad but because its still possible on some systems for a file to be written to the users computer in some form temporarily or the user system condition to slew the result some and although unlikely in most cases of well functioning systems if the goal is to not write a test file to the users computer then to be sure the third method 'downloadstring' method method would probably be best to use as 'Out-NUll' does not care about the users system interaction. Now, the bad news about speed tests on the internet - A connection speed test is only reasonably valid (for a range) within the users ISP network and then only with the measurement method provided by the ISP, the reason for this is because 'connection speed' is always relative to the end point and the entire (user accessable) ISP network is an end point for the user and the only end point for the user for which the conditions are known and measurable by the ISP. In other words, another way of putting it, the connection speed one gets at any given moment is what the end point will give and the user has absolutely zero control over it (given that everything is working on the user end properly). So although one may use a speed test site and brag about (or not brag about) their connection speed it means absolutely nothing and really can not be used for comparing one thing to another, for example saying that "when I connect to site xyz.com I only get 10 Mbps but the speed test site says I am getting 50 Mbps" is an absolutely useless thing to say and means nothing, the reason is that connection speed is always, and only, relative to the end point and no where else and if you only get, for example, 10 Mbps from the end point on an ISP rated 50 Mbps connection then that's what you get and it does not mean there is a problem. (note: the 'end point' results also includes the path between the user and the site end point. i.e. the latency for example). Another thing, sometimes people will say "oh the speed test site says I'm getting 100 Mpbs and my ISP connection is only 50 Mbps", or similar, well, first, its a physics impossibility to have more bandwidth than your ISP will allow which means your connection speed can not exceed that permitted by your allotted bandwidth unless basically you found a way to exceed the speed of light from your computer, and second, see first. The speedtest.net site and others are infamous for telling people they have, for example, 25% to 75% more connection speed than is a physics possibility and ISP's will frequently use these sites to back up how well they say they perform over other ISP's. If you have been or are chasing that speed test site result trying to get that connection speed everywhere on the internet then you have been or are wasting your time along with thousands or millions of others. Once your packets leave the ISP network all bets for connection speed are off and at the mercy of the internet. The only way to get a somewhat reasonable assessment of connection speed for a specific end point is to download an actual file to your system from the end point and measure the time it takes (which is something ISP's do not like people to know so they always say use "speedtest site so and so"), your computer is also part of the path and excluding that by not writing an actual file to the system can slew the results greatly (depending on the users system), so using the first method I outlined in this post where an actual file is written to the system and then deleted is probably the best although the other two examples will work too for a general thing (if considering the above), but remember that the test is only valid to the test site end point used and no where else on the internet so do not expect to get the connection speed given in the results elsewhere or everywhere on the internet.
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.