Reading a text file line by line - powershell

I'm new to power shell and can't seem to get this script correct.
$WshShell = New-Object -comObject WScript.Shell
$users = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users.txt'
$users1 = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users1.txt'
$Shortcut = $WshShell.CreateShortcut("\\Fssrv\homeshares$\$users1\$users.lnk")
$Shortcut.TargetPath = "\\asrv1\users\$users"
$Shortcut.Save()
Every time it goes to "users.txt" it tries to read it as a complete file. Instead i would like for it to read as followed:
User1
User2
User3
I just can't seem to get this to work.
RE-EDIT:
What I’m trying to accomplish:
We went from one storage server to a new storage server.
ASRV1 to FSSRV
I want to create a shortcut from asrv1 to fssrv. Along with the upgrade we are also changing domains and changing everyone windows user names.
For example my user name was fMunoz and it was changed to fMunoz00.
I want to pull from a text file > users.txt with all the old usernames, and create shortcuts to the new user names storage file those users names are in a txt file called users1.txt.

If I understand your request, you want to create shortcuts in each home directory of the people in Users1.txt
This means you will need to complete multiple ForEach loops to cycle through both txt files.
$WshShell = New-Object -comObject WScript.Shell
$users = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users.txt'
$users1 = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users1.txt'
# For every line in $users1, Do something
Foreach($User in $users1) {
# For every line in $users, Do something
Foreach($Name in $users) {
$Shortcut = $WshShell.CreateShortcut("\\Fssrv\homeshares$\$User\$Name.lnk")
$Shortcut.TargetPath = "\\asrv1\users\$Name"
$Shortcut.Save()
}
}

I want to thank everyone for helping me.
I came up with a different solution for my problem. The way i fixed this issue was by creating an excel sheet w/ 2 columns.
OLD USERS | NEW USERS
I then created a mail merge using microsoft word. Then i continued with the following code in powershell to start my loop:
$WshShell = New-Object -comObject WScript.Shell
Once my loop was started with i then utilized mail merge to take <> from the excel sheet and replaced it with the insert it into my code. Also took <> and did the same.
$Shortcut = $WshShell.CreateShortcut("\Fssrv\homeshares$\«New_Users»\«Old_Users»_Z Drive.lnk")
$Shortcut.TargetPath = "\asrv1\users\«Old_Users»"
$Shortcut.Save()
I hope this can help anyone else trying to accomplish something similar.
Yet again, Thanks & Good Luck...

Related

How do I get the target of a web shortcut lnk file?

This is a rather unique issue, I'm finding. I have created a web shortcut using the following script:
$target = "http://internalwebsite/subsite"
$shortcut_lnk = "$publicDesktop\usefulname.lnk"
$wscriptshell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($Shortcut_lnk)
$Shortcut.TargetPath = $Target
$ShortCut.IconLocation = "https://internalwebsite/1.ico, 0";
$Shortcut.Save()
Unfortunately, the target is http instead of HTTPS. I would like to detect any shortcuts that have the unsecure version and replace it with the secure version.
I've found the following code, which is supposed to give me the information I need:
$shortcut = 'C:\users\public\Desktop\usefulname.lnk'
$sh = New-Object -ComObject WScript.Shell
$target = $sh.CreateShortcut($shortcut).TargetPath
However, this returns no data. If I just run:
$sh.CreateShortcut($shortcut)
It returns:
FullName : C:\users\public\Desktop\usefulname.lnk
Arguments :
Description :
Hotkey :
IconLocation : https://internalwebsite/1.ico,0
RelativePath :
TargetPath :
WindowStyle : 1
WorkingDirectory :
I've confirmed that the link works as intended. My question now - how do I get the actual web link? If I go to the properties of the link, I see that the target type is the website (http://internalwebsite/subsite) and the target is greyed out with the same info. But so far, my Google-fu has yielded nothing in regards to listing the target type, or explaining why I can't pull the path (or any alternative methods).
If anyone can help I would greatly appreciate it!
You need to create the web shortcut in a slightly different way. According to this TechNet forum posting, the filename must have an extension of .url rather than .lnk, and you must explicitly call the .Save() method of the object.
Jeff Zeitlin, thank you, that ended up being the answer for me! I ended up having to do a little more looking to create a custom icon for a URL (uses a slightly different method). Looks like I'm going to have to remove the link and replace it with a URL. I'm using the following script (part of which I pilfered from a different stack overflow question):
$WshShell = New-Object -comObject WScript.Shell
$targetPath = "http://internalwebsite/subsite"
$iconLocation = "https://internalwebsite/1.ico"
$iconFile = "IconFile=" + $iconLocation
$path = "$publicDesktop\usefulname.url"
$Shortcut = $WshShell.CreateShortcut($path)
$Shortcut.TargetPath = $targetPath
$Shortcut.Save()
Add-Content $path "HotKey=0"
Add-Content $path "$iconfile"
Add-Content $path "IconIndex=0"
THEN, if I want to figure out information about the file, I can use:
get-content $path
This makes any future scripting against the path significantly easier. Thank you very much for the help!

