If I wanted to alter the behaviour of every cmdlet in a Powershell session to pass verbose flag I could achieve this in Powershell v3 with $PSDefaultParameterValues like so
$PSDefaultParameterValues['*:Verbose'] = $true
What would be a possible workaround for Powershell v2?
No, this is a 'new' feature in PowerShell 3.0 and up: about_Parameters_Default_Values
Related
I'm trying to find a cleaner way (NOT using a file or export-clixml) to pass powershell variable from one session to another or from one stage to another in terms of Jenkinsfile.
I'm trying to avoid using the following from link below:
How to pass powershell variable from one session to another or from one stage to another in terms of Jenkinsfile
In the context of PowerShell you can take a variable from one PSSession, and set it in another PSSession with:
Invoke-Command -Session $session -ArgumentList $variable {
Set-Variable VariableName $args[0]
}
But if the execution engine is NOT PowerShell (such as in this case, you are using the Jenkinsfile to run two instances of powershell.exe at different stages), you only have two options:
Set the output from a shell command to a variable in the Jenkinsfile, and reference it later when building your next PowerShell command
Serialize the PowerShell object to disk with Export-CliXml and read it in from a different PowerShell session using Import-CliXml, as outlined in the question you linked to
I am basically trying to customize my PowerShell a bit using a profile. I'm following some instructions from Internet, but the configuration works on the ISE but not on PowerShell itself. I'm using the profiles $PROFILE.AllUsersAllHosts.
Here are some sceenshots:
Works on ISE:
but not on PowerShell
This is in my ISE profile (I use my ISE as my master).
cp -Force "C:\Users\USERNAME\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1" "C:\Users\USERNAME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
Basically it writes whatever is in my ISE profile to my normal powershell profile everytime I start up ISE.
Depending on whether you are using ISE or plain Powershell $profile is a different value
Plain PowerShell
$profile = C:\Users\USERNAME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
ISE
$profile = C:\Users\USERNAME\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
You could theoretically change the value of $profile in one so that it uses the other :)
I have a Powershell Commandlet which prompts a user from a secure string based on a condition. Now I want to automate the testing of this commandlet for which I use a Powershell Remote Runspace to Invoke the commandlet. Currently it fails with this error.
Write-Host : A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.
How can I automate this?
It sounds like you are running powershell via c#. You can't prompt the user for input from the powershell script. You either need to pre-provide the necessary info in the script, or prompt for the info from your application and then pass the info to the powershell script.
As ojk mentioned the easiest way to accomplish this would probably be to use a powershell function then pass the necessary parameters to it via the code.
I can't post all of the script contenet, but the basic idea is that it downloads JSON and converts it to objects using the ConvertFrom-Json cmdlet. Some objects are filtered out, and the rest are written to an XML/XLS document (in the Excel 2003 format). This file is then attached to an email and sent to various people.
The problem I'm having is that it only works when run from the Powershell ISE. Once I try setting up a scheduled task, calling it from cmd, or even calling it from powershell, the attached file is completely empty. It is as if some functions do not run (the one that loops through and creates all rows).
I can continue to run from ISE for the time being, but the idea of this script is to send out an automatic email that will require no intervention. Any ideas as to what could be causing this?
You need to run the script "dot sourced"
which can be done like this
powershell.exe -noexit -file c:\test.ps1
or
pwershell.exe -noexit ". c:\test.ps1"
See this link under the -File section
http://technet.microsoft.com/en-us/library/hh847736.aspx
Based on the answer from the comments to the original post, it seems that the script was (unexpectedly?) working in the ISE because of the bug/quirk/feature that allows scripts run in the ISE to be aware of variables used in the console window and vice-versa.
This can often cause logic problems or unexpected results when a script runs with a variable already defined (and not initialized carefully in the script).
Ways to avoid this problem:
Try to test your code in as clean an environment as possible.
http://powershell.com/cs/blogs/tips/archive/2015/02/12/getting-a-clean-powershell-environment.aspx
To make sure a script runs in a completely clean test environment, you
could of course restart the PowerShell ISE. A more convenient way is
to open another PowerShell tab: in the PowerShell ISE, choose File/New
PowerShell Tab.
Use Set-StrictMode 2 in your script to catch undefined variables, etc.
https://technet.microsoft.com/en-us/library/hh849692.aspx
Set-StrictMode -Version 2.0
Prohibits references to uninitialized variables (including uninitialized variables in strings).
Prohibits references to non-existent properties of an object.
Prohibits function calls that use the syntax for calling methods.
Prohibits a variable without a name (${}).
I have had this problem be for and for me executing the scrip using single-threaded function from powershell worked.
You could also try some other options, go to this link to find more info.
Example
powershell.exe -noexit c:\test.ps1 -sta
Change the missing variable or function to global.
function global:mycall{}
Start your Script with:
[cmdletbinding()]
We use PowerShell for some of our automated build scripts. Unfortunately, by default, PowerShell continues after an error.
Ordinarily, I can change this behaviour by setting $ErrorActionPreference = Stop.
I can't see a corresponding command line switch for PowerShell.exe, and we (deliberately) run the commands with -noprofile, so I can't put it in there.
How do I do this for a build script?
Put it at the top of the script you're running?
$ErrorActionPreference = 'Stop'
Alternatively, you can also get similar control at the cmdlet level using the ErrorAction parameter.
There doesn't seem to be a way to set:
powershell -erroractionpreference stop ...
The following would work:
powershell -command { $ErrorActionPreference = "stop"; .\test.ps1 } -noprofile
There is of course nothing to stop the script (re)setting ErrorActionPreference.