Sign in to follow this  
Followers 0
Jibberish

Run Powershell command line from AutoIt

2 posts in this topic

Hi all,

I need to read a log file into an array, but the log file is encoded as $FO_UTF16_BE_NOBOM (2048) = Use Unicode UTF16 Big Endian (without BOM) per FileGetEncoding (it returns 2048).

I have searched how to convert these log files to UTF-8 and finally found a Powershell command. Since then I have been racking my brain trying to get the function to work. The command itself works from a Powerscript prompt:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-Content C:\Logs\Myplayer_10-10-17-02-31.log | Set-Content -Encoding utf8 C:\Logs\Myplayer1.log

This is my sandbox;

#include <array.au3>
#include <File.au3>

Local $aArrayLogFile
Local $sLogDir = "C:\Logs\"
Local $sLogFile = "Myplayer_10-10-17-02-31.log"
Local $sConvertedLog = "ConvertedLog.log"
Local $sLogDirFile = $sLogDir&$sLogFile

RunWait("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-Content "&$sLogDirFile&" | Set-Content -Encoding utf8 "&$sConvertedLog,$sLogDir)

_FileReadToArray($sLogDirFile, $aArrayLogFile)
_ArrayDisplay($aArrayLogFile)

Also tried

RunWait("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-Content "&$sLogDirFile&" | Set-Content -Encoding utf8 "&$sConvertedLog,$sLogDir)

and

ShellExecuteWait("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"," -Command Get-Content "&$sLogDirFile&" | Set-Content -Encoding utf8 "&$sConvertedLog,$sLogDir)

Tried without -Command and a bunch of other parameters that were sprinkled throughout the internet from people trying to get this to work.

Thanks

Jibs

Share this post


Link to post
Share on other sites



This one works for me :

RunWait("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-Content 'c:\SessionChange_20.06.2016 _ 12.53.11.log' | Set-Content -Encoding utf8 'c:\SessionChange_20.06.2016 _ 12.53.11222.log'")

I have done it with hard coded names for simplicity - using variables also works.

1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • Jibberish
      By Jibberish
      I need to read log files into an array to search for errors. However when I display the array I get garbage or "chinese characters". Our developers say they are using UTF-8, but FileGetEncoding says the logs are "2048" or $FO_UTF16_BE_NOBOM (2048) = Use Unicode UTF16 Big Endian (without BOM) from the Encoding codes in FileOpen().
      There is an app called Detenc that detects the encoding used by files. You have to guess, but it returns correctly when I set the Encoder for UTF-8. I understand Encoding is not etched in stone, but the first character of the file is a capital B, using HxD Hex Editor.
      I even have another  topic here about running PowerShell to reencode the file so AutoIt will store the file properly in the array - See:
      So I am trying to figure out why AutoIt thinks my logs are not UTF-8.
      Here is sample code:
      #include <array.au3> #include <File.au3> Local $aRetArrayFile _FileReadToArray("C:\Logs\Myplayer1.log", $aRetArrayFile) _ArrayDisplay($aRetArrayFile) I won't post the results as it is illegible, but I did attach a screenshot of the _ArrayDisplay results, and this is the first line of the Log file:
      BANNER 10/10/2017 15:56:00 ====================================================================== And the Hex from the beginning of the file:
      42 41 4E 4E 45 52 20 31 30 2F 31 30 2F 32 30 31 37 20 31 34 3A 33 31 3A 33 35 20 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 0D 0A 42 41 4E 4E 45 52 20 So I don't understand why AutoIt thinks the file is UTF16 BE.
      If I can get the Powershell script running, I have a workaround.
      BTW none of my other arrays display as garbage, just the log files.
      Weird.
      Rereading my post, what seems to be missing is the question. I guess my question is, does anyone know why these logs are being displayed incorrectly?
      Cheers
      Jibs

    • ur
      By ur
      I am maintaining all the reusable code in a separate file as library.au3.
      In that file I have referenced some dependent files using fileinstall, so that they will be extracted when necessary.
       
      Problem is, if I use a function in the library.au3 in another script which doesn't require this dependent file, as I am including the whole file using include tag, it is embedding that file also.
      Is there any way to exclude that.
       
    • david1337
      By david1337
      Hi guys,
      I need a little help here
      I have this simple Powershell script, that is able to set "sig.htm" as signature in OWA for the mail account specified.
      First part of the script just loads the Exchange snapin, since this is needed for the Get-Mailbox command.
      #Add Exchange 2010/2013 snapin if not already loaded in the PowerShell session if (!(Get-PSSnapin | where {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) { try { Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction STOP } catch { #Snapin was not loaded Write-Warning $_.Exception.Message EXIT } . $env:ExchangeInstallPath\bin\RemoteExchange.ps1 Connect-ExchangeServer -auto -AllowClobber } $mailboxes = Get-Mailbox -Identity user@domain.com $mailboxes| foreach {$file= "sig.htm"; Set-MailboxMessageConfiguration -identity $_.alias -SignatureHtml "$(Get-Content -Path $file -ReadCount 0)"} What I want is for AutoIt to use Powershell.exe to run the script somehow. This way I would be able to use AutoIt variables and arrays together with the PS script.
       I guess the solution would be to run Powershell.exe with command line arguments?
      If that is not possible, maybe have the script stored as a "ps1" file, and interact with it that way? (not preferred)
    • tcox8
      By tcox8
      Hello,
      Currently I am running a script that calls a powershell script. To read the results of that I am reading StdOut. I am parsing things accordingly but unfortunately it doesn't parse correctly all the time and I end up missing parts of the string or other problems. My question then is, what is the best results for reading what is returned when running a powershell script or something similar?
    • jloyzaga
      By jloyzaga
      I can run this and notepad++ opens the file Run("C:\Program Files (x86)\Notepad++\notepad++.exe E:\icare\icare\NI-WI-NBSE-ERR.xml")
      But what I want is to run the .au3 or exe from command line and send it the file as parameter -am I being to simplistic in hoping its this easy?
      #include <FileConstants.au3>
      #include <MsgBoxConstants.au3>
      #include <Array.au3>
      #include <File.au3>
      ;E:\icare\icare\NI-WI-NBSE-ERR.xml
      ;Run("C:\Program Files (x86)\Notepad++\notepad++.exe E:\icare\icare\NI-WI-NBSE-ERR.xml")
      Run("C:\Program Files (x86)\Notepad++\notepad++.exe " & $CmdLine[0] )
      sleep (200)
      Send("{CTRLDOWN}a{CTRLUP}")
      sleep (500)
      Send("{CTRLDOWN}a{CTRLUP}{CTRLDOWN}s{CTRLUP}{CTRLDOWN}{SHIFTDOWN}b{CTRLUP}{SHIFTUP}")
      sleep (500)
      Send("{CTRLDOWN}{ALTDOWN}s{ALTUP}{CTRLUP}")
      sleep (500)
      Send("E:\icare\icare\NI_WI_MONTHLY_INSTALMENTS_c2base64-7.txt")
      sleep (500)
      Send("{ENTER}")