How to split PDF files in PowerShell with iTextSharp (iText v.5) - powershell

New to PowerShell; working through the Windows PowerShell Basics course on edX. But I like to have a project to help me learn. Project: take a folder full of 2 page PDFs and split them each into 2 files, one for each page.
I found iTextSharp but I just cannot figure it out and it is driving me mad. I admit, I know nothing about .NET and next to nothing about PowerShell, so that is likely the main problem. I'm hoping someone can help me out with the basics of iTextSharp in PowerShell.
Embarrassed to post the code I have so far, but here it is:
Add-Type -Path itextsharp.dll
$Files = Get-ChildItem -Filter *.PDF
if (-not (Test-Path .\page1)) {New-Item -Type Directory -Path .\page1}
foreach($File in $Files) {
$EntireDoc = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $File
$Page1 = $EntireDoc.GetPageContent(1)
$Page2 = $EntireDoc.GetPageContent(2)
}
Even if that did work, I'm not sure how to save the newly created PDF files.

Related

PowerShell Move Files From One Server to Another

I know this has been asked a million times, but I can't seem to find anything that works for me. I don't know if there is a permissions issue or what, but I am trying to move files from one server to another using a PowerShell script in the task scheduler and it worked for about a week before it stopped working. There are no errors in the task scheduler, and I'm not well versed in PowerShell at all, I'm just trying to get something quick and simple for our CMS manager to move her files from the website to a folder on another server.
$ORG = "E:\folders\uploads\" ## enter current source folder
$DEST= "\\server-folder-structure\uploads\" ## enter your destination folder
foreach ($ORG in gci $DEST -include *.doc,*.docx,*.pdf,*.png,*.gif,*.jpg,*.jpeg,*.html,*.htm -recurse)
{
Move-Item -path $ORG -destination $DEST ## Move the files to the destination folder
}
I tried this too, in hopes it would work, but still no files are being moved.
Get-ChildItem E:\folder-structure\uploads\* -Include *.doc,*.docx,*.pdf,*.png,*.gif,*.jpg,*.jpeg,*.html,*.htm -Recurse |ForEach-Object { Move-Item $_.FullName \\server-folder-structure\uploads\ }
Am I doing something wrong? Are there permissions to folders that I need to set that I don't know about? Is PowerShell just not the best way to do this? Thanks in advance.
I believe you're making this harder than it should be (with respect to PowerShell being new to you). You don't need a loop on any of your examples if you want to pipe directly to Move-Item:
$ORG = "E:\folders\uploads\" ## enter current source folder
$DEST = "\\server-folder-structure\uploads\" ## enter your destination folder
$filterFor = "*.doc","*.docx","*.pdf","*.png","*.gif","*.jpg","*.jpeg","*.html","*.htm"
Get-ChildItem $ORG -Include $filterFor -File -Recurse |
Move-Item -Destination $DEST -WhatIf
As for what you tried, and as Mathias pointed out, you would be searching your $DEST location in which the files wouldn't exist as they would only be in $ORG; given that that's the actual source folder.
This would also overwrite you $ORG variable with the current item in your iteration in: foreach ($ORG in gci ..){ ... }.
Meaning, your Move-Item would be invalid.

delete folder and contents with Powershell

I'm trying to delete the contents of the users folder. It works for files but it leaves folders. How can I take folders with it? This is what i have tried using powershell:
$folders = #(
"C:\Users\*\Downloads\*",
"C:\Users\*\Downloads\*.*",
"C:\Users\*\Documents\*.*",
"C:\Users\*\Documents\*.*",
"C:\Users\*\Pictures\*",
"C:\Users\*\Pictures\*.*",
"C:\Users\*\Desktop\*",
"C:\Users\*\Desktop\*.*",
"C:\Users\*\Videos\*",
"C:\Users\*\Videos\*.*")
foreach ($folder in $folders) {Remove-Item $folder -force -recurse -ErrorAction SilentlyContinue}
exit 0
How can i take parents folders in those folders and their child elemetns? I want complete empty Document, desktop etc.
I got it, im slightly stupid in my script i noted:
"C:\Users*\Documents*.",
"C:\Users*\Documents*.",
rip and that was the folder i was working with.. RIP! i changed it to
"C:\Users*\Documents*",
"C:\Users*\Documents*.*",
now it works!! thanks

ls into an extracted directory

