Check if file exists from URL powershell - powershell

I have the following line of code to download a list of files from a text file. It is part of a larger program. Currently, the PS script will grab the files that were downloaded. But if a file does not exist, I would like to pause the PS script so the user can adjust the URL to point a good file, then continue. Is this possible? If not maybe go through the list and output which paths are bad?
Get-Content file.txt | ForEach-Object {Invoke-WebRequest $_ -OutFile C:\test\download\$(Split-Path $_ -Leaf)}
Edit
I will test out the -method HEAD but for now, I found the test-path cmdlet. Still working out the kinks to not display the whole path but this seems to work for me. As it prints out what is found and missing. I will have to work on adding a pause and re run in my program.
Get-Content c:\file.txt | `Select-Object #{Name='FileName';Expression=$_}},#{Name='FolderExist';Expression={ Test-Path $_}}

Related

Enter individual folder(s) and execute PowerShell command

I have many folders with even more subfolders, and as posted in my first question
How to create a powershell script / or windows .bat file for ffmpeg
I want to encode all video files in the folders.
The Script I got from mklement0 works fine but lazy as I am, I was wondering if there was a way to tell the PowerShell to enter folder 1, go to subfolder_1, and execute the ps1 script (would be perfect if it executed in a new powershell instance), wait a certain time and go into subfolder_2
Repeat until no more subfolders available.
Is this possible?
Edit:
The Script I got:
Get-ChildItem *.mkv | where BaseName -notlike '*`[encoded]' | foreach {
ffmpeg -i $_ -c:v libx265 -c:a copy -x265-params crf=25 "$($_.BaseName)[encoded].mkv"
pause
}
What is the reason for the desire to process each subfolder in a separate instance of powershell.exe? by Mathias R. Jessen
Because I want to encode multiple folders at once to save some time.
If there is a way to execute the script in the same PowerShell (as far as my understanding goes, I can only encode one folder at one time if I use the same PowerShell instance)
You could wrap the whole thing in another Get-ChildItem to find all the subdirectories in your main folder, then set-location to that path and run in each of those:
$directories = get-childitem -path "C:\Path\To\Videos\Folder" -Recurse -Directory
Foreach ($dir in $directories) {
Set-Location $dir.fullname
*Run your code here*
}
This would be better than trying to do them all in parallel with different PowerShell instances. Correct me if I'm wrong, but video encoding uses a lot of your computer's resources, so trying to run them in parallel would actually be more of a hindrance.

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

How can I execute a command to each file in a directory?

I want to view what's inside each pak of an Unreal Engine 4 game. In the game files there are like 700+ paks, and execute the command to view the content manually it's just a pain.
I'm using Unreal Engine's UnrealPak.exe to view the contents of a pak file.
I use the next command to view (and log) the contents of a pak-
E:\UE_4.10\Engine\Binaries\Win64\UnrealPak.exe (directory of the game)\Content\Paks\(pak file name).pak -test > C:\Users\(user)\Desktop\Logs\log.txt
This will put all the output of that command in the log.txt textfile.
What I want to do is a loop that will execute that command to each file, and then I could do two things, have one log.txt and then put all the outputs of all the paks in there, or have log_(pakname).txt
I could just go manually, but it's going to take a lot of time to execute the same command 700+ times. If someone can help me, I'll be very grateful.
Get-ChildItem -Path. -Recurse -Include "*.pak" | Foreach-Object { invoke-expression "E:\UE_4.10\Engine\Binaries\Win64\UnrealPak.exe $($_.Fullname) -test " | Out-file "log.txt" -Append}
Let's add unrealpak to the path:
$env:path += E:\UE_4.10\Engine\Binaries\Win64
Then
cd somedir\Content\Paks
dir *.pak | foreach { unrealpak $_ -test >> ~\desktop\log.txt }

Using power shell to retrieve contents in a txt file

