|
|
|||||||
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 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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? |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
Many thanks Shane, you are great! |
||||||||
|
|
|||||||
The output is Unicode, not "Funky-Code" 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 Glenn PS - the FileIO() function is posted in the UDFs Forum. |
||||||||
|
|
|||||||
Quote: The output is Unicode, not "Funky-Code" |
||||||||
|
|
|||||||
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. 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 |
||||||||
|
|
|||||||
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. |