?
Solved

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

Posted on 2016-09-26
9
Medium Priority
?
102 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
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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 71

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

719 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