Create IP.txt from Hostnames.txt - powershell

What am I doing wrong?
I want to create a new text file containing IP Addresses from a text file containing only Hostnames.
function Get-HostToIP($hostname) {
$result = [system.Net.Dns]::GetHostByName($hostname)
$result.AddressList | ForEach-Object {$_.IPAddressToString }
}
Get-Content "hostnames.txt" | ForEach-Object {(Get-HostToIP($_)) >> ip.txt}

By default, Powershell is looking under C:\windows\system32 folder for the files. Replace the file names with full path of the files and it should work:
Get-Content "C:\path\to\hostnames.txt" | ForEach-Object {(Get-HostToIP($_)) >> c:\path\to\ip.txt}

Related

Merge PDF files using CSV file list using Powershell

I want to create multiple merged PDF files from around 1400+ pdf files.
I have a data.csv file with 2 columns as below.
The PDF files with filename matching Filename column and data.csv file are in the same folder.
I need to create multiple merged PDF files and each merged PDF will have group of files that have the same First three characters in the filename.
e.g.,
The filenames starting with EIN* need to be merged into one PDF file in the same sorting order as in the data.csv file. The filename of merged PDF should be Y followed by the first three characters. so in this example it should be YEIN.pdf
This process need to be looped in until all the rows in data.csv are actioned.
sample data.csv file
FilePath Filename
$FilePath1 EINCO01-174.pdf
$FilePath2 EINCO02-174.pdf
$FilePath3 EINCO03-174.pdf
$FilePath4 EINCO04-174.pdf
$FilePath5 EINCL01-174.pdf
$FilePath6 EINCL02-174.pdf
$FilePath7 EINCL03-174.pdf
$FilePath8 EINCL04-174.pdf
$FilePath9 EINCL05-174.pdf
$FilePath10 EINCL06-174.pdf
$FilePath11 EINCL07-174.pdf
$FilePath12 EINCL08-174.pdf
$FilePath13 EINCL09-174.pdf
$FilePath14 EINCL10-174.pdf
$FilePath15 EINCL11-174.pdf
$FilePath16 EINCL12-174.pdf
$FilePath17 EINCL13-174.pdf
$FilePath18 EINCL14-174.pdf
$FilePath19 EINCL15-174.pdf
$FilePath20 EINCL16-174.pdf
$FilePath21 EINCL17-174.pdf
$FilePath22 EINCL18-174.pdf
$FilePath23 EINCL19-174.pdf
$FilePath25 GINLG01-170.pdf
$FilePath26 GINLG02-166.pdf
$FilePath27 GINLG03-159.pdf
$FilePath28 GINLG04-159.pdf
$FilePath29 GINLG05-168.pdf
$FilePath30 GINLG06-152.pdf
$FilePath31 GINNO01-174.pdf
$FilePath32 GINNO02-131.pdf
$FilePath33 GINNO04-150.pdf
$FilePath34 GINNO05-174.pdf
$FilePath35 GINTA01-130.pdf
$FilePath36 GINTA02-139.pdf
$FilePath37 GINTA03-139.pdf
So to tackle this I have created a script to split data.csv file into multiple CSV files grouped by the First three characters as below.
$data = Import-Csv '.\data.csv' |
Select-Object Filepath,Filename,#{n='Group';e={$_.Filename.Substring(0,3)}}
$data | Format-Table -GroupBy Group
Group-Object {$_.Group}| ForEach-Object {
$_.Group | Export-Csv "$($_.Group).csv" -NoTypeInformation
}
foreach ($Group in $data | Group Group)
{
$data | Where-Object {$_.Group -eq $group.name} |
ConvertTo-Csv -delimiter "`t" -NoTypeInformation |
foreach {$_.Replace('"','')} |
Out-File "$($group.name).csv"
}`
From here, I am unable to proceed to next step to achieve what I need to. I presume there could be a better way to do this.
PS: I have installed PSWritePDF module on my machine.

Powershell - convert Windows Eventlog multiline message to single line

Scenario: Working PS script traverses multiple folders containing logs (folder name is the name of the originating server), extracting the last line from the last completed log - this works, but in case the last log line is part of a multiline message, I get useless data from this line.
Question: How can I implement in the working script a cleanup that puts multiline messages all in one line?
Working script (original post)
# List all folders on the Initial Path
$folders = Get-ChildItem D:\Logs | Select-Object -ExpandProperty FullName
$export = [system.collections.generic.list[pscustomobject]]::new()
foreach($folder in $folders)
{
# Get the newest file in each folder
$file = Get-ChildItem $folder -File | Sort-Object LastWriteTime -Descending | Select -First 1
# Read the content of the file and get the last line
#$content = (Get-Content $file)[-1]
$content = (Get-Content $file.FullName)[-1]
# Here you can create a new object that will be used to export to Csv
# As an example, i'll create an object with the File Name,
# Last Write Time and Last Line of the Content (This will be the CSV columns)
$export.Add(
[PSCustomObject]#{
DirName=$file.FullName
# FileName = $file.Name
LastWriteTime = $file.LastWriteTime
LastLine = $content
})
}
# After the loop finishes you can export the results to a Csv
$export | Export-Csv -NoTypeInformation D:\export\MyFilelist_LastLogLines.csv

compare 2 arrays and wrap the output in quotes using powershell

In a directory there will be 2 type of files with same name but different extension for eg:1.png , 1.txt, test_76.png,test_76.txt, test_70.png. In this case test_70.png dosent have its .txt counterpart.I am using this code
$scL = dir *.txt | select Basename
$SList = #($scL -notmatch "MICRO")
$pList = dir *.png | select Basename
$PList = #($pList -notmatch "MICRO")
Compare-Object $PList $SList | ForEach-Object {$_.InputObject} | out-file result.txt
but i am unable to get the desired output that is test_70.png in the output file.Also i want to wrap the output inside the text file in double quotes i.e; the outfile should contain entry like this "test_70.png". Can anyone throw light on what am i missing in the compare-object cmdlet.
Your InputObject is a file object so you will need to access the name. Also in Powershell you can access the properties of the the items in a collection directly. Something like this:
(Compare-Object $PList $SList).InputObject.Name | % {"`"$_`""} | Out-File result.txt

