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?
Related
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
The following script demonstrates an issue I'm experiencing after moving from PowerShell v4 to v5.1. I make extensive use of Start-Process redirecting StandardError to a file, if this file contains data after execution I can read the file to see what the issue was.
However, with v5.1 when the launched PowerShell process starts, it makes use of progress output which is on the error stream, so this is redirected to the error file. In previous versions of PowerShell this did not happen, if I use Command rather than EncodedCommand it works as expected.
I believe this is a bug but it would be useful if someone could confirm or suggest a workaround?
The output to the script below is as follows:
PowerShell v4
No Errors
hello world!
PowerShell v5.1
WARNING:
SourceId Record
1 parent = -1 id = 0 act = Preparing modules for first use. stat = cur = pct = -1 sec = -1 type = Completed
hello world!
hello world!
Demo Script
Note: this will create 2 temporary files, which are both deleted at the end.
$outfile = [System.IO.Path]::GetTempFileName()
$errorfile = [System.IO.Path]::GetTempFileName()
$command = "write-host 'hello world!'"
$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command))
$argumentList = #(
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-EncodedCommand",
$encodedCommand
)
Start-Process powershell `
-argumentlist $argumentList `
-PassThru `
-wait `
-RedirectStandardOut $outfile `
-NoNewWindow `
-RedirectStandardError $errorfile | Out-Null
if ((Get-Item $errorfile).length -gt 0)
{
Import-Clixml $errorfile | Out-String | Write-Warning
}
else
{
Write-Host "No Errors"
}
Get-Content $outfile
$outfile, $errorfile | Remove-item
We have a program that only updates when being run with the switch /t from an administrator account.
I came up with the CMD prompt version, but I'm new to powershell and having a hard time translating it to Powershell.
The CMD version is:
C:\Windows\System32\runas.exe /savecred /user:ourdomain\ouruseracct "C:\Program Files (x86)\ProjectMatrix\ProjectNotify\ProjectNotify.exe /t"
So far I got:
C:\Windows\System32\runas.exe /user:ourdomain\ouruseracct /savecred "powershell -c start-process -FilePath \"'C:\Program Files (x86)\ProjectMatrix\ProjectNotify\ProjectNotify.exe'\" -verb runAs"
Which runs powershell as admin and starts the program as admin but we need to pass the argument -t or /t to projectnotify.exe when running it.
I believe we need to make use of the -argumentlist but not sure how to word it.
I tried
$t = "-t"
Start-Process -FilePath "C:\Program Files (x86)\ProjectMatrix\ProjectNotify\projectnotify.exe" -ArgumentList $t -Verb runas
Which runs the program but not sure if that's how you pass the argument.
Extra work (troubleshooting):
$Cred = Get-Credential
$ProcInfo = New-Object -TypeName 'System.Diagnostics.ProcessStartInfo'
$ProcInfo.Domain = $Cred.GetNetworkCredential().Domain
$ProcInfo.UserName = $Cred.UserName
$ProcInfo.Password = $Cred.Password
$ProcInfo.FileName = "${Env:ProgramFiles(x86)}\ProjectMatrix\ProjectNotify\ProjectNotify.exe"
$ProcInfo.Arguments = '/t'
$ProcInfo.WorkingDirectory = "${Env:ProgramFiles(x86)}\ProjectMatrix\ProjectNotify"
$ProcInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Normal
$ProcInfo.Verb = 'RunAs'
$ProcInfo.UseShellExecute = $true
[System.Diagnostics.Process]::Start($ProcInfo)
After some more thought, here's a simpler way (in a single command even):
Start-Job -Credential (Get-Credential) -ScriptBlock {
$Dir = "${Env:ProgramFiles(x86)}\ProjectMatrix\ProjectNotify"
$StartArgs = #{
'FilePath' = "$Dir\ProjectNotify.exe"
'ArgumentList' = '/t'
'Verb' = 'RunAs'
'WindowStyle' = 'Normal'
'WorkingDirectory' = $Dir
'PassThru' = $true
}
Start-Process #StartArgs
} | Wait-Job | Receive-Job
My previous answer is at the bottom of this post now.
References:
about_Splatting
Get-Credential
Start-Process
Start-Job
Extra reading:
Import-CliXml
Export-CliXml
Assuming an on-demand script, you should create a pscredential object if you want to natively run this from powershell:
Launch.cmd
SET "PS=%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe"
SET "SCRIPT=%SYSTEMDRIVE%\Path\to\wrapper.ps1"
%PS% -NoProfile -NoLogo -ExecutionPolicy Bypass -File "%SCRIPT%"
wrapper.ps1
$Cred = Get-Credential
# To avoid prompting every time:
#
# if (-not (Test-Path -Path '.\mycred.xml')) {
# Get-Credential | Export-CliXml -Path '.\mycred.xml'
# }
# $Cred = Import-CliXml -Path '.\mycred.xml'
$StartArgs = #{
'FilePath' = "$PSHOME\powershell.exe"
'ArgumentList' = '-NoProfile', '-NoLogo', '-File', '.\runas.ps1'
'Credential' = $Cred
}
Start-Process #StartArgs
runas.ps1
$StartArgs = #{
'FilePath' = "${Env:ProgramFiles(x86)}\ProjectMatrix\ProjectNotify\ProjectNotify.exe"
'ArgumentList' = '/t'
'Verb' = 'RunAs'
}
Start-Process #StartArgs
I know the question asks for arguements, but if you don't them, this works:
Start cmd.exe -Verb RunAs
You can also run this using the 'Run' window or search box:
powershell -command start cmd.exe -verb runas
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
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