I have some powershell who has the following:
Param([Parameter(Mandatory = $true)] $TargetPaths=#())
I bundle this .ps1 into a .intunewin with content manager and upload to intune.
I now wanted to call my powershell with an install command similar to powershell -executionpolicy bypass -command .\powershell.ps1 -TargetPaths #('1','2','3'...)
This works fine on my local machine... but does not pass through the intune installer, my log files are never created and after careful examination it is clear the script is never run.
Can anyone advise me on moving forward to get this to run? If I execute the script locally with the same parameters it works fine. I even re-jiggered it to work through a cmd execution instead of powershell (including the use of """ for a single double quote) and couldn't get this working.
Unfortunately, I cannot provide you with the final solution.
But you may want to try the -file parameter of powershell.exe. It partially works for us. This means that the script is executed correctly, but Intune always shows a wrong installation status in the first moment. So we have not yet solved this case conclusively either.
powershell -executionpolicy bypass -file ".\install.ps1" -i -name "myApp" -variant "2022"
While not really an answer to the question, I was able to work around the problem of passing an array by instead flattening the entire thing into a string on parameter pass and later breaking it into an array inside my code. In order to do this I removed all internal quotation marks and then assumed their location later. Not really the most elegant but the script now executes.
Related
I'm in Windows 10 & I'm trying to open an app using start command which goes like:
start "C:\Program Files\MyElectronApp\MyElectronApp.exe" which opens the app as expected.
I also want to pass some arguments after the above-mentioned command such as:
start "C:\Program Files\MyElectronApp\MyElectronApp.exe" --UUID=762835745634 --org_token=r8347t89457
When I write this command, I get UUID as /prefetch:1 despite passing any thing, I tried adding double quotes "", but same issue. However, I manage to get org_token as expected. as shown in the screenshot.
What could be causing this issue?
What I tried to address the issue?
I tried to add double quotes for the value of UUID, then I tried to use powershell and git bash for the same thinking it could solve the issue. Then, I checked the prefetch folder only to find it's empty. Then, I changed the order of args, but nothing happened.
Here's how I solved it:
The args should NOT be in UPPERCASE, in this case it's UUID, I changed it to small case uuid. For windows uppercase args are reserved or fixed for some DOS-level stuff.
Just change:
start "C:\Program Files\MyElectronApp\MyElectronApp.exe" --UUID=762835745634 --org_token=r8347t89457
to
start "C:\Program Files\MyElectronApp\MyElectronApp.exe" --uuid=762835745634 --org_token=r8347t89457
The exact reason for this is not clear as I couldn't find one. Will certainly update this once I get to know the same.
I am attempting to run a .exe file for an app which is installed but requires to be activated. This would be a fairly easy process but the directory with .exe changes name slightly with each install. For example a number is added to each folder after the install (Different devices have different numbers) e.g. test1 and then test2. How could I use a wild card to target the folder as it changes?
Example code:
Start-Process -FilePath "\C:\ProgramData\app*/test.exe"
Please note: the app is not real, this is just for display purposes.
Your path seems to be incorrect. Wildcards should work in PowerShell. For example, the below wildcard works for me in PowerShell 5.0:
Try running Start-Process -FilePath "C:\ProgramData\app*\test.exe"
I'm building an installer using advanced installer and have run into a problem trying to add dates into the log file. I tried a command using cmd which worked, however when I added it to the MSI commandline all the date values came out as blank. Below is the parameters I pass for the MSI
/L*V "C:\Log_%date:~4,2%.%date:~7,2%.%date:~10,4%-%time:~0,2%.%time:~3,2%.%time:~6,2%.log"
We are trying to make the log be Log_04.05.2019-15.03.45.log instead of Log.log since the logs get overwritten when uninstall happens or on a retry of an installation..
Advanced Installer: Sorry, I see that I must have misunderstood. You are trying to set the log file name from within Advanced
Installer. Will have a quick look. Where do you specify this command line in the tool? Please note that setting the logging policy for "Global Logging" will ensure unique log file names and that every MSI operation is logged in TMP.
Clarification: So it looks like you don't want to write to the log, but to control the file name of the log file itself?
PowerShell: I find batch files clunky with regards to stuff like this. Can you invoke the installation via Powershell? I don't really use PowerShell, but seeing as it can use .NET, maybe a simple conversion of this C# call would do the trick?
You want something like: "Log_04.05.2019-15.03.45.log", so you could perhaps try this in C#:
Console.WriteLine("Log_" + DateTime.Now.ToString("dd.MM.yyyy-HH.mm.ss") + ".log");
Here is a blog on using PowerShell with Windows Installer, see towards the bottom for this PowerShell snippet (again, I do not use PowerShell for this purpose):
$DataStamp = get-date -Format yyyyMMddTHHmmss
$logFile = '{0}-{1}.log' -f $file.fullname,$DataStamp
$MSIArguments = #(
"/i"
('"{0}"' -f $file.fullname)
"/qn"
"/norestart"
"/L*v"
$logFile
)
Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow
Maybe also have a read about the Windows Installer PowerShell
Module (Heath Stewart) as linked to in this general purpose
answer:
How can I use powershell to run through an installer?.
Special-purpose PowerShell Module making Windows Installer operations
less clunky.
Some Links:
Various MSI logging methods: Enable installation logs for MSI installer without any command line arguments
Windows Installer Logging
I'm having some trouble with a .bat->.ps1 call in my build process after moving from Win7 to Win10 (1607 LTSB) - the call to PowerShell, that took less than 100msec on Win7 now takes 10sec+ on Win10 on certain hosts :-(
The call looks like this: powershell -ExecutionPolicy Bypass -NonInteractive %scriptfile%
Startup-performance of ISE and PowerShell.exe from start-menu is good.
I wasn't able to dig up anything helpful so far...
PoSh 5.1 on both, Win7 and Win10, PSProfile is clean apart from a couple of aliases
$env:PATH is "clean" apart from a couple of folders that have been added on purpose - removing those didn't improve the situation.
already did "ngen update" - problem still occurs.
I've create a little helper script that measures startup-performance of PowerShell from different starting points: https://gist.github.com/mwallner/d3c86794bb74680b0c0cf4e9a9758ab4 no luck with this either, on Win7 and most of my Win10 machines startup-time is way below 1sec.
anybody here who has already solved this riddle?
Solved! - it's been UAC all along :-(
Solution: disable UAC for the user that's running the scriots or make sure the first script that calls the others is being run as administrator / elevated.
Not sure why UAC is causing these delays, and I'm certain there are cases where this is not an option - but for me this is solved by ensuring the first script is elevated + disabling UAC for dev boxes.
I had a similar problem - scripts taking multiple seconds to start.
The problem turned out to be a combination of two factors:
I had ExecutionPolicy force-set to RemoteSigned via GPO (run Get-ExecutionPolicy -List and check the value for MachinePolicy)
I had quite a few processes running, so that enumerating them was taking a long time
...so it was fixed by a restart. (I think overriding the GPO-set value would also work, but I didn't try it.)
What didn't work:
Disabling the loading of my profile (powershell -NoProfile)
Running ngen to generate native images for an assembly and its dependencies and install them in the Native Images Cache
Disable module autoloading (introduced in v3) with $PSModuleAutoloadingPreference = 'none', i.e. powershell.exe -NoProfile -ExecutionPolicy Bypass -command "$PSModuleAutoloadingPreference = 'none'; & path\to\script.ps1"
"Use legacy console" in the shortcut properties
Disabling UAC
I noticed that simply running PowerShell was relatively fast, only script execution (via command-line parameters or the & / Call operator) was slow. For instance I could run the script by piping it to powershell's stdin, which didn't incur the slowdown:
type "script.ps1" | powershell.exe -noprofile -nologo -executionpolicy Bypass -file -
At this point I tried troubleshooting with ProcMon, which didn't show any long calls.
Next I tried opening Process Explorer to check the stack of the powershell.exe process while it was loading the script. The top of the stack was:
ntdll.dll!RtlGetNativeSystemInformation+0x14
KERNEL32.DLL!lstrcmpA+0x12d
KERNEL32.DLL!CreateToolhelp32Snapshot+0x108
[Native Frame: IL Method without Metadata]
[Managed to Unmanaged Transition]
System.Management.Automation.dll!System.Management.Automation.PsUtils.GetParentProcess+0x73
System.Management.Automation.dll!System.Management.Automation.Internal.SecuritySupport.GetExecutionPolicy+0x138
which led me to issue #2578 that explained the behavior I was seeing.
I have created a powershell script which will transfer(using winscp.dll) the files from Jenkins windows server to Linux server. In Jenkins batch command, I have executed that powershell script and it works fine.
But when i tried the same in Jenkins pipeline job, it calls the powershell script and comes to the next step. Its not waiting for powershell script response.
bat 'powershell.exe -ExecutionPolicy Bypass "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1 $env:EndMarket $env:Environment"'
I have tried with another powershell script which will connect to Linux server and execute some commands. It works fine in pipeline job
Kindly guide me to fix this issue.
Interesting. Your problem doesn't seem to be in your script because you already explained that it works in a batch job.
I don't know how your pipeline is written, but I would suggest taking a look to Stage, Lock and Milestone which is probably what you need.
The stage step is a primary building block in Pipeline, dividing the
steps of a Pipeline into explicit units and helping to visualize the
progress using the "Stage View" plugin
I guess you could add a stage block like this one in your pipeline:
stage("Previous Step") {
// Some previous step
}
stage("Wait for Script Execution") {
// Call your script
bat 'powershell.exe -ExecutionPolicy Bypass "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1 $env:EndMarket $env:Environment"'
}
stage("Next Step") {
// Script already finished its execution
}
But without your pipeline info is just a guessing. Also to improve your script compatibility avoiding "bat and ExcutionPolicy" and using the PowerShell plugin, with that plugin you could simplify your code like this:
powershell -File your_script.ps1
EDIT: I forgot to mention that you can try a different alternative to powershell and the winscp lib using "scp" direct compatibility between Windows and Linux, I'm talking about Cygwin.
With Cygwin installed (with scp) you can use scp as it was a Linux box and a bash script instead powershell:
D:/cygwin-64/bin/run.exe /usr/bin/bash -lic \"/home/user/file.sh\"
In this case I'm running a script silently through Cygwin within a Jenkins Project with a "Run Windows Batch" option. In the script you can add shell commands and the scp instructions you want.
It may seems a little bit more complex but adds more flexibility to perform Windows - Linux tasks.
I've detailed more examples in my blog, you may find it useful.
As avvi mentioned you should check if that variables are being loaded.
You are not calling the script correctly. You are passing in $Env in the powershell invocation which is a powershell variable.
You are in batch mode so should be passing in %.
bat 'powershell.exe -ExecutionPolicy Bypass "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1" "%EndMarket%" "%Environment%"'