Skip to content

Commit a242312

Browse files
Merge pull request #1 from sharefile-org/initial
Initial commit
2 parents 1c6d806 + 63fbb6d commit a242312

91 files changed

Lines changed: 11948 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
#ignore thumbnails created by windows
3+
Thumbs.db
4+
#Ignore files build by Visual Studio
5+
*.obj
6+
*.pdb
7+
*.user
8+
*.aps
9+
*.pch
10+
*.vspscc
11+
*_i.c
12+
*_p.c
13+
*.ncb
14+
*.suo
15+
*.tlb
16+
*.tlh
17+
*.bak
18+
*.cache
19+
*.ilk
20+
*.log
21+
[Dd]ebug*/
22+
*.lib
23+
*.sbr
24+
obj/
25+
_ReSharper*/
26+
[Tt]est[Rr]esult*
27+
*.InstallLog
28+
*.InstallState
29+
30+
TestClient/*.user
31+
TestClient/bin
32+
TestClient/obj
33+
packages/*
34+
Build/runbuild.txt
35+
docs/*
36+
37+
*/bin
38+
*/obj
39+
*/.vs
40+
/.vs

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
ShareFile PowerShell SDK
2+
========================
3+
The ShareFile PowerShell SDK is a PowerShell module that provides support for saving a user login for use in scripts, provides access to the ShareFile API, and also a provider that can be used within PowerShell to map to a ShareFile account.
4+
<br>There are two modules provided; `ShareFile` and `ShareFile-Core`. The former is for PowerShell 5.X, while the latter is for PowerShell 7.X+.
5+
6+
<table>
7+
<tr>
8+
<td height="69" width="69">
9+
<a href="https://github.com/sharefile-org/ShareFile-PowerShell-Module/releases"><img src="https://github.com/sharefile-org/ShareFile-PowerShell-Module/blob/master/Samples/Images/Download.png"/></a>
10+
</td>
11+
<td>
12+
<b>Download</b><br>
13+
Download the latest release <a href="https://github.com/sharefile-org/ShareFile-PowerShell-Module/releases">here</a>
14+
</td>
15+
</tr>
16+
<tr>
17+
<td>
18+
<a href="http://msdn.microsoft.com/en-us/library/5a4x27ek(v=vs.110).aspx" target="_blank"><img src="https://github.com/sharefile-org/ShareFile-PowerShell-Module/blob/master/Samples/Images/SystemRequirements.png"/></a>
19+
</td>
20+
<td>
21+
<b>System Requirements</b><br>
22+
The PowerShell SDK requires either <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_windows_powershell_5.1" target="_blank">PowerShell 5.x</a> and <a href="https://dotnet.microsoft.com/en-us/download/dotnet-framework/net48" target="_blank">Net Framework 4.8+</a>, or <a href="https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.4" target="_blank">PowerShell 7.x+</a> and <a href="https://dotnet.microsoft.com/en-us/download/dotnet/8.0" target="_blank">Net 8.0</a>. In either case, the <a href="https://developer.microsoft.com/en-us/microsoft-edge/webview2" target="_blank">WebView2 runtime</a> is also required.
23+
</td>
24+
</tr>
25+
<tr>
26+
<td>
27+
<a href="https://github.com/sharefile-org/ShareFile-PowerShell-Module/wiki/Getting-Started"><img src="https://github.com/sharefile-org/ShareFile-PowerShell-Module/blob/master/Samples/Images/GettingStarted.png"/></a>
28+
</td>
29+
<td>
30+
<b>Getting Started</b><br>
31+
Click <a href="https://github.com/sharefile-org/ShareFile-PowerShell-Module/wiki/Getting-Started">here</a> if you are just getting started for a list of common operations. We will walk you through your first time using the SDK using some basic operations.
32+
</td>
33+
</tr>
34+
<tr>
35+
<td>
36+
<a href="https://github.com/sharefile-org/ShareFile-PowerShell-Module/wiki/Sample-Scripts"><img src="https://github.com/sharefile-org/ShareFile-PowerShell-Module/blob/master/Samples/Images/SampleScripts.png"/></a>
37+
</td>
38+
<td>
39+
<b>Sample Scripts</b><br>
40+
<a href="https://github.com/sharefile-org/ShareFile-PowerShell-Module/wiki/Sample-Scripts">Here</a> you will find some example scripts that are commonly used by customers.
41+
</td>
42+
</tr>
43+
<tr>
44+
<td>
45+
<a href="Scheduling-Scripts"><img src="https://github.com/sharefile-org/ShareFile-PowerShell-Modulel/blob/master/Samples/Images/ScheduleScript.png"/></a>
46+
</td>
47+
<td>
48+
<b>Scheduling Scripts</b><br>
49+
Found a great script that you want to run automatically on a regular basis? Check <a href="https://github.com/sharefile-org/ShareFile-PowerShell-Module/wiki/Scheduling-Scripts">here</a> for more information on how to schedule scripts.
50+
</td>
51+
</tr>
52+
<tr>
53+
<td>
54+
<a href="https://github.com/sharefile-org/ShareFile-PowerShell-Modulel/wiki/Syncing-Files"><img src="https://github.com/sharefile-org/ShareFile-PowerShell-Module/blob/master/Samples/Images/SyncingFiles.png"/></a>
55+
</td>
56+
<td>
57+
<b>Syncing Files</b><br>
58+
Coming from the SFCLI tool? <a href="https://github.com/sharefile-org/ShareFile-PowerShell-Module/wiki/Syncing-Files">This section</a> contains details on how you can use the PowerShell SDK to do the same type of activities.
59+
</td>
60+
</tr>
61+
<tr>
62+
<td>
63+
<a href="https://github.com/sharefile-org/ShareFile-PowerShell-Module/blob/master/LICENSE"><img src="https://github.com/sharefile-org/ShareFile-PowerShell-Module/blob/master/Samples/Images/Agreement.png"/></a>
64+
</td>
65+
<td>
66+
<b>License</b><br>
67+
All code is licensed under the <a href="https://github.com/sharefile-org/ShareFile-PowerShell-Module/blob/master/LICENSE">MIT
68+
License</a>
69+
</td>
70+
</tr>
71+
</table>
72+
73+
74+
To follow us on the ShareFile blog, check here:
75+
https://www.sharefile.com/blogs

