Page 1 of 1 1
Topic Options
#212890 - 2017-11-16 08:35 PM Why I'm unable to verify the output correctly ?
Patrick99 Offline
Fresh Scripter

Registered: 2016-02-19
Posts: 14
Loc: Belgium
Hello,
I would like to verify the outcome of the Powershell command in an txt file , however It looks like I cannot, can someone help me?
script:
 Code:
Shell "Powershell Set-ExecutionPolicy RemoteSigned"
Shell "Powershell get-ExecutionPolicy > c:\temp\PSSec.txt"

$fileToCheck = "c:\temp\PSSec.txt"
$stringToFind = "RemoteSigned"
			
If Exist($fileToCheck)
	$stringFound = 0
	$fileContents = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile($fileToCheck, 1, Not 1).ReadAll, @CRLF)
	For Each $line in $fileContents
		If InStr($line, $stringToFind)
			$stringFound = 1
		EndIf
	Next
	If $stringFound
		MessageBox("Status OK ! ", "ICT - INFO", 64, 1)
	Else 
		MessageBox("Status NOT OK ! ", "ICT - INFO", 64, 1)
	EndIf
EndIf


Edited by ShaneEP (2017-11-17 10:25 PM)
Edit Reason: put code in code tags

Top
#212891 - 2017-11-16 10:43 PM Re: Why I'm unable to verify the output correctly ? [Re: Patrick99]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
I can't get it to fail on my computer. Maybe some security issue with the FSO? Stupied question...But you have verified the PS part is working, and the text is in the text file?

Have you tried using native kix functions only?
 Code:
$fileToCheck = "c:\temp\PSSec.txt"
$stringToFind = "RemoteSigned"

If Exist($fileToCheck)
   $stringFound = 0
   $fh = FreeFileHandle()
   $nul = Open($fh, $fileToCheck)
   While @error = 0
      $line = ReadLine($fh)
      If InStr($line, $stringToFind)
         $stringFound = 1
      EndIf
   Loop
   If $stringFound
      $nul = MessageBox("Status OK ! ", "ICT - INFO", 64, 3)
   Else 
      $nul = MessageBox("Status NOT OK ! ", "ICT - INFO", 64, 3)
   EndIf
EndIf

Top
#212892 - 2017-11-16 10:45 PM Re: Why I'm unable to verify the output correctly ? [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
When I try it with the PS portion as well...The first line doesn't change anything. I still just get 'Restricted' in the output file.
Top
#212893 - 2017-11-17 07:26 AM Re: Why I'm unable to verify the output correctly ? [Re: ShaneEP]
Patrick99 Offline
Fresh Scripter

Registered: 2016-02-19
Posts: 14
Loc: Belgium
Hello Shane,
The issue is not changing the status but checking the status in the txt file. The file contains just "Restricted" same as your output, so when you change the
$stringToFind = "RemoteSigned" to $stringToFind = "Restricted",do you get it right now?
For meself it didn't work, however I do the same with powershell it get the status OK.
Why is the status NOK with kix?
does it work for you?

Top
#212897 - 2017-11-17 10:33 PM Re: Why I'm unable to verify the output correctly ? [Re: Patrick99]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
The PS seems to be creating some kind of funky text file. If I add code to display the lines as they're read, it displays this...
 Quote:

ÿ_R

Top
#212898 - 2017-11-17 10:38 PM Re: Why I'm unable to verify the output correctly ? [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
If I use the code I posted above, which uses built in Kix functions. It still gets funky results, but at least usable. It shows...
 Quote:

ÿ_R
e
m
o
t
e
S
i
g
n
e
d

So try this code, and see if it gets you where you need.
 Code:
Shell "Powershell Set-ExecutionPolicy RemoteSigned"
Shell "Powershell get-ExecutionPolicy > c:\temp\PSSec.txt"

$fileToCheck = "c:\temp\PSSec.txt"
$stringToFind = "RemoteSigned"

If Exist($fileToCheck)
   $stringFound = 0
   $fh = FreeFileHandle()
   $nul = Open($fh, $fileToCheck)
   While @error = 0
      $line = ReadLine($fh)
      $text = $text + $line
   Loop
   If InStr($text, $stringToFind)
      $stringFound = 1
   Endif
   If $stringFound
      $nul = MessageBox("Status OK ! ", "ICT - INFO", 64, 3)
   Else 
      $nul = MessageBox("Status NOT OK ! ", "ICT - INFO", 64, 3)
   EndIf
Endif


Edited by ShaneEP (2017-11-17 10:39 PM)

Top
#212900 - 2017-11-18 01:59 PM Re: Why I'm unable to verify the output correctly ? [Re: ShaneEP]
Patrick99 Offline
Fresh Scripter

Registered: 2016-02-19
Posts: 14
Loc: Belgium
Many thanks Shane, you are great!
Top
#212902 - 2017-11-18 05:01 PM Re: Why I'm unable to verify the output correctly ? [Re: Patrick99]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
The output is Unicode, not "Funky-Code" \:D
Don't redirect from PS, use the Out-File method and specify ASCII encoding.
 Code:
; Get the FileIO function from the library
Call 'K:\KixLib\FileIO.kxf'

; Run the PowerShell command forcing ASCII output (default is "Unicode", not "Funky-Code"
Shell "Powershell get-ExecutionPolicy | Out-File -encoding ascii c:\temp\PSSec.txt"

; Read file as one big string - This UDF is faster that the FSO method
; FileIO returns the file contents as an array of lines - the Join() converts the entire 
; file into a single string
$FileContents = Join(FileIO($FileToCheck, 'R'), @CRLF)

; See if the result is found.
If InStr($FileContents, $StringToFind)
  'Found!' ?
Else
  'not found...' ?
EndIf
Works flawlessly.

Glenn

PS - the FileIO() function is posted in the UDFs Forum.


Edited by Glenn Barnas (2017-11-18 05:33 PM)
Edit Reason: Added UDF link
_________________________
Actually I am a Rocket Scientist! \:D

Top
#212903 - 2017-11-19 09:30 AM Re: Why I'm unable to verify the output correctly ? [Re: Glenn Barnas]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
 Quote:
The output is Unicode, not "Funky-Code"
Yea I had assumed it was probably something like that. I was just too lazy to confirm it. Either way...The code I posted solved the issue, without an addition UDF.


Edited by ShaneEP (2017-11-19 09:31 AM)

Top
#212904 - 2017-11-19 03:41 PM Re: Why I'm unable to verify the output correctly ? [Re: ShaneEP]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
No additional UDF is required - just the change to the PowerShell command to force ASCII and things will work "normally". I just pointed out that the FileIO UDF is faster than FSO in most cases.. in this case, it was over 3x faster. OF course, when it takes several seconds to instantiate PowerShell, saving 300ms is not a game-changer. \:D

I did test the code here with both FSO read and FileIO, which is how I found that it was 300ms faster. FileIO is my go-to UDF for file management - every line is an array record, and the same function can read, append, or write to a file.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#212909 - 2017-11-19 06:12 PM Re: Why I'm unable to verify the output correctly ? [Re: Glenn Barnas]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
The weird thing is, I had actually tried using the FSO function in the original post, but with the Unicode option, and it still didn’t read it correctly. Which is why I wasn’t sure. But yea I’d agree just forcing it to use ascii from the start is probably the best.
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.069 seconds in which 0.026 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org