Solved

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

Posted on 2016-09-26
9
51 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
 
LVL 68

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 68

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 68

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Synchronize a new Active Directory domain with an existing Office 365 tenant
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now