Samples/AutoArchiveFiles.ps1

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Import-Module ShareFile
2+
#Import-Module ShareFile-Core # For PowerShell 7+
3+
4+
################################################################################
5+
# AutoArchiveFiles.ps1
6+
#
7+
# This script will move all files in the specified Parent folder to
8+
# a sub folder named the current date
9+
#
10+
#
11+
12+
#Set this to the Id of the folder all the files are uploaded to
13+
$parentID = "Enter Parent Folder ID here"
14+
#Set this to the number of files you want to process at a time (recommended 1000 or less)
15+
$filesAtOnce = 1000;
16+
17+
#Run the following interactively to create a login token that can be used by Get-SfClient in unattended scripts
18+
#$sfClient = New-SfClient -Name ((Join-Path $env:USERPROFILE "Documents") + "\YourSubdomain.sfps") -Account YourSubdomain
19+
$sfClient = Get-SfClient -Name ((Join-Path $env:USERPROFILE "Documents") + "\YourSubdomain.sfps")
20+
21+
22+
#Create a new folder named with today's date
23+
$day = Get-Date
24+
$folderName = $day.Year.ToString() + "_" + $day.Month.ToString() + "_" + $day.Day.ToString()
25+
$folderInfo ='{
26+
"Name":"'+$folderName+'",
27+
"Description":"Auto-Generated Folder"
28+
}'
29+
$folder = Send-SfRequest -Client $sfClient -Entity Items -Method POST -Id $parentID -Navigation Folder -BodyText $folderInfo
30+
31+
32+
#We want to know how many files we'll be moving
33+
$pFolder = Send-SfRequest -Client $sfClient -Entity Items -Id $parentID
34+
35+
#We don't want to grab too many files at once so I limited it to 1000 at a time
36+
for($x=0; $x -le $pFolder.FileCount; $x+=$filesAtOnce){
37+
38+
#We only need the file ID and the type in order to move so we make a minimal query adding in the top to grab only 1000 files at a time
39+
$files += Send-SfRequest -Client $sfClient -Entity Items -Id $parentID -Navigation Children -Select Id -Parameters @{'$top' = "$filesAtOnce"; '$skip' = "$x"}
40+
}
41+
42+
$fileParent = '{
43+
"Parent": {"Id": "'+ $folder.Id +'"}
44+
}'
45+
46+
#Once we have the files we move them
47+
foreach($file in $files){
48+
if ($file.GetType().ToString() -eq "ShareFile.Api.Models.File"){
49+
Send-SfRequest -Client $sfClient -Entity Items -Method PATCH -Id $file.Id.ToString() -BodyText $fileParent
50+
}
51+
}

