PowerShell verbose Start-Process command - powershell

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

Related

Powershell with -ArgumentList

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?

How to add variables in a call to Start-Process

I'm building a PowerShell script to run the following command on various servers:
arapx acc, Export ExportFile=\"C:\\Temp\\DEV_Refresh\\AccExport.txt\"
This code works:
Start-Process -FilePath '\\<fileserver>\Bin\arapx' -argumentList 'acc, Export ExportFile=\"C:\\Temp\\DEV_Refresh\\AccExport.txt\"'
Different servers have different paths for the output file so I tried to set a variable. But this fails:
$Dest_Folder="DEV_Refresh"
Start-Process -FilePath '\\<fileserver>\Bin\arapx' -argumentList 'acc, Export ExportFile=\"C:\\Temp\\${Dest_Folder}\\AccExport.txt\"'
This fails:
$Dest_Folder="DEV_Refresh"
Start-Process -FilePath '\\<fileserver>\Bin\arapx' -argumentList 'acc, Export ExportFile=\"C:\\Temp\\$(Dest_Folder)\\AccExport.txt\"'
And this fails:
$Dest_Folder="DEV_Refresh"
$argumentList = "'acc, Export ExportFile=\""C:\\Temp\\" + $Dest_Folder + "\\AccExport.txt\""'"
Start-Process -FilePath '\\<fileserver>\Bin\arapx' -argumentList $argumentList
Can anyone help me get the command to work with a varable?
The solution from zett42 works:
$Dest_Folder="DEV_Refresh"
Start-Process -FilePath '\\<fileserver>\Bin\arapx' -argumentList ('acc, Export ExportFile=\"C:\\Temp\\' + $Dest_Folder + '\\AccExport.txt\"')

How to passing external argument by call other powershell script?

I want to call another powershell script with external argument. I try this but return error. anyone can help please
$direct = "D:\Learn"
Start-Process powershell.exe -WindowStyle Minimized ".\Testing.exe" -Path $direct
Testing.exe
Param(
[parameter(mandatory=$true)][string]$Loc
)
Get-Content $Loc\API.txt
Pause
The Start-Process cmdlet has a -AgumentList parameter:
$direct = "D:\Learn"
Start-Process powershell.exe -WindowStyle Minimized ".\Testing.exe" -ArgumentList "-Path $direct"
If you want to just run a File with some arguments
$filepath = ".\Testing.exe"
$direct = "D:\Learn"
Start-Process -FilePath $filepath -ArgumentList $direct -Wait -NoNewWindow
I think they all were arguments of powershell.exe.
The whole argument can be wrapped in one double quote in argumentlist.
Start-Process "powershell.exe" -ArgumentList "-windowstyle minimized '.\testing.exe' -path $direct"
Or even it can be done without start-process:
& "powershell.exe -windowstyle minimized '.\testing.exe' -path $direct"

How to pass arguments to MSI file with PowerShell

I've read that you can pass arguments to a .msi file, but I have no idea how to do it correctly. I've tried the following, where $ArgumentList is an array.
$ArgumentList = #("/i .\NSClient v67.msi", "/norestart", "/quiet", "/l*v '$directory'", "token=$token", "host=$_host", "mode=$mode")
Start-Process "msiexec" -ArgumentList $ArgumentList -Wait -NoNewWindow
This is part of my script, where I'm trying to install NetSkope on my machine by executing a command.
In theory, the command should look like msiexec /i "NSClient v67.msi" token=loremipsum host=bryan.goskope.com mode=peruserconfig /norestart /quiet /l*v "C:\Temp\NetskopeInstallation.log.
#Find file path
$rawPath = Invoke-Expression -Command 'C:\Windows\System32\WHERE /r C:\Users\ /f NSClient*.msi'
#Extract the directory
$filePath = Invoke-Expression -Command "cmd.exe --% /c FOR /f ""tokens=1"" %A IN ($rawPath) DO (ECHO
'%~dpA')"
#Cast $filePath to work with string methods
$filePath = Out-String -InputObject $filePath
$filePath = $filePath.split("'")[1]
Invoke-Expression -Command "cmd.exe --% /c cd $filePath"
$ArgumentList = #("/i .\NSClient v67.msi", "/norestart", "/quiet", "/l*v '$directory'",
"token=$token", "host=$_host", "mode=$mode")
Start-Process "msiexec" -ArgumentList $ArgumentList -Wait -NoNewWindow
I would also recommend using the Powershell MSI Module
Concerning Start-Process:
-Argumentlist expects string as a type. I don't think you can just pass an array.
You also need to surround the parameters that require a space with escaped double quotes. The escape character is powershell is the grave-accent(`).
Another problem is that the variable $directory will never be expanded, because it is surrounded with single quotes. You need to remove those.
The following should work for your example, but I personally would just remove the space in the file name, since you don't need to do weird stuff with escaping.
Without escaping:
$ArgumentList = "/i .\NSClientv67.msi /norestart /quiet /l*v $directory token=$token host=$_host mode=$mode"
With escaping:
$ArgumentList = "/i `".\NSClient v67.msi`" /norestart /quiet /l*v $directory token=$token host=$_host mode=$mode"
Here's a slightly different syntax:
$MSIArguments = #(
"/x"
"`"C:\path with spaces\test.msi`""
"/qb"
"/norestart"
"/l*v"
"`"C:\path with spaces\test.log`""
)
Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow

How to run a program as another user and add arguments in powershell?

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