Rundeck Job fails cause verbose output - powershell

I´m currently running in the issue that my Rundeck Job fails cause it write a verbose message. Is there any possibility to change this behavior from rundeck?
My Job is a basic power shell command:
Get-ChildItem "\\server\folder" -Recurse -File | Where CreationTime -lt (Get-Date).AddDays(-365) | Remove-Item -Force -Verbose
Without the -verbose I don´t see which file was deleted, but I´d like to see this.
Someone any idea?

You can attach to your step a quiet output filter, that filter need a regex to catch the lines that you don't need, you can use this tool to test your regex definitions.

Related

How to use Powershell Invoke-Item with a gci search?

I'm trying to build a Powershell script that uses the Invoke-Item cmdlet once I've found the files using the following script:
gci -Recurse -File *.xls* C:\folder_example\'  |  sort LastWriteTime | Select -last 2 | 
I'm not sure how best to implement the Invoke-Item cmdlet from here?
In its simplest usage, Invoke-Item (aliased as ii) just needs a path given to it either through pipeline or through a parameter.
In this case, it could be as simple as adding | Invoke-Item to the end of your command:
gci -Recurse -File *.xls* C:\folder_example\ | sort LastWriteTime | select -Last 2 | Invoke-Item
One thing I will mention, is that I have found that certain programs cannot handle a bunch of files thrown at them in quick succession like this script will do.
I usually run into this when trying to open a bunch of files in Notepad++.
I'm going to simplify the command a bit just to keep it short...but usually one of these two changes will fix my problem:
Get-ChildItem -Filter *.xls* | ForEach-Object { Invoke-Item $_.FullName }
Placing the call into a ForEach-Object script block will sometimes fix this. I'm not 100% why this fixes it, I have some theories but that's all they are, so I will keep them out of this answer. But it always fixes the issue for me when using Invoke-Item on Notepad++ files.
Some programs, like Excel, have a start up splash screen and some lag before they eventually open. So you could always throw a sleep in there just to help slow things down a bit in case you are still having trouble:
Get-ChildItem -Filter *.xls* | ForEach-Object { Start-Sleep 0.1; Invoke-Item $_.FullName }
That will add a small 100ms pause just to give whatever application you are running to have a little time to process.

Powershell: copy file without locking

I created simple nagios plugin check_log.ps1 to check log file on windows machine. It works in way that make copy content of log and in next time look for specified string in difference between copy of log and original log file.
The problem is that sometimes in random moments check_log.ps1 locks log file so it cause stop of the application which create log file.
Generally plugin use original log file in two places
# compare content of $Logfile and $Oldlog, save diff to $tempdiff
Compare-Object -ReferenceObject (Get-Content -Path $Logfile) -DifferenceObject (Get-Content -Path $Oldlog) | Select-Object -Property InputObject > $tempdiff
# override file $Oldlog using conetent of $Logfile
Copy-Item $Logfile $Oldlog
I make test. In one PS session I run while($true) { [string]"test" >> C:\test\test.log }, in second session I run plugin C:\test\check_log.ps1 C:\test\test.log C:\test\Old_log.log test
I'm not fully sure if my test is correct but I think that Copy-Item command cause problem. When I comment this line in script I don't see any errors in terminals. I tested some custom functions to copy file which I found in internet but I din't find solution for my problem.
Do you have an idea how to make it work fully?
if you think the copy-item is locking the file, try reading the content and then saving it to another location. Something like this:
Get-Content $Logfile | Set-Content $Oldlog

Is there a correct form of using find?

I am trying to delete everything except init.py, using this command in powershell(windows 10 and python django version- 2.2.7):
find . -path "/blog/migrations/.py" -not -name "init.py" -delete
Tree of this project(attaching image)
**If i run the command i am getting this error:
FIND: Parameter format not correct
What to do? Thank you in advance. I am new to python django.**
The following script will delete all files that aren't named "init.py". Make sure to run this at the root of the folder you would like to delete items from.
Get-Childitem -File -Recurse | Where-Object{$_.Name -ne "init.py"} | Remove-Item -WhatIf
To test, leave the -WhatIf switch. If it displays the files you would like to remove, run again without the -WhatIf switch.

