Solved

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

Posted on 2016-09-26
9
77 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
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 69

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 69

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 69

Expert Comment

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

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article will help you understand what HashTables are and how to use them in PowerShell.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

761 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