powershell running as windows service giving incomplete output - windows

I want to run a powershell script as windows service. The script is pretty huge and takes a while to run.
I am redirecting the output of this script as text using start-stop transcript in powershell script. When the script is ran through windows service output is not complete. Its giving me half the output.
I have created service using :
"C:\path\to\instsrv.exe" myservice "C:\path\to\srvany.exe" then giving
C:\path\to\powershell.exe -noprofile -File "C:\path\to\myscript.ps1" in Application in registry.
While running script through powershell console, transcript redirects full output to a file but not through service.
Any help will be much appreciated.

Related

Powershell script scheduled using Task scheduler doesn't run...script runs fine

I am using windows 8.
I have the following script file (that spits our all services to a CSV file). When I run this as follows from powershell editor, it works fine.
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe C:\Powershell\SchedulingaScript.ps1
When I schedule this script from windows task scheduler it won't kick off.
I have "Unrestricted" execution policy set on that session. Following is the code in the script file and 2 images are the setting on the task scheduler.
#Trying out scheduling powershell scripts
Get-Service | Export-CSV "C:\ExportingServicestoaCSVFile.CSV"
You have to use the -file Parameter. Simply enter powershell -? and you will see all possible options to launch powershell.
Hope this helps -tom
In the Edit Action dialog Program box simply put PowerShell. In Arguments put -file "C:\sciptlocation\scriptname.ps1"

How to debug hidden powershell scripts?

I'm writing a script that normally will get called by another application (VMware vCenter Server). From that application I trigger a batch file (redirect.bat) and pass a variable which is the powershell script name (TestMe.ps1).
The script is placed on a Windows Server and when I go into the command prompt of the Windows Server and call the redirect script, I see that my PowerShell script runs as expected. However when I trigger it from the app the Powershell script is not run or doesn't produce output. I have confirmation that the redirect.bat is run, because the redirect.bat writes a line in a log file.
The vCenter Server app is running under Local System account. Could it be a permissions error? Is LocalSystem allowed to run Powershell scripts?
I now have no clue if the Powershell script even starts, because it (of course) is not visible in my console when running. The batch file always returns errorlevel = 0.
Any tips on how to insert debugging info in the script that should always give output? Tips on how to troubleshoot this?
redirect.bat:
set POWERSHELL=C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -nologo -noprofile -noninteractive
SET ERRORLEVEL =
echo %1 > G:\DataStoreAlarms\Log\Redirect-batch.txt
start %POWERSHELL% -command "&"%1""
echo Error level: %ERRORLEVEL% >> G:\DataStoreAlarms\Log\Redirect-batch.txt
I call redirect.bat from the command line and from the app like this:
redirect.bat G:\DataStoreAlarms\Scripts\TestGabrie.ps1
TestGabrie.ps1:
$String = "This is a test"
$String | Out-File -FilePath "G:\DataStoreAlarms\Log\Powershell.txt" -Append
Regards
Gabrie
Problem seemed to be the START command:
start %POWERSHELL% -command "&"%1""
After changing it to this, it worked:
%POWERSHELL% -command "&"%1""
Thanks for all your help.

Powershell Tracing and Control-M

I seem to have run into a problem/bug when trying to capture tracing output when running a Powershell script from Control-M.
The output file shows the headers and footers of the start-trace and stop-trace commands, but it does not show anything else I try to capture. Specifically, if my script issues a write-host command somewhere, then that information is not captured in the output (trace) file.
Here is a super simple script that illustraes my problem:
start-transcript -path "C:\Powershell\transcript.log"
write-host "test message"
#do stuff...
stop-transcript
Here is an example of my current output when running the script through Control-M:
**********************
Windows PowerShell Transcript Start
Start time: 20140212002005
Username : mydomain\SYSTEM
Machine : myserver (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
**********************
Windows PowerShell Transcript End
End time: 20140212002008
**********************
Note that my test message does not show up! This only happens when I run the script via Control-M. When I run my script manually, my "test message" does show up in the transcript output.
My first suspicion was file permissions, but those look good to me. The Control-M agent uses system level access, so it should have all the permissions it needs anyway. If it were a file permission issue, I don't believe i would even get the header/footer messages.
I'm on PS v2.0. My server is running 2008r2.
Any thoughts appreciated...
Write-Host writes to to the console window, which is not what's being "watched" by Control-M. Try Write-Output instead. Write-Host is usually not what you want for producing output.
See http://windowsitpro.com/blog/what-do-not-do-powershell-part-1 and http://powershell.com/cs/blogs/donjones/archive/2012/04/06/2012-scripting-games-commentary-stop-using-write-host.aspx

Task Scheduler Action parameter should generate log, but doesn't

As the title suggests, I have an added parameter in my Task Scheduler Actions that logs stdout and stderr to a log.txt file. The logging works when the action is run through the command prompt, but not when the action is run by the actual Task Scheduler (at its specified time). Task scheduler reports the action runs successfully, but I can't be sure it does because there's no logging:)
Command looks like this
powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
I'm unfortunately not a native Windows user, so any help would be appreciated. I'm running Windows Server 2008 R2 Enterprise.
Thanks!
Cmd.exe handles command redirection. You have to run it under cmd.exe. Powershell probably also can do redirection but in your script (.NET can).
A black window just means a console program is running. Only if cmd is running does cmd features become available. By starting cmd or by putting it in a batch you canget redirection from cmd.
cmd /c powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
See for Help
cmd /?
Place the command you listed in a batch file and then schedule the batch file.
If you are doing so and it fails, then try it with your account credentials as authentication in task scheduler, to see if it is a permissions issue.
#echo off
powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1

Running a windows batch command using chef blocks the chef provision

I'm using chef for windows and need to run a batch file that starts up the selenium-server java service (java -jar seleniumserver.jar) as a daemon. When I try to use a windows_batch resource, it causes chef to hang during it's provisioning.
The problem is that the selenium server stays running in whatever command line you start it in, but chef won't continue provisioning the machine until the command is finished. The thing is, the command never finishes, it's not supposed to.
Here's what I've tried so far:
Executing java -jar seleniumserver.jar & (with the windows_batch resource)
Using a template to create a batch file for the command, then using windows_batch to execute file.bat
Using windows_batch to execute the batchfile with an & (file.bat &)
I'm open to any ideas, even if it's kind of hacky. Thanks in advance!
If I understand the question correctly, you can start a separate process so the main batch file ends. For example:
start java -jar seleniumserver.jar
You can control several execution parameters through the different start options.
Ending command lines with & does not do the same as *nix.
async process on windows that doesn't block chef run
batch "run" do
code 'powershell -c "start-process notepad.exe"'
end
The following is an example of using start-process to start a command with arguments.
Try running the command by itself in the command prompt to familiarize yourself with its output.
net statistics server
Now run it using powershell and start-process in the command prompt and verify the correct output shows in c:\output.txt. Please pay careful attention to the use of single or double quotes. My experience says start-process won't work with double quotes for some reason.
powershell -c start-process net -ArgumentList 'statistics workstation' -RedirectStandardOutput c:\output.txt
Now put the following in a Chef execute resource and run it. Again, be aware of the single quotes, double quotes and escaped single quotes.
execute "run" do
command 'powershell -c "start-process net -ArgumentList \'statistics workstation\' -RedirectStandardOutput c:\chef-output.txt"'
end
You should find the correct output in c:\chef-output.txt.

Resources