Search a file in 4 different path using powershell - search

I need to search in 4 different path:
C:\Program Files\test1
C:\Program Files\test3
C:\Program Files (x86)\test6
D:\
I am using the following shell command:
Get-ChildItem -Path C:\ -Filter file.txt -Recurse | fl directory > C:\filereport.txt
Can you please help me to use a similar command that would search all the above path and also not cut the path?
In fact, when I extract the file some path are cut. I would need the length to be longer as after 107 characters it is not showed in 1 line
Thanks,
Graig

Try:
$p = "C:\Program Files\test1", "C:\Program Files\test3", "C:\Program Files (x86)\test6", "D:\"
Get-ChildItem -Path $p -Filter file.txt -Recurse | select directory | ac C:\filereport.txt

Related

Powershell script to batch replace links in Excel workbooks

I am for quite a while, in my free time, tackling a script that can batch replace external link addresses in multiple excel files within script folder. I have learned, that you can't change external links via usual powershell to excel interaction, as these values are forced to read-only. However, there is a clever way to bypass that by converting the Excel file to a .zip archive and read/change the files inside and then rename them back to excel format.
Through learning and digging around the web, i have compiled this script function that should create a backup, rename to archive and then replace desired text within, renaming the file backwards afterwards.
'''
function Update-ExcelLinks($xlsxFile, $oldText, $newText) {
# Build BAK file name
$bakFile = $xlsxFile -ireplace [regex]::Escape(".xlsb"), ".bak"
# Build ZIP file name
$zipFile = $xlsxFile -ireplace [regex]::Escape(".xlsb"), ".zip"
# Create temporary folder
$parent = [System.IO.Path]::GetTempPath();
[string] $guid = [System.Guid]::NewGuid();
$tempFolder = Join-Path $parent $guid;
New-Item -ItemType Directory -Path $tempFolder;
# Uncomment the next line to create backup before processing XLSX file
# Copy-Item $xlsxFile $bakFile
# Rename file to ZIP
Rename-Item -Path $xlsxFile -NewName $zipFile
# Not using Expand-Archive because it changes the ZIP format
C:\7z\7za.exe x "$zipFile" -o"$tempFolder"
# Replace old text with new text
$fileNames = Get-ChildItem -Path $tempFolder -Recurse -Include *.xml,*.bin.rels
foreach ($file in $fileNames)
{
(Get-Content -ErrorAction SilentlyContinue $file.PSPath) |
Foreach-Object { $_ -replace $oldText, $newText } |
Set-Content $file.PSPath
}
# Changing working folder because 7Zip option -w doesn't work
Set-Location -Path $tempFolder
# Not using Compress-Archive because it changes the ZIP format
C:\7z\7za.exe u -r "$zipFile" *.*
# Rename file back to XLSB
Rename-Item -Path $zipFile -NewName $xlsxFile
}
'''
The problem is that it successfully interacts with the desired file and renames it, but refuses to interact with external links information within the archive at "'Excel File.zip'\xl\externalLinks_rels" directory. The link information I am trying to replace is to change the "/wk28/example_file_wk28.xlsb" with "/wk29/example_file_wk29.xlsb" by changing the wk28 string to wk29 for each external link and so on. Does anybody have experience in this field? As I am only starting my scripting adventure and can't quite diagnose the problem within this script.

GREP equivalent for Windows OS through Python script

Please, can someone help me with an alternative for Windows to the following command:
Linux Version:
find Win32_EXE -type f -name '*.json' -exec grep --files-with-match -i 'dbtool' '{}' ;
Windows version:
FIND /i "dbtool" \Win32_EXE*.json
Problem: The windows syntax lacks an option letting me specify that I want a result returned on the first match.
In python I would use a recursive glob:
from glob import iglob
def search(file):
with open(file) as fo:
for line in fo:
if "dbtool" in line.lower():
return True
return False
for file in iglob("Win32_EXE/**/*.json", recursive=True):
if search(file):
print(file)
but I think in general I wouldn't use Python - other solutions are probably faster/better.
e.g. Powershell
get-childitem -recurse *.json | where-object { select-string -quiet -pattern "dbtool" -path $_ }
or, if you can install ripgrep, that works well.
rg -l -g "*.json" -i dbtool Win32_EXE
This will search for *.json files and search them for the string dbtool. Change the PUSHD directory to your search directory.
PUSHD "C:\src\t"
FOR /F "delims=" %%A IN ('DIR /S /B /A:-D "*.json"') DO (
FIND /I "dbtool" "%%~A"
)
POPD

Translating linux command script to powershell command script

i am attempting to translate this snippet:
"find &filsti./ -name '*.sas7bdat' -type f -printf '%u;%p;%a\n'"
into powershell, so that i can scan for files the same way in windows as i do in linux. Can anyone assist me? Appreciate any help i can get.
No idea what that last part is, but try this:
Get-ChildItem -Path "C:\path" -Include *.sas7bdat -Recurse

Pipe files to uglifyjs - from Powershell

I would like to pipe my .js files to uglifyjs using windows powershell.
This will not work:
dir .\build -filter *.js | uglifyjs > bundle.js
From the uglifyjs2 docs i can see that uglifyjs takes 2 parameters:
uglifyjs [input files] [options]
I have learned that i can use the pipe operator on functions with one parameter without modifications. But how should i handle 2 parameters?
Also, uglifyjs will write the result to STDOUT. That means that i can simply use > to write it to a file?
It looks like uglifyjs can process multiple filenames or STDIN.
So I think you have two options:
Option 1 - Pipe the contents of the file into uglifyjs
dir .\build -filter *.js | Get-Content | uglifyjs -o bundle.js
Option 2 - Pass the filenames into uglifyjs
$files = (dir .\build -filter *.js | select -expandproperty Name) -Join " "
uglifyjs $files -o bundle.js

Import CSV foreach loop process

I am working on an issue and I can't seem to get the syntax correct.
I have a directory which has a series of csv files which each contain a list of virtual directories and paths from an old IIS6 machine. I am recreating those on a new IIS7.5 machine and I am able to get them added one directory at a time by going to the directory "iis:\sites\Atlanta" and running this command.
Import-Csv C:\Users\MIGXHZ700\Desktop\Atlanta.csv | Where-Object {$_.Path -match "\\"} | ForEach-Object {New-Item $_.Name -Type VirtualDirectory -physicalPath $_.Path}
For the life of me I can't get the syntax right to run this in a script. I think it's just an issue with concatenation, but I am not 100% sure. Here is where I am at with the script.
$dirs = ls C:\Users\[blah]\Desktop\*.csv | foreach-object {
Import-Csv $_ |
Where-Object {$_.Path -match "\\"} |
ForEach-Object {New-Item 'IIS:\Sites\'+$_.Name -Type VirtualDirectory -physicalPath $_.Path}
}
It also might be an issue doing Foreach inside of a Foreach?
Thanks in advance for any help.
'IIS:\Sites\'+$_.Name is not a valid argument to New-Item, because the -Path parameter takes a string argument, but that's an expression. It's an expression that evaluates to a string representing the path of the item you want to create, but you need to evaluate it by enclosing it in parentheses:
New-Item ('IIS:\Sites\' + $_.Name) -Type VirtualDirectory -PhysicalPath $_.Path
BTW, what's your intention for $dirs? It will be assigned the output of the New-Item command, which will be an array of DirectoryInfo objects (the same as what you'd get from $dirs = Get-ChildItem IIS:\Sites\ after creating all those directories). Is that what you want?

Resources