Powershell script to print file-path of every file in a folder

I have 2 folders: 'Old' and 'New'. Most of the files from 'Old' have been copied to 'New'. However, the structure of the sub-folders in 'Old' and 'New" are different. So the file-path for a file in 'Old' is very different from its copy in 'New'.
I need to loop through each file in 'Old', search for that file in 'New', and write the old and new file-paths for each file to a text file.
I have been assigned to do this manually, but it will take a long time due to the number of files. So I want to write a script. I am new to Powershell and am having difficulty figuring out which cmdlets can help me with my task.
I will appreciate any kind of guidance. Thank you.
try Something like this
#list old files
$patholdfile="c:\temp"
$listoldfile=Get-ChildItem $patholdfile -File -Recurse | select Name, FullName
#list new files
$pathnewfile="c:\temp2"
$listnewfile=Get-ChildItem $pathnewfile -File -Recurse | select Name, FullName
#extract liste file old and name and search all file in newlist
$resultsearch=#()
foreach ($currentfile in $listoldfile)
{
$resultsearch+=New-Object psobject -Property #{
Name=$currentfile.Name
OldPath=$currentfile.FullName
#if you want the firt founded uncomment this and comment after
#NewPaths=($listnewfile | Where {$_.Name -eq $currentfile.Name} | select FullName -First 1).FullName
NewPaths=($listnewfile | Where {$_.Name -eq $currentfile.Name} | select FullName).FullName -join "~"
}
}
#export result in csv file
$resultsearch | select name, OldPath , NewPaths | export-csv -Path "c:\temp\result.txt" -NoTypeInformation

How to get folder names using powershell or any windows script?

Say I have following file structure
L:\Enterprise\Legal\Planning\Contacts\A-D\
Under A-D I have 50 Folders which is named after company name. So it looks like this
L:\Enterprise\Legal\Planning\Contacts\A-D\3-M Company\subfolder\sub-subfolder\files.txt
L:\Enterprise\Legal\Planning\Contacts\A-D\ABC Company\subfolder\sub-subfolder\files.txt
L:\Enterprise\Legal\Planning\Contacts\A-D\XYZ Company\subfolder\sub-subfolder\files.txt
L:\Enterprise\Legal\Planning\Contacts\A-D\AAA Company\subfolder\sub-subfolder\files.txt
Is there a way to get the company name from the above file structure using powershell or some windows script?
if your goal is to have a list of company name one way is:
cd L:\Enterprise\Legal\Planning\Contacts\A-D\
dir | select -expand fullname | % { ($_ -split '\\')[6] }
Some people may also find that this works instead
dir | select -expand fullname | % { ($_ -split '\\')[7] }
If you are importing a list of files/folders from a CSV, you could do this:
$strings = Import-CSV -Path "C:\PathToCSV.csv"
foreach ($string in $strings) {
$string -replace 'L:\\Enterprise\\Legal\\Planning\\Contacts\\A-D\\' -replace '\\subfolder\\sub-subfolder\\files.txt'
}
Note the double-slashes (\\). You have to escape the slashes, otherwise they are treated as special. Output should look like:
3-M Company
ABC Company
XYZ Company
AAA Company