Jenkins parametrized build - read file in powershell - powershell

I'm trying to create a Jenkins Parametrized build using the file parameter.
The user will select build with parameters, and will supply a text document with a list of items (one per line).
I would like to try and read this list in my powershell script to then go off and make some service calls (I've got that bit sussed).
I'm struggling with getting my powershell script to read the file that has been passed into Jenkins, here is what I have so far
$ids = Import-Csv input.txt
$array = #()
Write-Host $array.Length
foreach($id in $ids){
Write-Host $id.id
$array += $id
Write-Host "Array now has" $array.Length "items"
}
I know the script is running OK as I can run in Powershell ISE and pass in the absolute path of the csv/txt file.
For reference, the content of input.txt is like this:
id
2884430041011214,
9751297519392363,
lfsdkjgskdjflgsdjfg
Here are the two outputs I get for
Powershell ISE (as expected):
0
2884430041011214
Array now has 1 items
9751297519392363
Array now has 2 items
lfsdkjgskdjflgsdjfg
Array now has 3 items
Jenkins (not what I'm hoping for)
Started by user anonymous
Building in workspace C:\Users\jonec34\.jenkins\jobs\getParty\workspace
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\jonec34\AppData\Local\Temp\hudson4035619350462822370.ps1'"
Import-Csv : Could not find file 'C:\Users\jonec34\.jenkins\jobs\getParty\workspace\input.txt'.
At C:\Users\jonec34\AppData\Local\Temp\hudson4035619350462822370.ps1:1 char:8
+ $ids = Import-Csv input.txt
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Import-Csv], FileNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
0
Finished: SUCCESS
Can anyone tell me how I can do this please. I've also tried putting Param($file) at the start of the script, and using ${WORKSPACE} but either they're not working or I'm not understanding it properly. Any help greatly appreciated.
Thanks

Right, I've found where I was going wrong...
In my file parameter, I hadn't set a file location (EG I'd left it blank), but I entered in '\input.txt' and low and behold I get
Building in workspace C:\Users\jonec34\.jenkins\jobs\getParty\workspace
Copying file to \input.txt
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\jonec34\AppData\Local\Temp\hudson6216657038417634126.ps1'"
getting requests from file
0
2884430041011214
Array now has 1 items
9751297519392363
Array now has 2 items
lfsdkjgskdjflgsdjfg
Array now has 3 items
Finished: SUCCESS

Related

Newbie powershell argument issues

