Example of the code:
$logfile = "log.txt"
$filename = "backup.rar"
Start-Transcript -Path $logfile -Append -Force
"Start..."
Start-Process -FilePath "C:\Program Files\WinRAR\Rar.exe" -ArgumentList ("a " + $filename + " #backup.lst") -NoNewWindow -Wait
"Done"
Stop-Transcript
Output in the console:
...
Start...
Error: Do not find backup.lst
Done
...
But in the log file:
...
Start...
Done
...
Where output Rar.exe?
PS: Sorry for my bad English.
This is a known issue with Start-Transcript - it doesn't capture output from exes.
My first thought is that the Start-Transaction is not capturing errors (obviously ;) )
You may want to look at using the start-process arguments of -RedirectStandardOutput and -RedirectStandardError
Related
I want to run following command in Powershell: AFImport.exe
[https://docs.osisoft.com/bundle/pi-server/page/afimport-utility.html][1]
Syntax Example #1
AFImport "\\AFServer\database" /File:"C:\Filename.xml" /P
Now I made following code:
Set-Location -Path "$Env:pihome64\AF"
$xml = "C:\Temp\test.xml"
$AF = "\\AFtest\AF-test"
$log = "C:\Temp\log.txt"
Start-Process AFImport.exe -ArgumentList '$AF','File:$xml','/P' -Wait -RedirectStandardOutput $log
When executting the script the CMD window an runs AFImport.exe but nothing is shown and closes after a few seconds.
What syntac error do I have?
I have a PowerShell script that starts a process:
$pythodDir, $argsOriginalList = $args
$pythonExePath = Join-Path $pythodDir "python.exe"
$argsList = #("--no-render") + $argsOriginalList
Start-Process -FilePath $pythonExePath -ArgumentList $argsList
I wish to see the full command line of the Start-Process for example:
C:\python\python.exe --no-render --arg1 value
I didn't find a flag to render the full command that Start-Process creates. Is there any way to verbose it?
You can get the command line by grabbing two properties from the ProcessInfo of the process you started:
# Capture the process object using -PassThru
$p = Start-Process -FilePath $pythonExePath -ArgumentList $argsList -PassThru
# Output the command line
($p.StartInfo.FileName,$p.StartInfo.Arguments) -join ' '
For example:
$p = Start-Process -FilePath 'Notepad.exe' -ArgumentList 'c:\folder\file.txt' -PassThru
($p.StartInfo.FileName,$p.StartInfo.Arguments) -join ' '
C:\WINDOWS\system32\notepad.exe C:\folder\file.txt
i need to call the cdb.exe as a Process to check to kill the process after a few seconds.
Some Dumps cannot be analyzed so i have to do an other call.
Here you can see my code. But it doesn't work. The cdb.exe is not started correctly and i am not getting the output file.
Do you have some advises for me?
The call "before" implementing the process part starts the cdb.exe
$maximumRuntimeSeconds = 3
$path = "C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\cdb.exe"
$process = Start-Process -FilePath $path "-z $unzippedFile.FullName, -c `".symfix;.reload;!analyze -v; q`""
try {
$process | Wait-Process -Timeout $maximumRuntimeSeconds -ErrorAction Stop > $outputFile
Write-Warning -Message 'Process successfully completed within timeout.'
}
catch {
Write-Warning -Message 'Process exceeded timeout, will be killed now.'
$process | Stop-Process -Force
}
# call before implementing Process
& "C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\cdb.exe" -z $unzippedFile.FullName -c ".symfix;.reload;!analyze -v; q" > $outputFile
-Passthru was needed to make Wait-Process work.
I think you also need to look at how the double quoted string is expanding. I think $UnzippedFIle.Fullname might be adding a literal ".FullName" at the end of the actual fullname of the zip file. I don't have your environment, but the rudementary tests I've done show that. Try packing it in a sub-expression like:
"-z $($unzippedFile.FullName), -c `".symfix;.reload;!analyze -v; q`""
Let me know how that goes. Thanks.
C:\>dir /b ok.txt
File Not Found
C:\>type dodump.ps1
$path = "C:\Program Files\Windows Kits\10\Debuggers\x86\cdb.exe"
$process = Start-Process -PassThru -FilePath $path -ArgumentList "-z `"C:\calc.DMP`"" ,
"-c `".symfix;.reload;!analyze -v;q`"" -RedirectStandardOutput c:\\ok.txt
try {
$process | Wait-Process -Timeout 100 -ErrorAction Stop
Write-Host "Process finished within timeout"
}catch {
$process | Stop-Process
Write-Host "process killed"
}
Get-Content C:\ok.txt |Measure-Object -Line
C:\>powershell -f dodump.ps1
Process finished within timeout
Lines Words Characters Property
139
I'm having a heck of a time figuring out why this simple command is not working.
I'm attempting to take screenshots of a list of domains using PowerShell and Firefox per [this article][1].
Currently I have the following code, but it does not produce screenshots and I'm unsure what is wrong code wise. Any assistance and/or a point in the correct direction is greatly appreciated.
$screenshotdir = "$PSScriptRoot\FF_Screenshots"
If(!(Test-Path -Path $screenshotdir)) {New-Item -Path $PSScriptRoot -Name "FF_Screenshots" -ItemType Directory}
function getFireFoxScreenShot() {
$importedCSV = Import-Csv .\Domains.csv
foreach ($url in $importedCSV) {
$domainName = $url.Name #example google.com
$domain = $url.Domain #example google (no tld)
if (-not ([string]::IsNullOrEmpty($domainName))){
Echo "Getting Screen Shot for: $domainName"
Start-Process -FilePath "C:\Program Files\Mozilla Firefox\firefox.exe " -ArgumentList " --screenshot $screenshotdir\$domain.png ", "$domainName" -Wait
}
}
}
getFireFoxScreenShot
[1]: https://www.bleepingcomputer.com/news/software/chrome-and-firefox-can-take-screenshots-of-sites-from-the-command-line/
Make sure to specify protocol (https:// or http://) as it is in the article you linked to:
# Tested with Developer Edition of Firefox
$domain = "example"
$domainName = example.com"
$screenshotdir = "C:\SO\56572800"
# This works
Start-Process -FilePath "C:\Program Files\Firefox Developer Edition\firefox.exe" -ArgumentList "--screenshot $screenshotdir\$domain-with-https.png", "https://$domainName" -Wait
# But doesn't work
Start-Process -FilePath "C:\Program Files\Firefox Developer Edition\firefox.exe " -ArgumentList " --screenshot $screenshotdir\$domain-no-https.png ", "$domainName" -Wait
From what I checked, if you don't specify https:// prefix (or http:// if applicable), it'll hang for a long time so you might have an impression that it's working.
As #lloyd mentioned in comments, you have to make sure that value of $screenshotdir is properly assigned and available to the function.
Also, it's a good practice to trim leading/trailing spaces from your command, even though in your example it still works with the spaces. I mean these ones:
HERE | HERE | HERE |
Start-Process -FilePath "C:\Program Files\Mozilla Firefox\firefox.exe " -ArgumentList " --screenshot $screenshotdir\$domain.png ", "$domainName" -Wait
I have been trying to re-write the following PowerShell code as I need it to wait until completion before carrying on so assumed Start-Process with -Wait would be sufficient however I can't seem to get it to actually run...
Original code which works, but won't wait until it's finished before carrying on with the script.
function ZipAndDeleteFile([string] $file, [string] $saveLocation)
{
$command = [string]::Format("`"{0}`" a -ep -df `"$saveLocation`" `"$file`"", $winrarPath);
iex "& $command";
}
My attempt at re-writing which isn't running as expected, does nothing so far...
function ZipAndDeleteFile([string] $file, [string] $saveLocation)
{
Start-Process -FilePath $winrarPath -ArgumentList "a -ep -df $saveLocation $file" -Wait
}
Fixed with the following... knew it was something silly.
Start-Process -FilePath $winrarPath -ArgumentList "a -ep -df `"$saveLocation`" `"$file`"" -Wait