Solved

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

Posted on 2016-09-26
9
71 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This article will help you understand what HashTables are and how to use them in PowerShell.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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