Solved

powershellscript,save output as .txt file in the same dir

Posted on 2016-09-26
9
90 Views
Last Modified: 2016-09-27
for example i have this script and i run it on Videos folder or..on the desktop,can anyone make this script to save output on Videos or desktop?

@echo off

REM ... some CMD commands here ...
echo Batch cmd 1
call :callPS PSScript1
REM ... some CMD commands here ...
echo Batch cmd 2
call :callPS PSScript2
REM ... some CMD commands here ...
pause
exit /b

:callPS label

powerShell.exe -ExecutionPolicy RemoteSigned -Command "$script = Get-Content '%~f0'; Invoke-Expression -Command ($script[(($script | select-string '::%1::').LineNumber)..(($script | select-string '::%1End::').LineNumber-2)] -join [environment]::NewLine)"


exit /b

----------------

::PSScript1::
write-host -foreground red Script1
Get-EventLog System | Where-Object {$_.EventID -eq "1074" -or $_.EventID -eq "6008" -or $_.EventID -eq "1076"} | ft Machinename, TimeWritten, UserName, EventID, Message -AutoSize -Wrap
::PSScript1End::

----------------

::PSScript2::
write-host -foreground green Script2
::PSScript2End::

Open in new window

0
Comment
Question by:john lambert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 4

Expert Comment

by:SEHC
ID: 41816726
1
 

Author Comment

by:john lambert
ID: 41816741
how to save on desktop or in the same folder? and no need to change all the time the path : c:/john/Desktop/script blah blah
0
 

Author Comment

by:john lambert
ID: 41816754
Ok i use this line ''Get-Process | Out-File c:\test.txt'' and is working but i prefer save on desktop or in the same folder without type the path c:/John/Desktop  and when i use the script on otehr PC i change again the path to c:/Michael.Desktop/Script blah blah.....i wanna make it work in all PC's desktop or same folder
0
Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41816772
My recommendation is to change the folder in the cmd batch file part, to the folder where the batch file is located:
@echo off
pushd %~dp0

REM ... some CMD commands here ...
echo Batch cmd 1
call :callPS PSScript1
REM ... some CMD commands here ...
echo Batch cmd 2
call :callPS PSScript2
REM ... some CMD commands here ...
pause
popd
exit /b

Open in new window

Then you can just use the file name, without path, in Out-File.
1
 

Author Closing Comment

by:john lambert
ID: 41816798
Qlemo no doubts ur AN EXPERT!!
0
 

Author Comment

by:john lambert
ID: 41816810
for this script dones't work:

@echo off
pushd %~dp0

REM ... some CMD commands here ...
echo Batch cmd 1
call :callPS PSScript1
REM ... some CMD commands here ...
echo Batch cmd 2
call :callPS PSScript2
REM ... some CMD commands here ...
pause
popd
exit /b

:callPS label

powerShell.exe -ExecutionPolicy RemoteSigned -Command "$script = Get-Content '%~f0'; Invoke-Expression -Command ($script[(($script | select-string '::%1::').LineNumber)..(($script | select-string '::%1End::').LineNumber-2)] -join [environment]::NewLine)"


exit /b

----------------

::PSScript1::
write-host -foreground red Script1
Get-WinEvent -FilterHashTable @{LogName="Microsoft-Windows-TerminalServices-RemoteConnectionManager/Operational";StartTime=(get-date).AddDays(-1);ID=1149} | %{					
	New-Object PSObject -Property @{
		MachineName = $_.MachineName
		TimeCreated = $_.TimeCreated
		User = $_.Properties[0].Value
		Domain = $_.Properties[1].Value
		SourceIP = $_.Properties[2].Value 
	}
}| Select MachineName,TimeCreated,User,Domain,SourceIP | Out-File last24.txt
::PSScript1End::

----------------

::PSScript2::
write-host -foreground green Script2
::PSScript2End::

Open in new window

0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41816822
I would have to check myself. Doesn't it save in the script's folder?
0
 

Author Comment

by:john lambert
ID: 41816850
ok maybe the script works fine..i made same changes but the RDP logon history is deleted
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41817586
You can use other event IDs for testing ;-).
0

Featured Post

What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question