I am trying to write a powershell script which will return the contents inside a txt file. However, I do not want to specify the drive of the txt file as the txt file will be placed in the same folder as the powershell script.
I am using this line of code:
get-content .\document.txt | select -first 1 -skip 1
but it doesnt work.
Inside document.txt:
This is the first line
This is the second line
What script do I write to retrieve the second line "This is the second line" without having to put the full path like "C:\Data\Scripts\Document.txt"? I have searched online solution but many solutions required me having to put its destination path.
get-content $PSScriptRoot\document.txt | select -first 1 -skip 1
Note that $PSScriptRoot will only have a value when the script is executing, but it will represent the path of where the script resides.
I may not understand question correctly but you can display the content of the text file by
cat <yourfile.name>
if you want to get the part of the text file you can do
cat <yourfile.name> | Select-String '<text you want to get>'
you can refer to this site on how to manipulate the output https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/select-string
and if ever you want to run the script in Bash you need different script if ever
If you are looking for the second line then use the below:
By default, each line will be an index of an array. So the second line will be having the index as 1
(get-content .\document.txt)[1]
TO find the text file have a look at this link
PowerShell: current directory relative to executing script
After you get that ,We can move to second line of the file easily,
Solution here:-to print the required line
The code may convert itself to be something like this
#include this is in your script,this part of the code will get the absolute path of the script from whereever its running
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$path = Join-Path (Get-ScriptDirectory) 'Your Script Name'
$path.ToString()
$path =$path.Substring(0,$path.IndexOf('Your Script Name'))
$path
#now $path has the directory in which you have your files,append it with your text file name
(Get-content $path+'textfilename.txt')[1]
#and get the second line of the content
Credits to SomeShinyobjects
This can be simply written as
$path=$PSScriptRoot+'\textfilename.txt'
(Get-content $path)[1]

PowerShell for-each cleanup

I know PowerShell is up to v5, but as I am new to PowerShell, I've been looking through Stack Overflow to generate the script I have. I've found that I need a generic non-version specific way of accomplishing this process...
Here is the issue - Step 1 - I'm pulling application installation locations information from the registry and am using a temporary file to house the results.
dir "HKLM:\SOFTWARE\Wow6432Node\companyname" | Get-ItemProperty | Select installdir | Out-File "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-Installs.txt"
This provides me a list of installation directories for the company's software that is installed on a particular machine. I then want to take these results, append *.config to each line, as well as taking these results and appending *.xml to each line, and output to a new text file.
The input for the process would be the contents of the initial results file, and the output file should have each line listed in the first results, added to the final results file, once appended with *.xml, and once appended with *.config.
The net effect I am looking for is the creation of a #file for a 7z command. I am attempting this by using the following -
(Get-Content "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-Installs.txt") -replace '\S+$','$&*.config' | Out-File "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-config.txt" -Encoding utf8
(Get-Content "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-Installs.txt") -replace '\S+$','$&*.xml' | Out-File "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-config.txt" -Append -Encoding utf8
However, I'm only getting one line that has *.xml and one line that has *.config appended -
After getting this far, I'm thinking that some for-each loop is needed, but I'm not getting anywhere with what I have tried adapting from here. I'm looking now for some way to combine the three lines into one function, if that is possible, and eliminate the temporary file step in the first command, by reading and outputting in the same step. This would also need to remove the "installdir" and "----------" lines from the output. Anyone have some ideas and maybe examples?
Taken your above command dir "HKLM:\SOFTWARE\Wow6432Node\companyname" | Get-ItemProperty | Select installdir | Out-File "$env:USERPROFILE\Desktop\KDI-Admin\Export\$env:COMPUTERNAME-SC-Installs.txt" you could put the result of your query into a variable $result:
$result = dir "HKLM:\SOFTWARE\Wow6432Node\microsoft" | Get-ItemProperty | Select installdir;
From there you can easily loop through the array, skipping empty ones and process the rest of it:
foreach($path in $result.installdir)
{
# skip empty paths
if([string]::IsNullOrWhiteSpace($path)) { continue; }
# now do your processing ...
$path;
}
Is this what you were asking for?