Monday, November 23, 2015

Automatically saving a file in IE

Requirement: Using PowerShell, go to a specific site/URL in Internet Explorer (IE), when prompted to save the file, save it, and close out of the 'Downloads'  dialog box.


Here is the PowerShell code that I came up with.  This is intended to work for a site/URL that when you navigate to it, it prompts you to save a document/file.


#Start IE and navigate to your download file/location
$ie = New-Object -Com internetExplorer.Application
$ie.Navigate("http://www.your.website.here.com/awesome_stuff/DOC_1.docx")

#------------------------------
#Wait for Download Dialog box to pop up
Sleep 5
while($ie.Busy){Sleep 1}
#------------------------------

#Hit "S" on the keyboard to hit the "Save" button on the download box
$obj = new-object -com WScript.Shell
$obj.AppActivate('Internet Explorer')
$obj.SendKeys('s')

#Hit "Enter" to save the file
$obj.SendKeys('{Enter}')

#Closes IE Downloads window
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{Enter}')


Please let me know your feedback!!!

Wednesday, November 18, 2015

Copy a file from SharePoint without Microsoft.SharePoint.Powershell Snap-in

My requirement is to find the most up-to-date file in a SharePoint document library.  The caveat is that I cannot run PowerShell on the SharePoint server.  So how do we do this?

First, determine the File Explorer address of your document library.
  • Check to see if the 'webclient' service is running:
  • Copy the SharePoint document library URL and paste it to your local text editor.
    • SharePoint URL example: http://sharepoint.url.com/site/Your_Site_Name/Sub_Site/SharedDocuments/
  • Drop the "http:" from the URL
  • Switch all of the "/" to "\", so your folder location looks like this:
    •  \\sharepoint.url.com\site\Your_Site_Name\Sub_Site\SharedDocuments\
  • Copy this new address, click on 'Start', then 'Run', paste the new location text, then click 'OK'.
  • You may have to play around with the location name, but once you get it, Explorer will open up and you will see the folders and/or the documents in that Share Documents directory. 

Second, using PowerShell, determine the most-up-to-date file and copy it.


$dir = "\\sharepoint.url.com\WWWRoot\site\Site_Name\Sub_Site\SharedDocuments\Folder_1\Sub_Folder_1\File_Name*"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name

$fromfile = "\\sharepoint.url.com\WWWRoot\site\Site_Name\Sub_Site\SharedDocuments\Folder_1\Sub_Folder_1\"+$latest.name
$tofile   = "c:\temp\YOURE_FILE.xlsx"

Copy-Item $fromfile $tofile

  • If you know part of the file name, your can put that at the end of the directory ($dir) location and add a "*" for a wildcard search.