Powershell close multiple windows / end processes based on name

I have a pretty neat mess of batch/python scripts that install a program called MATRIS, followed by about 15 exe updates.
Around 11 of these updates open a window telling me the that the update was successful.
Now it would be really fun to run a batch or powershell script which closes all of these windows for me.
The last thing I tried was Get-Process | Where-Object {$_.Path -like "MatrisInstaller.APCIPLUS"} | Stop-Process -WhatIf
I wasn't sure if it was the name as read in task manager, or like the title of the window, but I tried both.
Please note that a couple of these are (32 bit) - I'm not sure if that would impact the script.
I was able to run tasklist followed by kill {PID} but PIDs change: I'm not sure how to script it.
Please reply if you need any clarification, I've historically been poor at wording my questions.
In your example, Path is pointing to the executable file on disk, so while possible to use (if it is consistent), it won't match the name you find in the processes tab of Task Manager. Typically, people will use the name as shown on the Details tab of Task manager. For example, with Outlook on my system, these three possibilities are:
Path: C:\Program Files\Microsoft Office\Office16\OUTLOOK.EXE
Processes tab: Microsoft Outlook
Details tab: outlook.exe
So, you need a command like this:
Get-Process | Where Name -eq 'Outlook' | Stop-Process
or, better:
Get-Process -Name 'Outlook' | Stop-Process
Note that PowerShell expects you to remove the '.exe' you see in Task manager.
EDIT: Additional technique
If you know the names of the processes, then you can simplify your script by doing something like this:
$processList = "Process1","Process2","Process3" # Add all process names to the list
$processList |
ForEach-Object {
Get-Process -Name $_ | Stop-Process
}
You were almost there, just need to change "Path" to "ProcessName" like so:
Get-Process | Where-Object {$_.ProcessName -like "MatrisInstaller.APCIPLUS"} | Stop-Process -WhatIf

Powershell script required

this is the scenario.
p1
|_f1
|_f2
p2
|_f1
|_f2
Can anyone please help me with a powershell script that copies the files shown above from the TFS to a temporary folder ??? where f1,f2,and so on are the subfolders..
I have no experience with either, but in the interest of a least pointing you in the right direction, check out this site. http://coolthingoftheday.blogspot.com/2009/03/pstfs-powershell-and-tfs-better-than.html
There are a couple of commands that will give you at least part of what you want. You will still need to do some digging to figure out the time stamp stuff.
You may want to check the answer to my question on a very similar scenario.
You will find answer to
give me all files in this folder (or subfolder)
as well as
that where modified after x/y/zzzz
but I'm still not sure about the
dump those files to folder other than they would normally go to
update
Incorporating your approach
Get-TfsItemProperty $/MyFirstTFSProj -r -server xyzc011b |
Where {$_.CheckinDate -gt (Get-Date).AddDays(-30)} |
Copy-Item -Destination C:\SomeDir -Whatif
you normally can omit the Copy-Item -Path param because it will be provided by the pipeline.
I don't have a TFS at to test with Get-TfsItemProperty but you could try
Get-TfsItemProperty $/MyFirstTFSProj -r -server xyzc011b |
Where {$_.CheckinDate -gt (Get-Date).AddDays(-30)} |
Get-Member
do find out about where this $null value is coming from.
I assume you did already see this post. To maintain the folder structure on the destination you need to include the -Force switch on the Copy-Item to create missing target folders:
Get-TfsItemProperty $/MyFirstTFSProj -r -server xyzc011b |
Where {$_.CheckinDate -gt (Get-Date).AddDays(-30)} |
Copy-Item -Destination C:\SomeDir -Force -Whatif
I'm still not sure if you need to retrieve/export the files prior to copy them - you should check on the second answer from Richard Berg in the post mentiond above.