Page 1 of 1

Multiple PowerShell commands from BAT file.

Posted: 16 Dec 2020 04:50
by karanik
Hello,

I want help to understand some things and to resolve some issues.
I am trying to run multiple PowerShell commands from a bat file. I have problem on output or with execution of some commands.
For Example

Question1
I have test.bat

Code: Select all

@echo off
echo. > .\user-info.txt 
whoami >> .\user-info.txt  
echo. >> .\user-info.txt  
wmic /APPEND: ".\user-info.txt " USERACCOUNT get Caption,Name,PasswordRequired,Status
net user >> .\user-info.txt  
echo. >> .\user-info.txt  
::
echo === DISK INFORMATION === >> .\user-info.txt  
PowerShell.exe -nologo -noprofile -ExecutionPolicy Bypass -Command "& {get-psdrive -psprovider filesystem  }" >> .\user-info.txt
wmic /APPEND: ".\user-info.txt " diskdrive get Name,InterfaceType,MediaType,Model,Size,Status
echo. >> .\user-info.txt  
::
echo === Top 10 CPU Processes === >> .\user-info.txt  
echo ----------------------------------- >> .\user-info.txt  
PowerShell.exe -nologo -noprofile -ExecutionPolicy Bypass -Command "& {Get-Process | Sort-Object -Property CPU | Select-Object -Last 10 }" >> .\user-info.txt
echo. >> .\user-info.txt 
::
echo === Chkdsk Logs === >> .\user-info.txt  
echo ----------------------------------- >> .\user-info.txt  
PowerShell.exe -nologo -noprofile -ExecutionPolicy Bypass -Command "& {get-winevent -FilterHashTable @{logname="Application"; id="1001"}| ?{$_.providername -match "wininit"} | fl timecreated, message }" >> .\user-info.txt
echo. >> .\user-info.txt  
The output give that file and i have one error on last PowerShell command >> PowerShell.exe -nologo -noprofile -ExecutionPolicy Bypass -Command "& {get-winevent -FilterHashTable @{logname="Application"; id="1001"}| ?{$_.providername -match "wininit"} | fl timecreated, message }" >> .\user-info.txt
I don't know why. Probably from character { and } but i don't know alternative method to bypass this.
image1 -https://imgur.com/f3s9GlP
Image


Question2
If i change the output method from this

Code: Select all

>> .\user-info.txt
to this

Code: Select all

Out-File '.\user-info.txt' -Append

then the results is like this on Notepad++ [image2] , and this if because has many spaces charecters [image3].
image2 - https://imgur.com/heKUCDH
Image

image3 - https://imgur.com/ebWxlFD
Image

Re: Multiple PowerShell commands from BAT file.

Posted: 16 Dec 2020 10:25
by aGerman
Things that you could try:
- The entire command is enclosed into quotes. So it's likely that you must escape quotes inside of the powershell command. Try \" or `". Alternatively use single quotes ' instead of double quotes " for the strings.
- Obviously the Out-File cmdlet appends UTF-16-encoded text. Changing the encoding may help

Code: Select all

Out-File -Encoding ([Text.Encoding]::GetEncoding(1252)) -Append '.\user-info.txt'
Steffen