Sign in
|
Join PowerShell.com!
|
Help
Home
PowerTips
Ask the Experts
Forums
Webcasts
Blogs
eBookV2
Script Library
Twitter Grid
Modules
QuickClick
Scripts
Snippets
Videos
Library
»
Script Library
»
SharePoint
»
Slideshow
SharePoint Slideshow
Share
|
Browse Library
Module Library
QuickClick Library
Script Library
Active Directory
BizTalk
Citrix
Clustering
Desktop
Exchange Server 2003
Exchange Server 2007
File System
Group Policy
Internet Information Server (IIS)
Local Accounts
Logs
Microsoft Office
Microsoft Team Foundation Server
MySQL
Networking
Registry
Remote Desktop Services
Remoting
Security
SharePoint
SQL Server
System Center Virtual Machine Manager
System Center Configuration Manager
System Center Operations Manager
Tutorial
Terminal Server
Using .Net
Virtual Server
VMware
Windows 7
Windows HPC
Windows Server 2000
Windows Server 2003
Windows Server 2008
Windows XP
WMI
Misc
Snippet Library
Video Library
Members Only
Tags
AppPath
Documents
List Items
List Properties
SharePoint
Site Methods
Site Properties
Solutions
View more
Using Powershell to run Excel macro on all files of sharepoint document library
Param
(
$DocLibSite
=
"http://MyMOSS/Reports"
,
# SHAREPOINT Document Library site
$DocLibName
=
"Daily"
# SHAREPOINT Document Library Name
)
#REGION EXCEL FUNCTIONS
function
Invoke([
object
]
$m
, [
string
]
$method
,
$parameters
)
{
$m
.
PSBase.GetType
().
InvokeMember
(
$method
, [
Reflection.BindingFlags
]::
InvokeMethod
,
$null
,
$m
,
$parameters
,
$ciUS
)
}
Function
RefreshExcel {
Param
(
$xls
)
#Only XL* files
if
(
$xls
.
split
(
"."
)[
-
1]
-match
"xl*"
) {
# US culture
$ciUS
=
[
System.Globalization.CultureInfo
]
'en-US'
$Excel
=
New-Object
-ComObject
excel.application
$thisThread
=
[
System.Threading.Thread
]::
CurrentThread
$originalCulture
=
$thisThread
.
CurrentCulture
$thisThread
.
CurrentCulture
=
New-Object
System.Globalization.CultureInfo
(
"en-US"
)
$Excel
.
Visible
=
$false
$Excel
.
Displayalerts
=
$false
$Books
=
$Excel
.
Workbooks
Write-Progress
-Activity
"Open file"
-status
"f:"
-PercentComplete
10
-CurrentOperation
$xls
Try {
$ExcelWB
=
Invoke
$Books
open(
$xls
);
} catch {
Write-Host
"Open:$_"
;
$Excel
.
quit
() |
Out-Null
;
Return
$false
}
Write-Progress
-Activity
"Run Macro"
-status
"f:"
-PercentComplete
40
-CurrentOperation
$xls
#Run Refresh Macro
Try {
$Excel
.
Application.Run
(
"Refresh"
);
} catch {
Write-Host
"Run:$_"
;
$Excel
.
quit
() |
Out-Null
;
Return
$false
}
Write-Progress
-Activity
"Save file"
-status
"f:"
-PercentComplete
60
-CurrentOperation
$xls
Try {
invoke
$ExcelWB
Save|
Out-Null
} catch {
Write-Host
"Save:$_"
;
$Excel
.
quit
() |
Out-Null
;
Return
$false
}
invoke
$ExcelWB
Close |
Out-Null
$Excel
.
quit
() |
Out-Null
$thisThread
.
CurrentCulture
=
$originalCulture
Return
$true
}
else
{}
}
#REGION SHP
Function
Get-SHPDocList
{
Param
(
$site
,
$listName
,
$rowLimit
=
"100"
)
$uri
=
$site
+
"/_vti_bin/lists.asmx?WSDL"
# Create the service
try{
$service
=
New-WebServiceProxy
-Uri
$uri
-Namespace
SpWs
-UseDefaultCredential
}
catch{
Write-Error
$_
-ErrorAction
:
'SilentlyContinue'
}
# Create xml query to retrieve list.
$xmlDoc
=
new-object
System.
Xml
.
XmlDocument
$query
=
$xmlDoc
.
CreateElement
(
"Query"
)
$viewFields
=
$xmlDoc
.
CreateElement
(
"ViewFields"
)
$queryOptions
=
$xmlDoc
.
CreateElement
(
"QueryOptions"
)
$query
.
set_InnerXml
(
"FieldRef Name='ID'"
)
$list
=
$null
if
(
$service
-ne
$null
){
try{
$list
=
$service
.
GetListItems
(
$listName
,
""
,
$query
,
$viewFields
,
$rowLimit
,
$queryOptions
,
""
)
}
catch{
Write-Error
$_
-ErrorAction
:
'SilentlyContinue'
}
}
if
(
$list
) {
$list
.
data.row
|
%
{
$_
.
ows_EncodedAbsUrl
}
}
}
#ENDREGION SHP
#ENDREGION EXCEL FUNCTIONS
Get-SHPDocList
$DocLibSite
$DocLibName
1000 |
%
{
if
(
$_
) {
Write-Host
"Refreshing ... $_"
if
(RefreshExcel
$_
) {
Write-host
"
SUCCESS Refresh on $_"
}
else
{
"
FAILED Refresh on $_"
}
}
}
Loading...
Using Powershell...
Import-SPSite...
Export-SPSite...
Remove-SPSite...
Remove-SPList...
Remove-SPItem...
Remove-SPField...
Add-SPListViewWebPart...
Add-SPImageWebPart...
Add-SPSitePermission...
View all files
Copyright 2011 PowerShell.com. All rights reserved.