We have an internal process set up in Powershell which runs an exe file internal.exe which creates a lot log files in an absolute path "C:\This\is\absolute" which contains all the log files of the past 30 days. "C:\This\is\absolute" contains also log files from other applications and while internal.exe runs and creates log files in "C:\This\is\absolute" another application might create a log file there as well.
Now we need to send the log files created by internal.exe and for this they have to be moved to another folder "C:\Move\here" after having been created.
The process is currently simply set up as
Start-Process -FilePath "internal.exe"
I was looking for something like
Get-Outputfiles (Start-Process -FilePath "internal.exe") | foreach {Move-Item -Path $_ -Destination "C:\Move\here"}
but I found only ways to write output to files, e.g., via Out-File. Is there a way to get something like the Get-Outputfiles which lists the paths of output files from a process?
Related
I have a Script that reads a log file and creates a text file with some output taken from the log file.
The script works and it takes the right log file as long as it is on my C:\ drive.
The Original File is located on the network drive called s:\ but if I want to take this log by entering the whole path where the file lives I get the error that >> The Drive wasnt found and that a Drive called S does not exist.
How can I connect to a network drive?
$inVar = Select-String -path C:\Dev\Script\Ldorado.log -pattern "IN:" -WORKS
$inVar = Select-String -path S:\Lic_Debug\Ldorado.log -pattern "IN:" - Does not work!
Thanks for all the Answers - I actually managed it by changing the Path name in \fs01\ because that represents the S:\ and it works. Thanks
Try prepending the path with "FileSystem::" i.e. -path FileSystem::s:\Lic_Debug\Ldorado.log
This ensures your script uses the FileSystem provider which should correctly recognise the drive.
I have one folder in which there is a txt file that generates every 30 minutes and overwrites the previous version of it. I wish to create a backup folder where a copy of the txt file will be held, for that to happen I want a cmd file that basically copies the file and adds the timestamp at the end of the new file (I will even settle for a serial number at the end of the file.) This cmd file I will run using task scheduler of windows.
I tried using the robocopy command but it doesn't seem to create new files but only copy once and keep overwriting over it.
So basically: a command that will copy File.txt -> File_timestamp.txt every 15 minutes without overwriting anything.
Here is a command you can use to make a copy of the file with a timestamp in another directory. When you are confident that the file will be copied correctly, remove the -WhatIf from the Copy-Item cmdlet.
powershell -NoProfile -Command "Move-Item -Path .\yyy.txt -Destination C:\temp\yyy_$(Get-Date -UFormat '%Y-%m-%dT%H-%M') -WhatIf"
Developing our own application for our company only, we have developed script used for installation from shared drive. Except the installation itself, the script should also create/update values in the registry of particular user (HKEY_CURRENT_USER).
These values are separated for:
Directories (HKCU:\Software\Classes\Directory)
All File Extensions (HKCU:\Software\Classes\*)
For the directory folder the update is immediate, where for the extensions it seems to take quite some time depending on machine hardware (from 40 sec to 2 minutes).
Now there is a trouble to create "entry" in the registry for the folder named * only. I've got a question for this to resolve (PowerShell: How do I create selector on file/folder, whose name is '*' (asterisk/star)?).
Ignoring the issue above, we have found some solution how the string path works, however I'm not sure what is happening behind the code and do not understand why it takes so long time.
# Directory
New-Item -Path "HKCU:\Software\classes\Directory" -Name "shell" | Out-Null
# All Files Extension
New-Item -Path "HKCU:\Software\classes\[*]" -Name "shell" | Out-Null
One idea is that the [*] solution actually goes through all the file extensions, but the registry itself is showing this NewItem under * folder and not shown under particular extensions:
Another idea about this, is when we have a registry file (*.reg), by running the file the registry entry is added immediately and resolve the case.
Questions:
What is actually happening when we are running the query to add entry under [*] selector?
How can be this process optimized to lower the time for creating new folder in registry for all files' extension?
I suspect what's happening is that the -Path in your New-Item call is recursive because of the wildcard. Hence the delay.
Here's a workaround to the issue:
Set-Location -LiteralPath "HKCU:\Software\classes\*"
New-Item -Name "shell"
New-Item uses the current location as the -Path if not explicitly passed to the function.
Is there a bat command which checks if a specific directory (not recursive in subdiretories) has a file with a specific size (like 109485 bytes)?
If I have to scan the entire directory only to check this, is it possible to only scan the 5 most recent files (last changed) to check if their size matches?
I cant scan an entire directory cause this code will execute every 3 second so I really need something really efficient.
If you're willing to involve PowerShell in the process, you could put something like this in a batch file.
PowerShell -Command "ls C:\Windows\System32\ | Where-Object {$_.length -eq 30720}"
This command lists all the files in system32 and then filters the list by files that are 30720 bytes. Where-Object also has a bunch of other useful properties and switches that you can filter by.
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ee177028(v=technet.10)
We have .txt files sent by users which are encrypted. We decrypt them and send it to a 3rd party system downstream as input. It had been working well but users started to send files are .TXT instead of .txt. It doesn't make any difference during decryption but it is affecting the downstream system. We are supposed to change the .TXT to .txt
I tried changing it this way
Copy-Item -Path $myOfile –Destination ([io.path]::ChangeExtension($myOfile, '.txt')) -Verbose
Here $myOfile is my file name and it named something like this
20160506_205400_Sender_header.TXT.GPG which we decrypt and it changes to 20160506_205400_Sender_header.TXT
I used the above command to change it to 20160506_205400_Sender_header.txt and it throws the below error
Copy-Item : Cannot overwrite the item C:\Sender\Submit\20160506_205400_Sender_header.TXT with itself.
It appears as if there is no distinction between .TXT and .txt. Is there a way to do it or a workaround?
Windows is not case-sensitive when it comes filepaths, so a copy-operation with the same destination and source will fail because you're reading the file your trying to replace.
Use Rename-Item to rename files. Ex:
Rename-Item -Path $myOfile -NewName ([io.path]::ChangeExtension($myOfile, '.txt')) -Verbose