Using PowerShell, I am downloading and extracting a file that has a directory and another file in it (it's basically from https://aka.ms/downloadazcopy-v10-windows). I'd like to be able to get into the directory after extraction.
So, in PS, I am at c:\AzCopyTest while downloading the file. It is being extracted at the same location. Here's the code for it:
$URL = "https://aka.ms/downloadazcopy-v10-windows"
New-Item -ItemType Directory -Path c:\AzCopyTest
$Destination = "c:\AzCopyTest\zzz.zip"
$WebClient = New-Object -TypeName System.Net.WebClient
$WebClient.DownloadFile($URL, $Destination).
$ExtractLocation = "c:\AzCopyTest"
$ExtractShell = New-Object -ComObject Shell.Application
$file = $ExtractShell.NameSpace($Destination).Items()
$ExtractShell.NameSpace($ExtractLocation).CopyHere($file)
How can I get into the folder after the extraction is done? FYI, I don't want to ls into it directly (or manually instead). I'd like to be able to list out the items in that directory and get the first directory. azcopy_windows_amd64_10.3.4 is what the directory called BTW. Now, when MS realease a new version (say 10.3.5), the directory will be renamed, and I do not want to go back in and manually change it. You get where I am going with this..
I know Get-ChildItem -Path ("$ExtractLocation") -Recurse will list the items in the directory and the sub-directories. But it does not server my purpose unfortunately.
Any help is greatly appreciated!
I tried this:
Get-ChildItem -Path $ExtractLocation -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object).Name and it worked.
FYI, at a later time, if MS decided to add another directory in the zipped file, you can simple do this:
Get-ChildItem -Path $ExtractLocation -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object).Name[<position_of_the_directory_starting_from_0]

Jenkins: Convert .docx to .pdf issues

We're trying to use Jenkins to convert a set of "Working Documents" into "Release Documents" when it builds out project. This involved taking the .docx files and saving them as .pdf files, which we accomplish with the following Powershell script:
$documents_path = "E:\Documentation\Working Documents\"
$word_app = New-Object -ComObject Word.Application
echo $word_app
# This filter will find .doc as well as .docx documents
$files = Get-ChildItem -Path $documents_path -Filter *.doc?
ForEach ($file in $files) {
echo "Converting Document to PDF: $($file.FullName)"
$document = $word_app.Documents.Open($file.FullName)
$pdf_filename = "$($file.DirectoryName)\..\Release Documents\$($file.BaseName).pdf"
$document.SaveAs([ref] $pdf_filename, [ref] 17)
$document.Close()
}
$word_app.Quit()
Now, that script works 100% the way we expect when I log in to the Jenkins PC and run it myself in Powershell. However, when Jenkins tries running it we get You cannot call a method on a null-valued expression at $document.SaveAs and $document.Close.
I assume this is because the user Jenkins runs as, SYSTEM does not have permission to access the .docx files, or can't find the Word installation, or something of that nature, but I can't think of how I should try to debug it further than this. Any tips are very much appreciated!
I had the same problem and found a simple workaround.
Create an empty directory "Desktop" in
C:\Windows\SysWOW64\config\systemprofile\

Where in the world is complete documentation for PowerShell scripting?

I started doing some powershell scripting today for my work and I can find this page: http://technet.microsoft.com/en-us/library/hh849827.aspx
This shows all the Cmdlets that I am using in the scripts, but I cannot find the documentation on how to use the returned objects of these Cmdlets. For example, I am using the Get-ChildItem cmd to get all the files in a dir recursively. Then I am using a ForEach loop like this:
$dest = "C:\Users\a-mahint\Documents\Testing\Dest"
$destlist = Get-ChildItem $dest -Recurse
foreach ($file in $destlist){
write-host "File: $file"
write-host $file
$result = test-path -path "C:\Users\a-mahint\Documents\Testing\Src\*" -include $file.Name
if (-not $result){
Copy-Item $file -Destination "$backup"
}
}
write-host "Done copying deleted files"
Except I have no idea what type of object a $file is...In the documentation above, it just says it outputs a System.Object, but that doesn't help at all. I want to know all the properties of this object so I can use them to debug.
From a question I asked one time Andy Arismendi supplied some links for me to read.
You can download said specification: 2.0 and 3.0.
$file = Get-Item C:\foo.txt
Remember there is a $file | Get-Member command you can use to view the objects methods and properties. Also since everything in PowerShell is an Object you can always do $file.GetType() and then Bing that type.
Get-ChildItem "C:\Windows\System32\WindowsPowerShell\v1.0\en-US" -Filter *.txt
Here is a decent reference for get-childitem.
http://technet.microsoft.com/en-us/library/ee176841.aspx
What kind of file details are you needing exactly?