Marking Emails as Read

I might be asking this in the wrong place so I apologize. I'm running a PS1 file to look in a specific folder, mark the emails as read, save the attachments and send me an email. For whatever reason, marking the emails as read, doesn't always work.
Since everything else works perfectly, I'll leave you with just what is not working
#Open Outlook and find emails from today
$ol = New-Object -ComObject Outlook.Application
$ns = $ol.GetNamespace('mapi')
$mb = $ns.Stores['some#email.com'].GetRootFolder()
$inbox = $mb.Folders['Reports']
$inbox.Items | ForEach-Object {$_.UnRead = $false}
If I run the script in ISE one line at a time, it works.
The problem arises when I select all and run in ISE, or as a PS1
Thanks!

How to display excel data on powershell console using powershell script

I am trying to display excel data(text format) present across 10 columns in powershell console
But it is not working
$objExcel = New-Object -ComObject Excel.Application
$objexcel.visible=$true
$objexcel.DisplayAlerts=$false
$workbook=$objExcel.Workbooks.open("C:\Users\Desktop\commute.xlsx")
$worksheet=$workbook.sheets.item(1)
$usedrange=$worksheet.UsedRange
#{TIME=$worksheet.Range("A1:A10").text
DEPART=$worksheet.Range("B1:B10").text
REDS=$worksheet.Range("C1:C10").text
TRAINS=$worksheet.Range("D1:D10").text }
why don't you use the cmdlet Import-CSV? It will be much easier.
try the following:
$Worksheet = Import-Csv -Path 'C:\Users\Desktop\commute.xlsx'
It will create a Powershell object you can use and manipulate.
hope it helps you.

Printing all Excel files in a folder using PowerShell

I have a PowerShell script that I wrote a few years ago and it worked great...until it didn't.
$shell = New-Object -com Shell.Application
$filepath = 'C:\Billing\Clients'
$shell.Namespace($filepath).Items() |
% { $_.InvokeVerb('Print') }
In the C:\Billing\Clients folder I copy ~100 Excel files. Each of these Excel files need to be printed, in the alphabetical order of the file name.
This was working great until this month. I guess an update to Excel changed things.
Now the script tries to open and print all of the Excel files at the same time. Previously it printed the files in serial.
This was awesome. Now it brings my system to it's knees and documents are printed in a random order.
Any ideas on how I can invoke the Print operation and wait for it to complete prior to calling the Print operation on the next file?
I have used this in the past to pull files into excel and print them. You will need to ensure your default print settings are set first or use $xl.ActivePrinter = "PRINTERNAME AS EXCEL SEES IT" to set it first.
#Open Excel
$xl = new-object -comobject excel.application
#don't show the window
$xl.visible = $false
#get all of your files
get-childitem "C:\SOME\Path" "*.xlsx" | foreach-object {
#open file
$wb = $xl.Workbooks.Open($_.FullName)
#print with defaults
$wb.PrintOut()
#close without saving any changes
$wb.Close($false)
}
#all done so close excel
$xl.Quit()

iTextSharp to merge PDF files in PowerShell

I have a folder which contains thousands of PDF files. I need to filter through these files based on file name (which will group these into 2 or more PDF's) and then merge these 2 more more PDF's into 1 PDF.
I'm OK with group the files but not sure the best way of then merging these into 1 PDF. I have researched iTextSharp but have been unable to get this to work in PowerShell.
Is iTextSharp the best way of doing this? Any help with the code for this would be much appreciated.
Many thanks
Paul
Have seen a few of these PowerShell-tagged questions that are also tagged with itextsharp, and always wondered why answers are given in .NET, which can be very confusing unless the person asking the question is proficient in PowerShell to begin with. Anyway, here's a simple working PowerShell script to get you started:
$workingDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path;
$pdfs = ls $workingDirectory -recurse | where {-not $_.PSIsContainer -and $_.Extension -imatch "^\.pdf$"};
[void] [System.Reflection.Assembly]::LoadFrom(
[System.IO.Path]::Combine($workingDirectory, 'itextsharp.dll')
);
$output = [System.IO.Path]::Combine($workingDirectory, 'output.pdf');
$fileStream = New-Object System.IO.FileStream($output, [System.IO.FileMode]::OpenOrCreate);
$document = New-Object iTextSharp.text.Document;
$pdfCopy = New-Object iTextSharp.text.pdf.PdfCopy($document, $fileStream);
$document.Open();
foreach ($pdf in $pdfs) {
$reader = New-Object iTextSharp.text.pdf.PdfReader($pdf.FullName);
$pdfCopy.AddDocument($reader);
$reader.Dispose();
}
$pdfCopy.Dispose();
$document.Dispose();
$fileStream.Dispose();
To test:
Create an empty directory.
Copy code above into a Powershell script file in the directory.
Copy the itextsharp.dll to the directory.
Put some PDF files in the directory.
Not sure how you intend to group filter the PDFs based on file name, or if that's your intention (couldn't tell if you meant just pick out PDFs by extension), but that shouldn't be too hard to add.
Good luck. :)