I made powershell script that [1] accepts 2 arguments (aka parameters), [2] changes a file's modified date & time, and [3] writes something to host. The following command line works just fine in the powershell console, but triggers an error message when I run the same command line in a Windows cmd prompt (DOS) Window:
E:\Apps\UtilitiesByMarc\Change_DateTime_for_test1.bat_and_Hello_world_with_2_named_args_aaa.ps1 -dateTimeVarArg "01/11/2005 06:01:36" -file_dateTimeMod_fullname "E:\Apps\Delete01\test1.bat"
The following is the coding for the powershell script to which I gave the long name, 'Change_DateTime_for_test1.bat_and_Hello_world_with_2_named_args_aaa.ps1':
param ( [string]$dateTimeVarArg, [string]$file_dateTimeMod_fullname)
Get-ChildItem $file_dateTimeMod_fullname | % {$_.LastWriteTime = $dateTimeVarArg}
#Get-ChildItem "E:\Apps\Delete01\test1.bat" | % {$_.LastWriteTime = $dateTimeVarArg}
$strString = "Hello World"
write-host $strString
function ftest{
$test = "Test"
write-host $test
}
ftest
When I run the command line shown above in a Windows DOS command prompt setting, I get the following error message:
Exception setting "LastWriteTime": "Cannot convert null to type "System.DateTime"."
At E:\Apps\UtilitiesByMarc\Change_DateTime_for_test1.bat_and_Hello_world_with_1_named_arg_aaa.ps1:6 char:50
+ ... "E:\Apps\Delete01\test1.bat" | % {$_.LastWriteTime = $dateTimeVarArg}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
I would like to know [1] how to alter the command line shown above (which works fine in the powershell console) so that it works in a Windows DOS command prompt setting, and [2] where I can learn more about why my command line triggers errors, and how to avoid them.
According to the output from the command "Get-Host | Select-Object Version", I am running version. 5.1.19041.1682.
Any tips would be much appreciated.
By default, you can not directly execute PowerShell scripts (.ps1 files) from cmd.exe, the Windows legacy shell, or from outside PowerShell altogether.
Attempting to do so opens the script file for editing instead, as does double-clicking .ps1 files from File Explorer / the desktop.
Executing .ps1 scripts from outside PowerShell itself requires use of the PowerShell CLI (powershell.exe for Windows PowerShell, pwsh for PowerShell (Core) 7+), which in your case translates to a call such as (additional CLI parameters may be called for):
powershell.exe -File E:\Apps\UtilitiesByMarc\Change_DateTime_for_test1.bat_and_Hello_world_with_2_named_args_aaa.ps1 -dateTimeVarArg "01/11/2005 06:01:36" -file_dateTimeMod_fullname "E:\Apps\Delete01\test1.bat"
As for what you tried:
The fact that you were able to execute your .ps1 from cmd.exe suggests that you changed the file-type definition for such files to execute via powershell.exe instead.
The fact that the arguments you tried to pass to the script were ignored - as implied by the error message you got - suggests that you used File Explorer's Open with shortcut-menu command to choose to open all .ps1 files with powershell.exe; said method does not support argument-passing.
There is a way to change the file-type definition to support argument-passing too, and it is detailed in this answer (section "Programmatic method").
Generally, however, I suggest not applying this customization, especially in batch files that must also be run by other users / on other machines, which cannot be expected to have the same customization in place.

Running my powershell script errors, while entering line by line into terminal works