Samples/BackupShareFileAccount.ps1

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
################################################################################
2+
# BackupShareFileAccount.ps1
3+
#
4+
# Download an entire ShareFile account to a local directory.
5+
#
6+
#
7+
# Local Directory Structure:
8+
#
9+
# Local Directory
10+
# │ log-<date>.txt
11+
# │ account-<date>.csv
12+
# │ employees-<date>.csv
13+
# │ sharedfolders-<date>.csv
14+
#
15+
# └───Users
16+
# │ │
17+
# │ └───user@email.com
18+
# │ │ 'Home Folder Name'
19+
# │ └─ ...
20+
#
21+
# └───Shared Folders
22+
# │ folder
23+
# │ ...
24+
#
25+
#
26+
# Requirements:
27+
# PowerShell 5
28+
# The latest PowerShell SDK for ShareFile: https://github.com/citrix/ShareFile-PowerShell
29+
# A Superuser for the ShareFile account: https://support.citrix.com/article/CTX208527
30+
#
31+
# Usage:
32+
# .\BackupShareFileAccount.ps1; Backup-ShareFileAccount -Type Shared -Path <LOCAL BACKUP PATH> -Email <ADMIN EMAIL> -Subdomain <ACCOUNT SUBDOMAIN>
33+
#
34+
# Type: Full, Users
35+
# Path: The local path to backup to
36+
# Email: A superuser account email
37+
# Subdomain: The account subdomain
38+
#
39+
40+
Import-Module ShareFile
41+
#Import-Module ShareFile-Core # For PowerShell 7+
42+
$global:BackupPath = $null
43+
$global:SFClient = $null
44+
45+
function Backup-ShareFileAccount {
46+
[CmdletBinding(DefaultParameterSetName = "RequiredSet")]
47+
param(
48+
[Parameter(Mandatory,ParameterSetName = "RequiredSet")]
49+
[ValidateSet("Shared","Users","Full")]
50+
[string]$Type,
51+
[Parameter(Mandatory,ParameterSetName = "RequiredSet")]
52+
[string]$Path,
53+
[Parameter(Mandatory,ParameterSetName = "RequiredSet")]
54+
[string]$Email,
55+
[Parameter(Mandatory,ParameterSetName = "RequiredSet")]
56+
[string]$Subdomain
57+
)
58+
59+
$global:BackupPath = $Path
60+
$SFClientPath = $Path + "\" + $Email + ".sfps"
61+
62+
if ([System.IO.File]::Exists($SFClientPath)) {
63+
Write-Host $SFClientPath " was found." -BackgroundColor Green -ForegroundColor DarkBlue
64+
$global:SFClient = Get-SFClient -Name $SFClientPath
65+
# Check validity
66+
if($?){
67+
$(Send-SfRequest $SFClient -Entity Accounts) 2>&1 | out-null
68+
} else {
69+
Write-Host "Saved client file is invalid. Please sign in." -BackgroundColor Yellow -ForegroundColor DarkBlue
70+
New-SfClient -Name $SFClientPath -Account $Subdomain
71+
$global:SFClient = Get-SFClient -Name $SFClientPath
72+
}
73+
} else {
74+
Write-Host $SFClientPath " was NOT found. Please sign in." -BackgroundColor Yellow -ForegroundColor DarkBlue
75+
New-SfClient -Name $SFClientPath -Account $Subdomain
76+
$global:SFClient = Get-SFClient -Name $SFClientPath
77+
}
78+
79+
Log "A $Type backup was requested by $Email."
80+
$AccountInfo = Send-SfRequest -Client $SFClient -Method GET -Entity Accounts -select "CompanyName,subdomain,Id" | Select-Object -Property CompanyName,subdomain,Id
81+
$AccountInfo | Export-Csv -Path $BackupPath/account-$("{0:yyyyMMdd}" -f (Get-Date)).csv -NoTypeInformation
82+
83+
switch ($Type) {
84+
"Shared" { Backup-SharedFolders -SFClient $SFClient }
85+
"Users" { Backup-Users -SFClient $SFClient }
86+
"Full" { Backup-Full -SFClient $SFClient }
87+
}
88+
}
89+
90+
function Backup-Full {
91+
param(
92+
[Parameter(Mandatory)]
93+
[ShareFile.Api.Powershell.PSShareFileClient]$SFClient
94+
)
95+
96+
Log "Initiated Backup-Full function."
97+
Backup-Users -SFClient $SFClient
98+
Backup-SharedFolders -SFClient $SFClient
99+
100+
}
101+
102+
function Backup-SharedFolders {
103+
param(
104+
[Parameter(Mandatory)]
105+
[ShareFile.Api.Powershell.PSShareFileClient]$SFClient
106+
)
107+
108+
Log "Initiated Backup-SharedFolders function."
109+
$sharedfolders = Send-SfRequest -Client $SFClient -Method GET -Entity Items -Id "allshared" -Expand "Children" | Select-object -ExpandProperty Children
110+
$sharedfolders | Select-Object -Property Name,Id | Export-Csv -Path $BackupPath/sharedfolders-$("{0:yyyyMMdd}" -f (Get-Date)).csv -NoTypeInformation
111+
112+
# Local Folder Structure and Download
113+
$SharedPath = "$BackupPath/Shared Folders"
114+
if (!(Test-Path $SharedPath)) { New-Item -Path $SharedPath -Type Directory }
115+
foreach ($sharedfolder in $sharedfolders) {
116+
$Root = "$($sharedfolder.Id):/" + "$($sharedfolder.Name)"
117+
$LocalPath = "$SharedPath/"
118+
Backup-ChildItems -Root $Root -LocalPath $LocalPath -DriveId $($sharedfolder.Id) -SFClient $SFClient
119+
}
120+
121+
Log "Shared Folders backup completed."
122+
}
123+
124+
function Backup-Users {
125+
param(
126+
[Parameter(Mandatory)]
127+
[ShareFile.Api.Powershell.PSShareFileClient]$SFClient
128+
)
129+
130+
Log "Initiated Backup-Users function."
131+
$Employees = Send-SfRequest -Client $SFClient -Method GET -Entity Accounts/Employees -select "Id,Email"
132+
$Employees | Select-Object -Property Email,Id | Export-Csv -Path $BackupPath/employees-$("{0:yyyyMMdd}" -f (Get-Date)).csv -NoTypeInformation
133+
134+
# Local Folder Structure & Download
135+
$UsersPath = "$BackupPath/Users"
136+
if (!(Test-Path $UsersPath)) { New-Item -Path $UsersPath -Type Directory }
137+
foreach ($Employee in $Employees) {
138+
if (!(Test-Path "$UsersPath/$($Employee.Email)")) { New-Item -Path "$UsersPath/$($Employee.Email)" -Type Directory }
139+
$Root = "$($Employee.Id):/" + (Send-SfRequest $SFClient -Entity "Users($($Employee.Id))/HomeFolder").Name
140+
$LocalPath = "$UsersPath/$($Employee.Email)/"
141+
Backup-ChildItems -Root $Root -LocalPath $LocalPath -DriveId $($Employee.Id) -SFClient $SFClient
142+
}
143+
144+
Log "User backup completed."
145+
}
146+
147+
function Backup-ChildItems {
148+
param(
149+
[Parameter(Mandatory)]
150+
[ShareFile.Api.Powershell.PSShareFileClient]$SFClient,
151+
[Parameter(Mandatory)]
152+
[string]$Root,
153+
[Parameter(Mandatory)]
154+
[string]$LocalPath,
155+
[Parameter(Mandatory)]
156+
[string]$DriveId
157+
)
158+
New-PSDrive -Name $DriveId -PSProvider ShareFile -Client $SFClient -Root "/"
159+
Copy-SfItem -Path $Root -Destination $LocalPath
160+
Remove-PSDrive $DriveId
161+
}
162+
163+
function Log {
164+
param(
165+
[Parameter(Mandatory=$true)][String]$msg
166+
)
167+
$LogFileDate = "{0:yyyyMMdd}" -f (Get-Date)
168+
$LogTime = "[{0:MM/dd/yy} {0:HH:mm:ss}] " -f (Get-Date)
169+
Add-Content $BackupPath/log-$LogFileDate.txt ($LogTime + $msg)
170+
}

Samples/CreateUsers.ps1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Import-Module ShareFile
2+
#Import-Module ShareFile-Core # For PowerShell 7+
3+
$sfClient = New-SfClient
4+
5+
#The import file would typically be an exported contact list from Outlook or equivalent
6+
$contacts = Import-Csv .\Contacts.CSV
7+
foreach ($contact in $contacts)
8+
{
9+
#make sure we have an email, name, and company
10+
if ($contact.'E-mail Address' -and $contact.'First Name' -and $contact.'Last Name')
11+
{
12+
#create contact in ShareFile
13+
$user = New-Object ShareFile.Api.Models.User
14+
15+
#required fields
16+
$user.FirstName = $contact.'First Name'
17+
$user.LastName = $contact.'Last Name'
18+
$user.Email = $contact.'E-mail Address'
19+
20+
#optional fields
21+
$user.Company = $contact.Company
22+
23+
#create client user
24+
Send-SfRequest $sfClient -Method POST -Entity Users -Body $user -Parameters @{"addshared" = "true"}
25+
}
26+
}

0 commit comments

Comments
 (0)