Patrick99
(Fresh Scripter)
2017-11-16 08:35 PM
Why I'm unable to verify the output correctly ?

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


ShaneEP
(MM club member)
2017-11-16 10:43 PM
Re: Why I'm unable to verify the output correctly ?

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


ShaneEP
(MM club member)
2017-11-16 10:45 PM
Re: Why I'm unable to verify the output correctly ?

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.

Patrick99
(Fresh Scripter)
2017-11-17 07:26 AM
Re: Why I'm unable to verify the output correctly ?

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?


ShaneEP
(MM club member)
2017-11-17 10:33 PM
Re: Why I'm unable to verify the output correctly ?

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


ShaneEP
(MM club member)
2017-11-17 10:38 PM
Re: Why I'm unable to verify the output correctly ?

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


Patrick99
(Fresh Scripter)
2017-11-18 01:59 PM
Re: Why I'm unable to verify the output correctly ?

Many thanks Shane, you are great!

Glenn BarnasAdministrator
(KiX Supporter)
2017-11-18 05:01 PM
Re: Why I'm unable to verify the output correctly ?

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.


ShaneEP
(MM club member)
2017-11-19 09:30 AM
Re: Why I'm unable to verify the output correctly ?

 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.


Glenn BarnasAdministrator
(KiX Supporter)
2017-11-19 03:41 PM
Re: Why I'm unable to verify the output correctly ?

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


ShaneEP
(MM club member)
2017-11-19 06:12 PM
Re: Why I'm unable to verify the output correctly ?

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.