PS1 section not working
› Forums › Desktop Info › PS1 section not working
Tagged: PowerShell SET-SECTION CMD
- This topic has 4 replies, 2 voices, and was last updated 1 month ago by
Nathan_K.
-
AuthorPosts
-
-
25 March, 2023 at 7:59 am #5803
I’ve had some issues with GETHTTP not returning data consistently, so I decided to write a small PS1 section to do the same task. The script I wrote works perfectly in a PowerShell window, but when I put it into a [section] it doesn’t work.
1234567891011121314## v1.0 -- Retrieve IPv4, IPv6 and Reverse DNS for IPv4## Output headers# Write-Host wan_ipv4 ',' wan_ipv6 ',' wan_rdnsTry { $IPv4_addr = (Invoke-WebRequest -Uri https://ip4.seeip.org/ -UseBasicParsing -ErrorAction 'Stop' | Select -ExpandProperty Content) }#Catch { $IPv4_addr = "" }Try { $IPv6_addr = (Invoke-WebRequest -Uri https://ip6.seeip.org/ -UseBasicParsing -ErrorAction 'Stop' | Select -ExpandProperty Content) }Catch { $IPv6_addr = "" }Try { $IPv4_rdns = (Invoke-WebRequest -Uri https://ipinfo.io/$IPv4_addr/hostname -UseBasicParsing -ErrorAction 'Stop' | Select -ExpandProperty Content) }Catch { $IPv4_rdns = "" }Write-Host $IPv4_addr ',' $IPv6_addr ',' $IPv4_rdnsExit 0If I use it as a PowerShell script call, it works:
CMD=interval:120,color:55cc77,text:WAN Address,read-as-csv:1,csv-header:0,file:powershell.exe, parameters:".\Scripts\wan-ip-lookup.ps1",trim:1,show-window:0,hide-no-result:1, display:{{{IPv4: %1 }}}{{{IPv6: %2}}}{{{ |Reverse DNS: %3}}}
If I call it directly from the [wan-ip-lookup] section I created, I get errors.
12SET-SECTION=key:wan-ip-lookup,value:wan-ip-lookup.ps1CMD=interval:120,color:55cc77,text:WAN Address,read-as-csv:1,csv-header:0,file:powershell.exe, parameters:-Command "& { %wan-ip-lookup% }",trim:1,show-window:0,hide-no-result:1, display:{{{IPv4: %1 }}}{{{IPv6: %2}}}{{{ |Reverse DNS: %3}}}Any assistance would be appreciated, as I’d rather have it contained in a section vs an external script.
Thanks!
-
25 March, 2023 at 8:02 am #5806
I’ll look into this shortly.
-
3 April, 2023 at 10:23 am #5822
I’ll try and convey this without mangling the code:
1234567891011121314[wan-ip-lookup.ps1]## v1.0 -- Retrieve IPv4, IPv6 and Reverse DNS for IPv4## Output headers# Write-Host wan_ipv4 "," wan_ipv6 "," wan_rdnsTry { $IPv4_addr = (Invoke-WebRequest -Uri https://ip4.seeip.org/ -UseBasicParsing -ErrorAction "Stop" | Select -ExpandProperty Content) }#Catch { $IPv4_addr = '' }Try { $IPv6_addr = (Invoke-WebRequest -Uri https://ip6.seeip.org/ -UseBasicParsing -ErrorAction "Stop" | Select -ExpandProperty Content) }Catch { $IPv6_addr = '' }Try { $IPv4_rdns = (Invoke-WebRequest -Uri https://ipinfo.io/$IPv4_addr/hostname -UseBasicParsing -ErrorAction "Stop" | Select -ExpandProperty Content) }Catch { $IPv4_rdns = '' }Write-Host $IPv4_addr","$IPv6_addr","$IPv4_rdns12SET-SECTION=key:wan-ip-lookup,value:wan-ip-lookup.ps1CMD=interval:120,color:55cc77,text:WAN Address,read-as-csv:1,csv-header:0,file:powershell.exe, parameters:-Command %wan-ip-lookup%,trim:1,show-window:0,hide-no-result:1, display:IPv4: %1 IPv6: %2 |Reverse DNS: %3getting the single/double quote characters right is all important. the third return value has something in there that’s messing up the csv output.
probably worth adding no-wait:1 as well.
-
3 April, 2023 at 11:31 am #5825
Ok, this version sorts out the null values:
12345678910111213141516[wan-ip-lookup.ps1]## v1.0 -- Retrieve IPv4, IPv6 and Reverse DNS for IPv4## Output headers# Write-Host wan_ipv4 "," wan_ipv6 "," wan_rdnsTry { $IPv4_addr = (Invoke-WebRequest -Uri https://ip4.seeip.org/ -UseBasicParsing -ErrorAction "Stop" | Select -ExpandProperty Content) }#Catch { $IPv4_addr = '' }Try { $IPv6_addr = (Invoke-WebRequest -Uri https://ip6.seeip.org/ -UseBasicParsing -ErrorAction "Stop" | Select -ExpandProperty Content) }Catch { $IPv6_addr = '' }Try { $IPv4_rdns = (Invoke-WebRequest -Uri https://ipinfo.io/$IPv4_addr/hostname -UseBasicParsing -ErrorAction "Stop" | Select -ExpandProperty Content) }Catch { $IPv4_rdns = '' }if($IPv4_addr.Trim().Length -eq 0) {$IPv4_addr='<null>'}if($IPv6_addr.Trim().Length -eq 0) {$IPv6_addr='<null>'}if($IPv4_rdns.Trim().Length -eq 0) {$IPv4_rdns='<null>'}Write-Host $IPv4_addr','$IPv6_addr','$IPv4_rdns12SET-SECTION=key:wan-ip-lookup,value:wan-ip-lookup.ps1CMD=interval:120,color:55cc77,text:WAN Address,read-as-csv:1,csv-header:0,file:powershell.exe, parameters:-Command %wan-ip-lookup%,trim:1,show-window:0,hide-no-result:1, display:{{{IPv4: %1 }}}{{{IPv6: %2}}}{{{|Reverse DNS: %3}}}-
This reply was modified 1 month ago by
Glenn.
-
This reply was modified 1 month ago by
-
4 April, 2023 at 4:26 pm #5835
Thanks Glenn.. I had made several revisions to the original PS1 since posting, so here’s the updated version as a block that works inside the .ini file.
123456789101112131415161718192021222324[wan-ip-lookup.ps1]## v1.0 -- Retrieve IPv4, IPv6 and Reverse DNS for IPv4## v1.1 -- Updated to use Invoke-RestMethod instead of Invoke-WebRequest## v1.2 -- Added secondary Uri for IPv4 and IPv6## Output headersWrite-Host wan_ipv4 ',' wan_rdns ',' wan_ipv6Try { $IPv4_addr = (Invoke-RestMethod -Uri https://ip4.seeip.org/ -UseBasicParsing -ErrorAction "Stop") }Catch { Try { $IPv4_addr = (Invoke-RestMethod -Uri https://4.tnedi.me/ -UseBasicParsing -ErrorAction "Stop") }Catch { $IPv4_addr = 'n/a' }}Try { $IPv6_addr = (Invoke-RestMethod -Uri https://ip6.seeip.org/ -UseBasicParsing -ErrorAction "Stop") }Catch { Try { $IPv6_addr = (Invoke-RestMethod -Uri https://6.tnedi.me/ -UseBasicParsing -ErrorAction "Stop") }Catch { $IPv6_addr = 'n/a' }}Try {$IPv4_rdns = (Invoke-RestMethod -Uri https://ipinfo.io/$IPv4_addr/hostname -UseBasicParsing -ErrorAction "Stop").Trim()If ( -not $IPv4_rdns ) { $IPv4_rdns = 'No record found' }}Catch { $IPv4_rdns = 'n/a' }Write-Host $IPv4_addr ',' $IPv4_rdns ',' $IPv6_addrExit 01234SET-SECTION=key:wan-ip-lookup,value:wan-ip-lookup.ps1CMD=interval:120,color:55cc77,text:WAN Address,read-as-csv:1,csv-header:1,file:powershell.exe, \parameters:-Command %wan-ip-lookup%,trim:1,show-window:0,hide-no-result:1, \display:{{{IPv4: %1}}}{{{ IPv6: %3}}}{{{ |Reverse DNS: %2}}}
-
-
AuthorPosts
- You must be logged in to reply to this topic.