i am attempting to create a power shell script that will help with automation (collecting data off a website).
I have several lines of commands saved in a script (.ps1), in windows powershell ISE (i have trimmed a small section below)
$ieObject = New-Object -ComObject 'InternetExplorer.Application'
$ieObject.Visible = $true
$ieObject.Navigate('---')
$currentDocument = $ieObject.Document
$currentDocument.IHTMLDocument3_getElementById("serialno")
My issue, is that when i copy paste each line by line from above into the terminal, it returns the result i want.
However when i run the actual script using the ise, i get various errors such as
You cannot call a method on a null-valued expression.
At C:\Users\--\--\script.ps1:13 char:47
+ $currentDocument.IHTMLDocument3_getElementById <<<< ("serialno")
+ CategoryInfo : InvalidOperation: (IHTMLDocument3_getElementById:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I feel that the powershell script is executing differently to how I need it to, any help would be great. thanks
The instance has not received all data yet. Add
do { Start-Sleep -Seconds 1} until ($ieObject.ReadyState -eq 4)
after instantiating $ieObject, that seems to do the trick.
I recommend against automating Internet Explorer, that's comprehensively last century technology. If you need to parse HTML document from a PowerShell script, using built-in Invoke-WebRequest is the easiest way.

Set window title when running "start powershell" in powershell not working?

The following command works in CMD (How to start powershell with a window title?).
start powershell -NoExit -command "$Host.UI.RawUI.WindowTitle = 'bits'"
But it doesn't work in Powershell.
PS C:\> start powershell -noexit -command "$Host.UI.RawUI.WindowTitle = 'test'; read-host"
Start-Process : A parameter cannot be found that matches parameter name 'noexit'.
At line:1 char:18
+ start powershell -noexit -command "$Host.UI.RawUI.WindowTitle = 'test ...
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
The following command can open a new powershell window.
start powershell "$Host.UI.RawUI.WindowTitle = 'test'; read-host"
However, the new window shows the following error message and the title is not set.
System.Management.Automation.Internal.Host.InternalHost.UI.RawUI.WindowTitle : The term
'System.Management.Automation.Internal.Host.InternalHost.UI.RawUI.WindowTitle' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
At line:1 char:1
+ System.Management.Automation.Internal.Host.InternalHost.UI.RawUI.Wind ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (System.Manageme...wUI.WindowTitle:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Bacon Bits' helpful answer explains that start in cmd.exe means something different than in PowerShell.
Use Start-Process as follows to get the desired result; note that powershell implicitly binds to parameter -FilePath, whereas the ,-separated arguments starting with -NoExit bind implicitly to the -ArgumentList (-Args) parameter, which accepts an array of strings:
# In PowerShell, `start` is an alias for `Start-Process`
start powershell '-NoExit', '-command', "`$Host.UI.RawUI.WindowTitle = 'bits'"
In paticular, --prefixed pass-through arguments must be quoted so that they're not mistaken for Start-Process's own parameters.
Also note the ` preceding the $ in $Host, which prevents up-front interpolation of $Host by the calling PowerShell instance.
You could also use '$Host.UI.RawUI.WindowTitle = ''bits''', a single-quoted literal string with embedded single quotes escaped as ''.
Important:
While passing arguments as an array to -ArgumentList is conceptually the best approach, it is unfortunately ill-advised due to a long-standing bug in Start-Process, still present as of this writing (v7.1) - see GitHub issue #5576.
For now, using a single string comprising all arguments, enclosed in embedded "..." quoting as necessary, is the only generally robust approach. As discussed in the linked GitHub issue, an -ArgumentArray parameter that supports robust array-based argument passing may be introduced in the future.
In the case at hand this means the following, as suggested by PetSerAl in a comment on the question:
Start-Process powershell '-NoExit -command "$Host.UI.RawUI.WindowTitle = ''bits''"'
Note the single-quoting_ ('...') of the overall argument-list string, which then necessitates escaping the embedded single quotes - those that PowerShell should see as part of the command - as ''.
In Command Prompt, start is the start internal command. In Windows Powershell, start is an alias for Start-Process, which does something similar but isn't identical.
Try running this:
powershell -NoExit -command "`$Host.UI.RawUI.WindowTitle = 'bits'"
Here's another way, while avoiding the dollar sign. The double quotes have to be on the outside, so that bits stays quoted.
start powershell "-noexit (get-variable host).value.ui.rawui.windowtitle = 'bits'"
You can always avoid these quoting issues putting the command in a file.
start powershell '-noexit .\window.ps1'
start powershell with the command option for setting the window title did not work for me. Maybe because I want to open another powershell with a ps1 file. so in the other ps1 file I added the first line as below,
(Get-Host).ui.RawUI.WindowTitle='TEST TEST'
and it worked like a charm....
If you add this to your powershell profile.ps1 you can get the window title to show the current running script and if you are just opening a window with no script then 'pwsh' will be displayed.
Will be systematic with no need to add a line on top of each script. The other answers
combined with $MyInvocation.MyCommand seem to give the name of the profile.ps1 instead when running a script from the context menu.
This can also be tweaked to change the result.
[console]::title = Split-Path -Leaf ([Environment]::GetCommandLineArgs()[-1]).Replace('pwsh.dll','pwsh')
Works on both PS 5 and 7 . For ver. 5 replace pwsh.dll by powershell.exe

Script runs in ISE but not in Powershell

I have a powershell script. This script runs perfectly fine if I open it in Powershell ISE, however, if I right click on the file and click 'run with powershell' the script throws an error.
Furthermore, I read in previous threads that the following execution pattern solved the issue for some people:
powershell -STA -File script.ps1
In this case this didn't solve the issue, however it did allow me to read the error:
At C:\Users\sancarn\AppData\Local\Temp\script.ps1:20 char:20
+ $parent = [System.Windows.Forms.TreeNode]$global:database.Ite ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.TreeNode].
At C:\Users\sancarn\AppData\Local\Temp\script.ps1:27 char:36
+ ... [void]$node.nodes.add([System.Windows.Forms.TreeNode]::new(" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.TreeNode].
At C:\Users\sancarn\AppData\Local\Temp\script.ps1:33 char:45
+ ... PSCustomObject]IWDBGetChildren([System.Windows.Forms.TreeNode]$node) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.TreeNode].
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound
This having been said, I'm not sure I can really do anything about this error specifically... I already load System.Windows.Forms and System.Drawing... Does anyone have any idea how to execute this file properly?
Edit
Other attempts at trying to fix the issue:
powershell -NoExit -STA -File script.ps1
powershell -NoExit -STA -File script.ps1 -Scope Global
Edit 2
I have also tried adding:
Add-Type -AssemblyName System.Windows.Forms
To the top of the powershell script. The issue remains unresolved however.
Edit:
Not sure why this is being flagged as a duplicate after
this answer already has a recommended answer and
the recommended answer demonstrates why this is different.
...
As #PetSerAl said in his comment, the answer is in https://stackoverflow.com/a/34637458
Every PowerShell script is completely parsed before the first statement in the script is executed. An unresolvable type name token inside a class definition is considered a parse error. To solve your problem, you have to load your types before the class definition is parsed, so the class definition has to be in a separate file.
Ultimately to resolve the issue I need to either load my class definitions as a separate file, or store my declarations in a header file, which calls the class definitions (and the rest of the script).
I am amazed this was even an issue but it works now, so that's good...
Edit
In my case it is a bit different to the post linked by the solution author. I'm actually building a ruby library for executing Powershell on windows machines. Currently I don't write data to a file and I can't guarantee that a here-string/invoke-expression will work.
Invoke-Expression #"
$anotherString=#"
hello world!
"#
"# <--- Powershell will throw an error here!
Instead I've decided to encode the body if a header is present, and then decode and execute at runtime
require 'base64'
if header!=""
encoded_body = Base64.strict_encode64(body.encode("utf-16le"))
body = <<-END_TRANSFORMATION
#{header}
$encoded_body = "#{encoded_body}"
Invoke-Expression $([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($encoded_body)))
END_TRANSFORMATION
end
So far this has appeared to work in all cases, doesn't need external files, and will work even if users use here-doc strings in their Powershell scripts!

Start a folder copy and continue script using PowerShell

I am several layers deep into a problem with a PowerShell Script that we use to manage user accounts. When users move between locations, we move their ADUser to a new OU, and move their data between servers.
While researching how to copy their home folder, it was determined that there is no easy way to copy files using PowerShell and exclude certain directories. As a result we are calling robocopy.exe and using /XD. This also lets us start the copy, but have the script continue on to the next user.
We are attempting to enhance our script by moving or deleting the older folder depending on the user type. I created a PS1 file that I would like to call so that the script can continue to the next user immediately and not wait for the copy to finish. The PS1 file looks like this:
Param(
[string]$source,
[string]$destination,
[string]$oldStaff
)
robocopy $source $destination
move $source $oldStaff
When I attempt to call this from our script in the following way:
start powershell.exe .\Move-WCFolders.ps1 $source $destination $oldStaff
I get the following error message:
Start-Process : A positional parameter cannot be found that accepts
argument
'<%Removed server name before posting to Stack Overflow%>'. At
line:1 char:1
+ start powershell.exe .\Move-WCFolders.ps1 $source $Description
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
I'm sure that there is likely a far better way to achieve what I am trying to achieve. It doesn't feel 'clean' to need to start robocopy, or to have to call things in an external script. The end goal is to copy folders while excluding directories, move the folder when the copy is done, but not have to wait for the copy to complete to continue the script.
Try using Start-Job. It's easier than you think, and you'll be able to capture the results for later:
$job = Start-Job -FilePath '.\Move-WCFolder.ps1' -ArgumentList $source, $destination, $oldStaff
# do more processing here
# . . .
# wait for the job to finish and output the results
$job | Receive-Job
start is an alias for Start-Process
The error is because the arguments at the end.
AFAIK, you will most likely want to turn them into a single string to pass to powershell.
You may want to try something like this:
start powershell.exe -File .\Move-WCFolders.ps1 -ArgumentList "$source $destination $oldStaff"
You will want to make sure you have the escaping right, so it may end up looking like this:
start powershell.exe -File .\Move-WCFolders.ps1 -ArgumentList "`"$source`" `"$destination`" `"$oldStaff`""
You could also investigate using Start-Job.